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

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

        super(SSSIncludeRule, self).__init__()

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

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

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

            for i in xrange(self._values.length):
                
                #pdb.set_trace()

                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.INCLUDE_RULE
    type = property(get_type)

    def __str__(self):
        string = self.createCommentText('@include ' + self._styleSheet.href) + '\n'
        if not self._parse_only:
            string += self._styleSheet.cssText
        return string
