#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
Step 4
'''
from __future__ import print_function
from base import Lo, create_csv, TARGET_DIR
import os
import csv
import sys

los = Lo()

xmodels = {
    'IMSf': {}, 'IMSD': {}, 'IMHf': {}, 'IMHD': {}, 'IFSf': {}, 'IFSD': {},
    'IFHf': {}, 'IFHD': {}, 'CMSf': {}, 'CMSD': {}, 'CMHf': {}, 'CMHD': {},
    'CFSf': {}, 'CFSD': {}, 'CFHf': {}, 'CFHD': {}, 'PMSf': {}, 'PMSD': {},
    'PMHf': {}, 'PMHD': {}, 'PFSf': {}, 'PFSD': {}, 'PFHf': {}, 'PFHD': {},
}
fs = ('Case1_LS.csv', 'Case1_Gender.csv', 'Case1_Level.csv', 'Case1_SciF.csv')


def get_match_all():
    result = {}
    fpath = os.path.join(TARGET_DIR, 'part3', 'step3_match_all.csv')
    with open(fpath, 'rb') as f:
        rows = csv.reader(f)
        for r in rows:
            result[r[0]] = r[1].split(',')
    return result


def get_weight(xmodel):
    result = {}
    fpath = os.path.join(TARGET_DIR, 'part2', '%s-3.csv' % xmodel)
    with open(fpath, 'rb') as f:
        rows = csv.reader(f)
        for r in rows:
            # result[r[0]] = [r[1], r[2], r[3], r[4], r[5], r[6]]
            result[r[0]] = [r[k] for k in xrange(1, 26)]
    return result


def get_x_from_step3_all(m):
    '''
    July 1 requested
    '''
    fpath = os.path.join(TARGET_DIR, 'part3', '%s-all.csv' % m)
    los = {}
    with open(fpath, 'rb') as f:
        rows = csv.reader(f)
        for r in rows:
            for lo in r[1].split(','):
                los[lo] = r[0]
    return los


def produce(match_all):
    for ii in xmodels.keys():
        rows = []
        # prepare for data  --> July 1 request
        step_3_data = get_x_from_step3_all(ii)
        weight = get_weight(ii)
        for lo in sorted(los.los):
            is_matched = 1 if ii.lower() == los.los[lo]['model'].lower() else 0
            # [July 1] some lo doesn't have that data, blank then
            july_1_col = step_3_data[lo] if lo in step_3_data else ''
            no_of_tree = '4Ts' if july_1_col == '4/4' else \
                '3Ts' if july_1_col == '3/4' else \
                '2Ts' if july_1_col == '2/4' else '1Ts'
            r = [no_of_tree, lo, is_matched, los.los[lo]['weight']]
            try:
                r += weight[lo]
                rows.append(r)  # this will append only LO w/ weight
            except KeyError:
                # print(ii, ' = ', is_matched, ' / ', lo)
                pass
        _f = '%s-step4.csv' % ii
        create_csv(_f, rows, directory='part4')


def main(*argv):
    ma_data = get_match_all()
    produce(ma_data)


if __name__ == '__main__':
    if len(sys.argv) > 1:
        main(sys.argv[1:])
    else:
        main()