import sys, os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import my_config as config

import logging
from src.getDf import readCsvFiles, addFundPerf, readStatics, getFeatureDf, dropOff, dropOffTrade
from src.calStatics import calStatics
from src.getQuantDf import getQuantDf
from src.calQuantCode import calQuantDf

import numpy as np
import pandas as pd

import json

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__file__)

def debug(df):
    print df.columns.values

def mergeDailyWeekly(d_quant, w_quant):
    daily_size = d_quant.shape[0]
    quant = pd.merge(d_quant, w_quant, on='DATE', how='outer')

    #Find the first weekly is Not NaN
    drop_index = 0
    for drop_index, row in quant.iterrows():
        if not pd.isnull(row['SPY_weekly']):
            break
    
    quant.drop(quant.index[0:drop_index], inplace=True)
    quant = quant.reset_index(drop=True)

    print quant.columns.names

    temp_dict = {}
    for index, row in quant.iterrows():
        print index
        print row
        for name in w_quant.columns.values:
            if pd.isnull(row[name]):
                row[name] = temp_dict[name]
            else:
                temp_dict[name] = row[name]
    quant.drop(quant.index[daily_size:], inplace=True)
    quant = quant.reset_index(drop=True)

    return quant

def trim(df):
    index = 0
    start_index = 0
    end_index = df.shape[0]
    for index, row in df.iterrows():
        date = row['date']
        if date >= config.START_DATE:
            start_index = index
            break

    for index, row in df.iterrows():
        date = row['date']
        if date >= config.END_DATE:
            end_index = index
            break

    df = df[start_index:end_index]
    df = df.reset_index(drop=True)
    return df


'''
Read price df, read statics data, for each record, calculate it's quantitative code
'''
if __name__ == '__main__':

    usage = "Usage: GetQuantCode, this program will calculate the quantitiave codes of trading data"
    print usage

    d, w, m = getFeatureDf()
    d_stat, w_stat = readStatics()

    d = dropOff(d, 'daily')
    print "Usage: 8 daily Features: KD, KD_SLOPE, ROC, MFI_SLOPE, HIST_MOM, MFI, KD_RANK, MFI_RANK"

    if sys.argv[1] == 'run':
        d = trim(d)
        w = trim(w)
    debug(d)
    d_quant = calQuantDf(d, d_stat, 'daily')
    debug(d_quant)

    w = dropOff(w, 'weekly')
    debug(w)

    print "Usage: 4 weekly features: KD, MFI, KD_RANK, MFI_RANK"
    w_quant = calQuantDf(w, w_stat, 'weekly')

    debug(w_quant)
    if d.shape[0] != d_quant.shape[0] or w.shape[0] != w_quant.shape[0]:
        logger.error("Wrong quant size")
        exit(1)

    quant_df = mergeDailyWeekly(d_quant, w_quant)

    d = dropOffTrade(d, quant_df.iloc[0]['DATE'])
    print "Save trade pickle to disk"
    d.to_pickle(config.TRADE_DF_PICKLE)

    print "Save trade quant pickle to disk"
    quant_df.to_pickle(config.QUANT_DF_PICKLE)

    usage = "Usage: GetQuantCode, save trade_quant to pickle, save quant_df to pickle"
    print usage

