import os, pdb, itertools

css_lines = []

var_lines = []

constant_name = ''

var_list = []
var_lookup = []

def extract_hex(css_list):

    counter = 1

    in_rule = False;
    
    in_hex = False;

    hex_val = ''

    hex_list = []

    for css_text in css_list:

        replaced_value = False

        usable_css = []

        for j in css_text:
            usable_css.append(j)

        counter = 0

        for i in usable_css:
            
            if i == '{':
                in_rule = True
            if i == '}':
                in_rule = False

            if i == '#' and in_rule:
                in_hex = True
                hex_begin = counter

            if in_hex:
                hex_val += i
            
            if (i == ';' or i == ' ') and in_hex:

                in_hex = False
                hex_end = hex_begin + len(hex_val) - 1

                if hex_val[-1] == ';': hex_val = hex_val[0:-1]
                
                if hex_val.lower() not in hex_list:
                    hex_list.append(hex_val.lower())

                print 'Found hex value in the following line:\n'
                print '----->\t' + ''.join(usable_css)
                var_name = raw_input('What variable shall I replace the hex code with? ')

                if var_name[0] is not '$':
                    var_name = '$' + var_name

                css_temp_after = usable_css[hex_end:]
                usable_css = usable_css[0:hex_begin]
                usable_css.extend(var_name)
                usable_css.extend(css_temp_after)

                counter += len(var_name) - len(hex_val)

                var_lookup.append((var_name, hex_val))

                hex_val = ''

            counter += 1

        css_lines.append(''.join(usable_css))

    return hex_list

if __name__ == '__main__':

    path_to_core = raw_input('What is the path to core? ')
    #path_to_core = 'C:\Documents and Settings\dworley\Desktop'

    file_to_parse = raw_input('What is the path to the file you\'d like to parse? ')
    #file_to_parse = 'C:\Program Files\Apache Group\Apache2\htdocs\mg\css\instant_messenger.css'

    sss_save_dir = raw_input('Where shall we save the site-specific SSS output? ')
    #sss_save_dir = 'C:\Program Files\Apache Group\Apache2\htdocs\mg\css\sss'

    constant_name = raw_input('What would you like to name the constants we\'ll be creating? ')
    #constant_name = 'TEST'

    constant_list = []

    constant_dict = {}

    if os.path.isdir(sss_save_dir) and os.path.isfile(file_to_parse):

        filename = os.path.splitext(os.path.split(file_to_parse)[1])[0]

        sss_output = open(os.path.join(sss_save_dir, filename + '.sss'), 'w')
    
        sss = open(file_to_parse, 'r')
        sss_text = sss.readlines()
        sss.close()

        hexcodes = extract_hex(sss_text)

        hexcodes.sort()

        for i in xrange(len(hexcodes)):
            constant_declaration = '@constant ' + constant_name + '_' + str(i + 1) + ':\t' + hexcodes[i] + ';\n'
            constant_list.append(constant_declaration)

            constant_dict[hexcodes[i]] = constant_name + '_' + str(i + 1)
            
            print constant_declaration[:-1]

        for i in var_lookup:
            print i[0] + ':\t' + constant_dict[i[1].lower()] + ';'
            var_list.append(i[0] + ':\t' + constant_dict[i[1].lower()] + ';\n')

        sss_output.writelines(constant_list)
        sss_output.write('\n\n')
        sss_output.writelines(var_list)
        sss_output.close()

        core_output = open(os.path.join(path_to_core, '_' + filename + '.sss'), 'w')
        core_output.writelines(css_lines)
        core_output.close()
        
