# coding=UTF-8
# -*- coding: UTF-8 -*-

##import sys
##setdefaultencoding('utf-8')
#general imports
import string, re
import os as oslib
import codecs
import pprint
import utilswin

##from odict import OrderedDict

#templating engine imports
from genshi.template import TemplateLoader
from genshi.core import Stream
from genshi.output import encode, get_serializer
from genshi.output import DocType
dirname_ = oslib.path.dirname(__file__)
##dirname_ = "D:\\Balt-MoksluiCD\\LPM-DVD\\autorun\\html\\generator\\".replace('\\', '/')
templates_path = oslib.path.join(dirname_, 'templates/')
print templates_path
loader = TemplateLoader( templates_path )
template = loader.load('main.html')


#config imports
import configs as config
import utils, mylibs, some_logic

#for future refractoring to make loop os and lang possible 
##targetos = config.targetos
##targetbrowser = config.targetbrowser 
##default_lang = config.default_lang  #now targetlang

 
# ----------------               ----------------
def pack_data_fields_2dict(namelist = config.data_fields , extranamelist =  []):
    #newly generated  variables are prepended with _
    item = dict()
    for field in namelist + extranamelist:
        exec "item['%s'] = %s" % (field, field)
    return item

#read data
f = codecs.open('src.txt', encoding='utf-8')
lines = f.readlines()
f.close()



for targetlang in config.langs:          #TODO: main loop - lang might go out
    langpart = '' #todo: in multilanguage   langpart=targetlang
    pagename_titles = dict(zip(config.pagenames,  config.titles[targetlang]))
##    for targetos , targetoslabel in config.oslabels.items():
    for targetos , targetoslabel in [('w', 'Windows')]:
        print ' --===######## %s #########===--' % targetoslabel
        progindexlist = [] #all occurencies of programs  (in all pages/views)
        _screenshots = []
        for pi in range(len(config.pages)): #[1, 3] 
            page = config.pages[pi]
            pagename = config.pagenames[pi]
            if config.pages[pi] == '-': pagename = '__just_4generating_some_lch'
            ## init vals
            _prev_header = ''
    ##        out = ""
            
            pagecatlist = []  # used to generate linked category TOC 
            proglist = []
            category_progs_counts = []
            _catname_list = []
            category_sizes = []# {}
            category_sizes_cumulative = []#{}
            catcount = 0

    ##        category_progs = []
    ##        category_out = ''

            catname_last  = '' # to trigger the change of category in search-list
            linenr = 1 ## linecount starts after header
            empty_data_str = ', '.join(["''" for i in range (len(config.data_fields))])
            for line in lines[1:]:
                linenr += 1            
                inputline2list = re.split(r'\t', line)
                inputline2list = inputline2list[:len(config.data_fields)] ## chop if there is too much data fields in source
                try: exec  "%s = inputline2list" % config.data_fields_str # get the row data 
                except(ValueError): 
                    if len(inputline2list) < len(config.data_fields): 
                        print "error[not enough data fields] (skip line) \n %d: %s" % (linenr, line)
                    else: print "error[assingning the data fields values] (skip line) \n %d: %s" % (linenr, line)
                    exec  "%s = [%s]" % (config.data_fields_str, empty_data_str) # get the row data 
                
                if lang == '': lang = config.default_lang #TODO: new functionality - to make multilanguage handling easier ;)
                os_alternatives_raw = os
                os = os.split('/')[0]  ## untested are separated with /
                os = os.split('?')[0]
                
                _catnames =  catname.split('|')         ##get/split/prepair languages info
                catname = _catnames[0].strip(); #todo: should be index of default_lang
                _catname = utils.latinize(catname)

                if not (catname.strip() in ['', '!!!'] or catname == catname_last):
                    catname_last = catname
                    if catcount > 0: category_sizes[catcount-1]  =  category_progs_counts[catcount-1]  # add previous final info to dict -- this dict is just for convenience
                    if catcount == 1: category_sizes_cumulative.append( category_progs_counts[catcount-1] )
                    if catcount > 1:  category_sizes_cumulative.append( category_sizes_cumulative[catcount-2] + category_progs_counts[catcount-1] )               
                    pagecatlist.append(catname)
                    _catname_list.append(_catname)
                    catcount += 1
                    category_progs_counts.append(0)
                    category_sizes.append(0)
                    

                
                filter_brackets_re = re.compile( '\(.*?\)')
                progname4work = filter_brackets_re.sub('', progname)
                _progname = utils.latinize(progname4work)
                _progname = _progname.strip(' -').lower()
                _progname = re.sub(r'[ -]+', '-', _progname )
    ##               category_progs.append(_progname)

                
                _icon = utils.look_for_img(_progname)
                _screenshot = utils.look_for_img(_progname,  img_subdir = 'ss')
                
                #about = utils.filter_linkify(utils.filter_os(targetos, about))
                if about.strip() == '...':
                    about = u'(žr.  aprašymą žemiau)'
                about = about.strip(' -')
                about = about.replace('--', '<br />')
                about =  utils.filter_linkify(about)
                about = utils.filter_os(targetos, about)
                        
                if progwww == '':
                    progwww = "http://www.google.lt/search?q=%s&btnI=I'm Feeling Lucky" % progname4work
                if downloadwww == '':
                    downloadwww = "http://www.google.lt/search?q=%s+download&btnI=I'm Feeling Lucky" % progname4work

                localised_int = 0;
                # ------------------------- check if we should include ----------------------- 
                if some_logic.check_include_program(usefor, os, progname, catname, importance, page, targetos,  lang, targetlang):
                    oslist = []
                    if os_alternatives_raw.find('l') >-1 :  oslist.append('l')
                    if os_alternatives_raw.find('w') >-1 : oslist.append('w')
                    
                    _install , _windirectrun , _winportableinstall , _install_path , _install_dir , _install_type,  _file_lch, _diropen_lch =  ['']*8 # '', '', '', '', '', '', '', ''
                    if targetos == 'w' and wininstall != '-': 
                        _install_type, _install_path, _install_dir, _file_lch, _diropen_lch = utilswin.prepair_lch_path(_progname, wininstall)  ##_install =  ...
                        if windirectrun != '':
                            _windirectrun, _, _windirectrun_lch, _, _ = utilswin.prepair_lch_path(_progname+'.direct', windirectrun)  
                        if winportableinstall != '':
                            _winportableinstall, _, _winportableinstall_lch, _, _ = utilswin.prepair_lch_path(_progname+'.portableinstall', winportableinstall)   
                            
                        localised = localised.strip()
                        try: localised_int = int(localised)
                        except ValueError: localised_int = 0
                    
                    if  targetos == 'l' and targetos in oslist and not linuxrun in ['-']:  # LINUX  '-' means 'no'
                        if linuxrun == '': 
                            linuxrun = _progname
                        if linuxrun.find('file://') ==-1: 
                            linuxrun = 'run://'+linuxrun
                            linuxrun = linuxrun.replace('run://_', 'run-terminal://')
                    
                    catname = catname_last
                    _catname = utils.latinize(catname)
                    
                    extranamelist_str = 'progname4work, _progname, _catname, _icon, _screenshot' +  \
                        ', _install_type, _install_path, _install_dir,  _file_lch, _diropen_lch, _windirectrun, _winportableinstall, oslist, localised_int' ;
                    proglist.append( pack_data_fields_2dict( extranamelist = extranamelist_str.split(', ') ) )
                    progindexlist.append( pack_data_fields_2dict( extranamelist = extranamelist_str.split(', ') + ['pagename', 'catname', '_catname' ] ) )  ##for search/index
                    category_progs_counts[catcount-1]  += 1
    ##                if _screenshot:
    ##                    _screenshots.append

            # ****** END Application Area loop ********
            category_sizes_cumulative.append( category_sizes_cumulative[catcount-2] + category_progs_counts[catcount-1] ) #just silly itteration finalizing step
            
            #  -----------   Feeding the template -------------
            stream = template.generate(\
                           programs = proglist, \
                           categories = pagecatlist, \
                           items = config.pagenames,\
                           titles = config.menus[targetlang],
                           maintitle = config.maintitles[targetlang],\
                           title_current = '', \
                           pagename_current = config.pagenames[pi],\
                           pageid = pi, \
                           pagename_titles = pagename_titles, \
                           targetos = targetos, \
                           category_progs_counts = category_progs_counts, \
                           category_sizes = category_sizes, \
                           category_sizes_cumulative=category_sizes_cumulative, \
                           #_catname_list=_catname_list\
                           )
            html = stream.render('html')
            pagepath = config.html_root_rel2generator + config.oshtmldirs[targetos] +langpart+ pagename+'.html'
            mylibs.putftext(pagepath , html)
            print "==%s== %s"  % (pagename, pagepath)
        
        
        
        #  -----------   Feeding the template -----= progindexlist =--------
        pagename = 'indexlist'
        stream = template.generate(\
                       progindexlist = progindexlist, \
                       programs = [], \
                       categories = [], \
                       items = config.pagenames,\
                       titles = config.menus[targetlang],
                       maintitle = config.maintitles[targetlang],\
                       title_current = '', \
                       pagename_current = 'indexlist',\
                       pagename_titles = pagename_titles, \
                       pageid = None, \
                       targetos = targetos, \
                       category_progs_counts = category_progs_counts, \
                       category_sizes = category_sizes, \
                       category_sizes_cumulative=category_sizes_cumulative, \
                       #_catname_list=_catname_list\
                       )
        html = stream.render('html', doctype=DocType.HTML5)
        pagepath = config.html_root_rel2generator + config.oshtmldirs[targetos] +langpart+ pagename+'.html'
        mylibs.putftext(pagepath , html)
        print "==%s== %s"  % (pagename, pagepath)
        
        rawfiles = oslib.listdir('raw_content/%s/' % targetlang) 
        if targetlang == 'lt': rawfiles  += map(lambda x:  '_detaliau/' + x, oslib.listdir('raw_content/%s/_detaliau/' % targetlang))
        rawfiles = [ f for f in rawfiles if f[-4:] == 'html'] 
        ##os.path.walk('raw_content', callback_scan, r'.*\.html')
        pagenr = 0
        for fname in rawfiles:
            fname = fname.replace('\\', '/')
            pagename = fname.split('/')[-1] 
            pagename = pagename[:-5]
            print fname
            
            rawhtml = mylibs.getftext('raw_content/'+targetlang+'/'+fname)        
            if fname.find('_detaliau')>-1:
                no_lch = re.compile(r'<a href=\".+?\.lch\">.+?</a>')
                rawhtml = no_lch.sub( "", rawhtml)
            
            page = dict(zip(config.pagenames,  config.pages)).get(pagename, '')
            title_current = pagename_titles.get(pagename, '')
            stream = template.generate(\
                   rawhtml = rawhtml.decode("UTF-8"), \
                   programs = [], \
                   categories = [], \
                   items = config.pagenames,\
                   titles = config.menus[targetlang],\
                   maintitle = config.maintitles[targetlang],\
                   title_current = title_current, \
                   pagename_current = pagename,\
                   pagename_titles = pagename_titles, \
                   pageid = None, \
                   targetos = targetos, \
                   category_progs_counts = [], \
                   category_sizes = [], \
                   category_sizes_cumulative=[]
                   
                   #_catname_list=_catname_list\
                   )
             
            
            
            html = stream.render(method='xhtml')
    ##        encode(generator, method=method, encoding=encoding)
    ## http://mail.mems-exchange.org/durusmail/quixote-users/3414/
            
            if targetos=='l':
                no_doctype_4gtkhtml = re.compile(r'<\!DOCTYPE [^>]+?>')
                html = no_doctype_4gtkhtml.sub( "", html)
                
           
            pagepath = config.html_root_rel2generator + config.oshtmldirs[targetos] +langpart +pagename+'.html'
            mylibs.putftext(pagepath , html)
            print "==%s== %s"  % (pagename, fname)
