from shutil import copyfile
from datetime import datetime
from ExcelMapper.mapper import *
import xlrd
import xlsxwriter
row_rules_sheet1_t1 = {
	'found "risk"': lambda data: 'risk' in data['type'],
	'found "Risk"': lambda data: 'Risk' in data['type'],
	'found "reward"(ignore letter casing)':  lambda data: 'reward' in data['type'].lower() or 'reward' in data['type'].lower()}

row_rules_sheet1_t2 = row_rules_sheet1_t1

col_rules_sheet1_t1 = {
	'"high"' : lambda data: 'high' in data['amount'],
	#'return true' : lambda data: True,
	'return false' : lambda data: False}
	
col_rules_sheet1_t2 = {
	'found "low"' : lambda data: 'low' in data['amount'],
	'return true' : lambda data: True,
	'return false' : lambda data: False}

row_rules_sheet2_t3 = {
	'found "fire"': lambda data: 'fire' in data['type'],
	'found "Fire"': lambda data: 'Fire' in data['type'],
	'found "damage"(ignore letter casing)':  lambda data: 'damage' in data['type'].lower()}

col_rules_sheet2_t3 = {
	'"low"' : lambda data: 'low' == data['amount'],
	'"high"':   lambda data: 'high' == data['amount']}
	
def main():
	date = str(datetime.now().date())
	print "Maping"
	excel_template = xlrd.open_workbook("map.xlsx")
	copyfile('map.xlsx', "map {}.xlsx".format(date))
	excel_data = xlrd.open_workbook("data.xlsx")
	t1_excel_mapper = create_mapper(wb=excel_template,table_index=1,row_rules=row_rules_sheet1_t1,col_rules=col_rules_sheet1_t1)
	t1_output_data = t1_excel_mapper.run(excel_data)
	t2_excel_mapper = create_mapper(wb=excel_template,table_index=2,row_rules=row_rules_sheet1_t2,col_rules=col_rules_sheet1_t2)
	t2_output_data = t2_excel_mapper.run(excel_data)
	t3_excel_mapper = create_mapper(wb=excel_template,table_index=3,row_rules=row_rules_sheet2_t3,col_rules=col_rules_sheet2_t3)
	t3_output_data = t3_excel_mapper.run(excel_data)
	
	workbook = xlsxwriter.Workbook('output {}.xlsx'.format(date))
	worksheet = workbook.add_worksheet()
	for (row,col),results in t1_output_data.iteritems():
		worksheet.write(row, col,len(results))
	for (row,col),results in t2_output_data.iteritems():
		worksheet.write(row, col,len(results))
	worksheet = workbook.add_worksheet()
	for (row,col),results in t3_output_data.iteritems():
		

	workbook.close()
	print "Done."

def clone_sheet(to_clone_sheet,new_sheet):
	for row in range(0,to_clone_sheet.nrows):
		for col in range(0,to_clone_sheet.ncols):
			new_sheet.write(row,col,to_clone_sheet.cell_value(row, col))
	
if __name__ == "__main__":
	main()

worksheet.write(row, col,len(results))