import threading, os
from ..configuration import config
from extract import extract
from .load import set_variables
from transform import *
import MySQLdb
import cardsharp as cs

__all__ = ['WriteThread', 'TransformThread', 'DemoTransformThread']

class WriteThread(threading.Thread):
    def __init__(self, segment, queue, filename, var_names): #var_meta
        threading.Thread.__init__(self)
        self.queue = queue
        self.filename = filename
        
        if not os.path.isdir(os.path.join(config.out_dir, 'recid', segment)):
            import time
            from random import random
            time.sleep(1*random())
            if not os.path.isdir(os.path.join(config.out_dir, 'recid', segment)):
                os.makedirs(os.path.join(config.out_dir, 'recid', segment))
        
        path = os.path.join(config.out_dir, 'recid', segment, filename.replace('.sav', '.txt'))
        if os.path.isfile(path):
            os.unlink(path)   
        self.file = open(path, 'a')
        self.file.write('%s\n' % '|'.join(var_names))
        
    def run(self):
        while True:
            #line = self.queue.get()
            #self.ds.add_row(line)
            self.file.write(''.join(['|'.join([str(i) if i is not None else '' for i in self.queue.get()]), '\n']))
            self.queue.task_done()
            #print 'trace run'
        
        self.file.close()
        #self.ds.save(source=self.source, format='spss', overwrite=True)
    
    
class TransformThread(threading.Thread):
    def __init__(self, queue, chflat_queue, chsum_queue, db_info, variables, max_cycles, varcodes):
        threading.Thread.__init__(self)
        self.queue = queue
        self.chflat_queue = chflat_queue
        self.chsum_queue = chsum_queue
        self.db = MySQLdb.connect(host=config.db_host, user=db_info['user'], passwd=db_info['pass'], 
                           charset='utf8', db=db_info['name'], port=config.db_port, use_unicode=True)
        self.variables = variables
        self.max_cycles = max_cycles
        self.varcodes = varcodes 
        
    def run(self):
        while True:
            transform_options = self.queue.get()
            arrest_records = extract('arrest', transform_options['id'], self.db)
            sentence_records = extract('sentence', transform_options['id'], self.db)
            chflat, chsum = transform(arrest_records, sentence_records, self.variables, self.max_cycles, self.varcodes, **transform_options)
            
            self.chflat_queue.put(chflat)
            self.chsum_queue.put(chsum)
            self.queue.task_done()

class DemoTransformThread(threading.Thread):
    def __init__(self, queue, out_queue, db_info, variables, max_profiles, varcodes):
        threading.Thread.__init__(self)
        #initialize queues and database connection
        self.queue = queue
        self.out_queue = out_queue
        self.db = MySQLdb.connect(host=config.db_host, user=db_info['user'], passwd=db_info['pass'], 
                           charset='utf8', db=db_info['name'], port=config.db_port, use_unicode=True)
        self.max_profiles = max_profiles
        self.variables = variables
        self.varcodes = varcodes
        
    def run(self):
        while True:
            transform_options = self.queue.get()
            #extract the demographic records
            demo_records = extract('%s.demographic' % config.db_stand_name, transform_options['id'], self.db)
            #transform the demogrraphic records and add to out_queue for flat file load
            self.out_queue.put(transfrom_demographic(transform_options['id'], demo_records, self.max_profiles, self.varcodes))
            self.queue.task_done()