import os, sys
sys.path.append('.')
import pandas as pd
import scipy as sp
import numpy as np


def main(input):
    if len(input) != 2:
        print('Usage: python3 ./scripts/createkpi.py <fulltrace directopry>')
        sys.exit(1)
    root = input[1]
    listdir = os.listdir(root)
    df_all = pd.DataFrame(columns=['q1','q2','q3','q4','q1_uptime','q2_uptime','q3_uptime','q4_uptime','q1_speed','q2_speed','q3_speed','q4_speed','q1_variance','q2_variance','q3_variance','q4_variance','f01_dis','f02_dis','f03_dis','f04_dis','f05_dis','f06_dis','f07_dis','f08_dis','f09_dis','f10_dis','f11_dis','f12_dis','f13_dis','f14_dis','f15_dis','f16_dis','f17_dis','f18_dis','f19_dis','f20_dis','f01_rate','f02_rate','f03_rate','f04_rate','f05_rate','f06_rate','f07_rate','f08_rate','f09_rate','f10_rate','f11_rate','f12_rate','f13_rate','f14_rate','f15_rate','f16_rate','f17_rate','f18_rate','f19_rate','f20_rate','f01_stroke','f02_stroke','f03_stroke','f04_stroke','f05_stroke','f06_stroke','f07_stroke','f08_stroke','f09_stroke','f10_stroke','f11_stroke','f12_stroke','f13_stroke','f14_stroke','f15_stroke','f16_stroke','f17_stroke','f18_stroke','f19_stroke','f20_stroke'])

    for index in listdir:
        try:
            filepath = os.path.join(root, index)
            df = pd.read_csv(filepath)
            q1 = df[(df['status']=='in') & (df['x'] >= 0) & (df['y'] >= 0)]
            # in q2 is the frame which status is 'in' and value of x is over 0, y is under 0.
            q2 = df[(df['status']=='in') & (df['x'] >= 0) & (df['y'] < 0)]
            # in q3 is the frame which status is 'in' and value of x, y is under 0.
            q3 = df[(df['status']=='in') & (df['x'] < 0) & (df['y'] < 0)]
            # in q4 is the frame which status is 'in' and value of x is under 0, y is over 0.
            q4 = df[(df['status']=='in') & (df['x'] < 0) & (df['y'] >= 0)]
            
            q1_stroke = []
            q1_frame = 0
            for x in q1.index:
                #find the nearest status 'out' frame before next 'in' frame.
                q1_out = df[(df['status']=='out') & (df.index > x)].index[0]
                q1_stroke.append((x, q1_out))
                q1_frame += q1_out - x

            q2_stroke = []
            q2_frame = 0
            for x in q2.index:
                #find the nearest status 'out' frame before next 'in' frame.
                q2_out = df[(df['status']=='out') & (df.index > x)].index[0]
                q2_stroke.append((x, q2_out))
                q2_frame += q2_out - x

            q3_stroke = []
            q3_frame = 0
            for x in q1.index:
                #find the nearest status 'out' frame before next 'in' frame.
                q3_out = df[(df['status']=='out') & (df.index > x)].index[0]
                q3_stroke.append((x, q3_out))
                q3_frame += q3_out - x

            q4_stroke = []
            q4_frame = 0
            for x in q4.index:
                #find the nearest status 'out' frame before next 'in' frame.
                q4_out = df[(df['status']=='out') & (df.index > x)].index[0]
                q4_stroke.append((x, q4_out))
                q4_frame += q4_out - x    
            q1_uptime = q1_frame / len(df)
            q2_uptime = q2_frame / len(df)
            q3_uptime = q3_frame / len(df)
            q4_uptime = q4_frame / len(df)


            #get 'velocity' of each frame of q1_stroke
            q1_velocity = []
            for x in q1_stroke:
                q1_velocity.append(df['velocity'][x[0]:x[1]].mean())
            q1_velocity_mean = np.mean(q1_velocity)
            q1_velocity_var = np.var(q1_velocity)

            q2_velocity = []
            for x in q2_stroke:
                q2_velocity.append(df['velocity'][x[0]:x[1]].mean())
            q2_velocity_mean = np.mean(q2_velocity)
            q2_velocity_var = np.var(q2_velocity)

            q3_velocity = []
            for x in q3_stroke:
                q3_velocity.append(df['velocity'][x[0]:x[1]].mean())
            q3_velocity_mean = np.mean(q3_velocity)
            q3_velocity_var = np.var(q3_velocity)

            q4_velocity = []
            for x in q4_stroke:
                q4_velocity.append(df['velocity'][x[0]:x[1]].mean())
            q4_velocity_mean = np.mean(q4_velocity)
            q4_velocity_var = np.var(q4_velocity)
            df_20 = np.array_split(df, 20)
            #count the number of not 'idle' status in each part
            active_rate_20 = []
            stroke_20 = []
            for x in df_20:
                active_rate_20.append(len(x[x['status']!='idle']))
                #count the number of 'in' status in each part
                stroke_20.append(len(x[x['status']=='in']))
            
            #divde each number by the number of all frames in each part
            active_rate_20 = np.array(active_rate_20) / len(df)
            dis_20 = []
            for x in df_20:
                dis_20.append(x['positionvectorvalue'].diff().abs().sum())
            stroke_20_sum = np.array(dis_20) / len(df_20)

            #make a list of all variables
            all_variables = [len(q1), len(q2), len(q3), len(q4), q1_uptime, q2_uptime, q3_uptime, q4_uptime, q1_velocity_mean, q1_velocity_var, q2_velocity_mean, q2_velocity_var, q3_velocity_mean, q3_velocity_var, q4_velocity_mean, q4_velocity_var]
            all_variables.extend(dis_20)
            all_variables.extend(active_rate_20)
            all_variables.extend(stroke_20_sum)
            #add a list to df_all
            df_all.loc[len(df_all)] = all_variables
            
        except Exception as e:
            print(e)
            continue
    #save df_all to csv file
    df_all.to_csv('kpi.csv', index=False)


if __name__ == '__main__':
    try:
        main(sys.argv)
    except Exception as e:
        print(e)
        sys.exit(1)