from Translator import *
import Object, StyleSheet, ValueList, PrimitiveValue

class SSSImportRule(Object.SSSObject):
    
    def __init__(self, rule = None):

        super(SSSImportRule, self).__init__()

        self._values = None
    
        #self._parse_only = False
        
        if rule: self._parse(rule);
        
    def _parse(self, rule):

        matches = preg_match('@import\s*([^\;]*)\;?$', rule.strip())
        
        if matches:

            self._values = ValueList.SSSValueList(matches[0], self)

            for i in xrange(self._values.length):

                item = self._values.item(i)
                
                if item.valueType == PrimitiveValue.SSSPrimitiveValue.CSS_URI:

                    self.href = item.value

                #if item.valueType == PrimitiveValue.SSSPrimitiveValue.CSS_STRING and item.value == 'parse-only':

                    #self._parse_only = True

    def getPropertyCSSValue(self,val):
        return self._styleSheet.getPropertyCSSValue(val)

    def get_href(self):
        self._styleSheet.href
    
    def set_href(self, value):        
        self._styleSheet = StyleSheet.SSSStyleSheet()
        self._styleSheet._setParentObject(self)
        self._styleSheet.href = value
    href = property(get_href, set_href)

    def set_cssText(self, rule):
        return self._parse(rule)

    def get_cssText(self):
        return self.__str__()
    cssText = property(get_cssText, set_cssText)

    def get_type(self):
        return Object.SSSObject.IMPORT_RULE
    type = property(get_type)

    def __str__(self):

        string = "@import"

        if self._styleSheet.href.rfind('.sss') == -1:
            href = self._styleSheet.href
            string += " url(" + href + ")"
        else:
            href = self._styleSheet.href[0:self._styleSheet.href.rfind('.sss')]
            string += " url(" + href + ".css)"

        string += ";"
        
        #if (self._parse_only):

            #string = self._parentStyleSheet.createCommentText(string)

        return string + "\n\n"
