#ifndef __SCH_PARSER_H__
#define __SCH_PARSER_H__

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <time.h>

#define SCH_TC_FILE "tc_sch_data.txt"



#define SCH_DELAY_PER_EVNT 		50  /// delay in seconds for successive events
#define SCH_DELAY_PER_TX_SEG 	10  /// delay (or sleep) in msec  for every transaction

#define SCH_SEG_RTRY_CNT 		2   //maximum re-transmit count when segment transmit fails
#define MAX_BYTE_PER_TX			1400 // value of CSP_IF_SLGND_MTU in csp
#define SCH_BYTE_PER_EVNT       50 /// Maximum  byte length of Event information sizeof(conops_sch_evnt_info_s)
#define SCH_MAX_LUT_CNT         2
#define SCH_MAX_SEG_PER_LUT     64
#define SCH_MAX_EVNT_PER_SEG    (MAX_BYTE_PER_TX / SCH_BYTE_PER_EVNT)
#define SCH_MAX_EVENT_PER_LUT   (SCH_MAX_EVNT_PER_SEG * SCH_MAX_SEG_PER_LUT)


#define EV_IN_POS           4  /// Position of Event Information sub string in each line
#define EV_UTC_POS          8  /// Position of Event UTC Execution time sub string in each line
#define EV_YEAR_POS         1  /// Position of Event Year within UTC sub string
#define EV_MON_POS          2  /// Position of Event month within UTC sub string
#define EV_DATE_POS         3  /// Position of Event date within UTC sub string




typedef enum
{
    SCH_RDY_EXEC=0,
    SCH_WAIT_TRIG,
}conops_sch_trig_type_e;


typedef struct
{
    uint16_t year;
    uint8_t month;
    uint8_t day;
    uint8_t hour;
    uint8_t min;
    uint8_t sec;
}evnt_time;

typedef struct
{
    uint8_t hour;
    uint8_t min;
    uint8_t sec;
} __attribute__((__packed__))sys_time_s;

typedef struct sch_date_info_t
{
    uint8_t date;   //conops_sch_exec_day_s
    uint8_t month;  //conops_sch_exec_day_s
} conops_sch_date_s;

typedef struct
{
    conops_sch_date_s   utc_date;
    sys_time_s          utc_time;
} __attribute__((__packed__))sch_utc_s;

typedef struct
{
    uint32_t epoch;  //sys_time_s
    uint8_t  src_id;   //conops_sch_dest_id
    uint8_t  dest_id;   //conops_sch_dest_id
    uint16_t  id;        //id present in conops_sch_dest_id
    uint32_t arg1;
    uint32_t arg2;
} __attribute__((__packed__))conops_sch_evnt_info_s;




typedef struct
{
    uint8_t lut_grp_id;     //group id of overall schedular lut info table
    uint8_t tot_seg;        //total cnt present in this group
    uint8_t crnt_seg_id;    //crnt lut info count
    uint8_t trig_type;      //conops_sch_trig_type_s
    uint8_t num_evnt;       //number of conops_sch_evnt_info_s present
    conops_sch_evnt_info_s *evnt_info_p;
} __attribute__((__packed__))conops_sch_lut_info_s;



typedef enum
{
    SCH_LUT_IDLE = 0,
    SCH_LUT_EXEC_CMPLT,
    SCH_LUT_EXEC_STOP,
    SCH_LUT_EXEC_PAUSE,
    SCH_LUT_EXEC_PROG,
    SCH_LUT_RDY,
    SCH_LUT_RX_PROG

}conops_sch_lut_grp_sts_e;



typedef struct
{
  uint8_t  trig_type;            //conops_sch_trig_type_s
  uint8_t  evnt_info_cnt;
  conops_sch_evnt_info_s *evnt_buff;
}conops_sch_lut_seg_map_s;

/**
 * @brief scheduler lut buffer structure for processing
 */
typedef struct
{
    uint8_t  grp_sts;              //conops_sch_lut_grp_sts
    uint8_t  totl_seg;             //total cnt present in this group
    uint8_t  crnt_seg_cnt;         //conops_sch_lut_grp_sts
    uint8_t  crnt_evnt_cnt;        //conops_sch_lut_grp_sts
    uint64_t totl_seg_bin;          //conops_sch_lut_grp_sts
    uint64_t rcd_seg_bin;          //conops_sch_lut_grp_sts
    uint64_t cmpl_seg_bin;         //conops_sch_lut_grp_sts
   conops_sch_lut_seg_map_s *seg_map;
}conops_sch_lut_info_gs;



void extract_event_info(char* line_buff);
void print_raw_event_info(conops_sch_evnt_info_s *_conops_sch_evnt_info);
void sch_pack_event_in_seg(uint32_t _tot_num_ev, uint8_t _exec_wait_ste);
void get_seg_info(uint8_t *tot_seg_cnt, uint64_t **seg_data_arr_p, uint32_t **seg_size_arr_p);
uint8_t sch_prsr_init(uint8_t lut_grp_id);
//void sort_sch_events_exec_order(void);

void get_time(evnt_time *utc);
void conv_date_time_to_sec_since_epoch(evnt_time *sch_evnt_info, uint32_t *sec_var);
void conv_epoch_to_localtime(evnt_time *cur_time,uint32_t epoch);
void conv_time_to_sec(sys_time_s *utc_t, int32_t *sec);
void conv_sec_to_time(int32_t sec, sys_time_s *utc);
void sch_utc_adjust_date(conops_sch_date_s *utc_date, int8_t num_days, conops_sch_date_s *new_utc_date);
void conops_sch_tm_decode(uint32_t tm_id,uint8_t *tm_frame_tm_data,uint16_t tm_len);
uint8_t conops_sch_tc(uint32_t tc_id, uint8_t** tc_data, uint16_t *tc_len);

#endif
