#!/usr/bin/python3

import sys
import argparse
import ifpostprocessinglib
from ifpostprocessinglib.ifpostprocessing import Postprocessing as postp
#import matplotlib
import matplotlib.pylab as plt


# Instantiate the parser
parser = argparse.ArgumentParser(description='Tool to extract information to be plotted.')
# Required FILENAME
parser.add_argument("file_name", type=str, help='Name of the log file')
# Required Attributes to be Plotted
parser.add_argument("attributes", nargs='+', help='List of attributes to be plotted. <all> will return all attributes.')
# Optional Zero Timestamp
parser.add_argument("-z", "--zerotimestamp", help="Start plot from time 0", action="store_true")
# Parse input arguments
args = parser.parse_args()

att_data_list = postp.split_logfile(args.file_name, args.zerotimestamp)
thisdict = postp.sort_attribute_data(att_data_list, args.attributes)
'''
att_data_list = postp.split_logfile('/home_local/eltdev/iftest-sener_april_2023/m2/tests/robot/results/cii-m2_test-07_ps_leg_mode_20230419-092055.log', 0)
thisdict = postp.sort_attribute_data(att_data_list, 'main_ps_sttlm_lrcurr_actuator_length')
'''

list_attributes = list(thisdict.keys())

fig, ax = plt.subplots(figsize=(10, 7))
ax.set_xlabel('Time')
ax.set_ylabel('Value')

for x in range (len(list_attributes)):
    #value is a list with all the data for this attribute, [[timestamp0, [act0, act1, act2... act15]], [timestamp1, [act0, act1, act2... act15]]]
    value = thisdict[list_attributes[x]]
    timestamplist = []    
    datalist = []
    single_datalist = []
    timestamp = -1
    
    for y in range(len(value)):
        #timestamplist.append(value[y][0])
        timestamp += 1
        timestamplist.append(timestamp)


    for j in range(len(value[y][1])):
        for k in range(len(value)):
            single_datalist.append(value[k][1][j])
        datalist.append(single_datalist)
        single_datalist =[]
   
    for h in range(len(datalist)):
        ax.plot(timestamplist,datalist[h])    
        ax.legend(list_attributes,loc='center left', bbox_to_anchor=(1, 0.5))
        plt.tight_layout(pad=0.2, h_pad=None, w_pad=None, rect=None)
    
#print (timestamplist)
plt.show(block=True)        
