#include "fep_client.h"
#include "fep.h"
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include "network.h"
#include "DataPacker.h"

/* Author: John A. Torres
   The fep_program_32 function creates a new RPC Client which
   sends an fep_reply structure to the ATMS Server. After the
   reply is sent, the ATMS Sends a response and the RPC Client
   is destroyed.
   */
void FEPClient::fep_program_32(char *host)
{
   CLIENT *clnt;

   /* Declerations to construct an fep_reply */
   fep_answer_info fai;
   fep_answer_short_msg fasm;
   fep_shortanswer fsa;
   fep_reply  fep_reply; // the reply to be sent to ATMS

   /* Var to recieve message back from ATMS */
   void  *rv;

   /* Create RPC Client to communicate with ATMS */
   printf("Preparing to create RPC client\n");
   clnt = clnt_create(host, FEP_PROGRAM, FEP_VERSION, "tcp");

   /* Check if client creation failed */
   if (clnt == (CLIENT *) NULL)
   {
      fprintf(stderr, "can't create client to %s\n", host);
      /* Show why rpc handle couldn't be created */
      clnt_pcreateerror("");
      exit(1);
   }

   /* Populate fep_shortmessage data */
   printf("setting reply values\n");

   // DUMMY DATA FOR SINGLE GREEN DOT
   // STATION
   // lds_id  line    drop sch lineinfo       system_key      sch_seq glo_seq         count   freeway Dir     ca_pm   lds_name
   // 1204666 45      10      11      11      1123005841      26484   1357646         19      5       N       20      LAKE FOR2

   // LOOPS
   // FWY     Dir     POSTMI  LDS_ID  VDS_ID  LOOP_ID LOC    LANE LOOP_LOC            PARAMICS_NAME  PARAMICS_LANE
   // 

   vector<LOOP*> loops;
/*
   LOOP * newLoopOne = new LOOP;
   newLoopOne->loopID = 1210490;
   newLoopOne->loop_loc = "ML_1";
   newLoopOne->vol = 0;
   newLoopOne->occ = 0;
   newLoopOne->spd = 0;

   LOOP * newLoopTwo = new LOOP;
   newLoopTwo->loopID = 1210492;
   newLoopTwo->loop_loc = "ML_2";
   newLoopTwo->vol = 0;
   newLoopTwo->occ = 0;
   newLoopTwo->spd = 0;

   LOOP * newLoopThree = new LOOP;
   newLoopThree->loopID = 1210493;
   newLoopThree->loop_loc = "ML_3";
   newLoopThree->vol = 0;
   newLoopThree->occ = 0;
   newLoopThree->spd = 0;

   LOOP * newLoopFour = new LOOP;
   newLoopFour->loopID = 1210769;
   newLoopFour->loop_loc = "RAMP_ON";
   newLoopFour->vol = 0;
   newLoopFour->occ = 0;
   newLoopFour->spd = 0;

   loops.push_back(newLoopOne);
   loops.push_back(newLoopTwo);
   loops.push_back(newLoopThree);
   loops.push_back(newLoopFour);
*/
   STATION * dummyStation = new STATION;
   dummyStation->lds = 1204666;
   dummyStation->line_num = 45;
   dummyStation->drop = 10;
   // dummyStation->loops = loops;

   dummyStation->MlTotVol = 0;
   dummyStation->OppTotVol = 0;
   dummyStation->length = dummyStation->loops.size() * 2 + 27 /* CONTROL_DATA_LEN */;

   printf("Station length: %d\n", dummyStation->length);
   // CHECK TO SEE IF I AM UPDATING THIS EVERY 30S
   /*
   for(int i = 0; i < 3; i++)
   {
      dummyStation->MlTotVol += loops.at(i)->vol;
   }
   */
   printf("Station MLTOTVOL: %d\n", dummyStation->MlTotVol);

   dummyStation->dataPack = DataPacker::packData(dummyStation);


   fep_reply.reply = SHORTPOLL;
   fep_reply.schedule = 0;
   fep_reply.lineinfo = 0;
   fep_reply.kind = (enum polltype) 0;
   fep_reply.flag = (enum replykind) 0;

   fep_reply.schedule_sequence = 0;
   fep_reply.global_sequence = 0;

   fep_reply.schedule_time = time(NULL);

   fep_reply.user_info1 = dummyStation->line_num; // LINE NUM;
   fep_reply.user_info2 = dummyStation->line_num; // LINE NUM;
   fep_reply.system_key = 0;

   fep_reply.answers.size = 1;
   fep_reply.answers.fep_answer_list_u.shortp.count = 1;

   fasm.message_len = dummyStation->length + 2; // MESSAGE LEN;
   fasm.message[0] = 0x0d;
   fasm.message[1] = 0x0a;

   for (int k = 0; k < dummyStation->length; k++) {
      printf("Adding: %d %02X\n", k, dummyStation->dataPack[k]); 
      fasm.message[2 + k] = dummyStation->dataPack[k];
   }

   int length = dummyStation->length + 2; // NEED CORRECT LENGTH
   fasm.message[length + 2] = 0x0d;
   fasm.message[length + 3] = 0xff;

   for(int l = 0; l < fasm.message_len + 2; l++)
   {
      printf("%02X", (unsigned char) fasm.message[l]);
   }
   printf("\n");

   fai.poll_error_count = 0;
   fai.poll_user_info1 = 0 ;// DROP NUMBER HERE
   fai.poll_user_info2 = 1;
   fai.retries = 0;
   fai.status = (enum replystatus) 1;

   fsa.info = fai;
   fsa.msg = fasm;
   fep_reply.answers.fep_answer_list_u.shortp.answers[0] = fsa;

   /* Make RPC Call to transfer reply to ATMS */
   printf("transferring data\n");
   rv = fep_reply_xfer_32(&fep_reply, clnt);
   printf("checking reply`\n");

   /* If ATMS reply call fails */
   if (rv == (void *) NULL)
   {
      clnt_perror(clnt, "call failed");
   }
   /* If recieved reply is successful, but empty */
   else if ((char *) rv == "")
   {
      printf("rv is empty\n");
   }
   /* If recieved reply is successful */
   else
   {
      printf("Result = %s\n", (char *)rv);
   }

   /* Destroy client */
   printf("destroying client\n");
   clnt_destroy(clnt);
   printf("returning to main\n");
}

/* Creates a single client and sends an fep_reply to the ATMS */
int main(int argc, char *argv[]) {
   char *host;

   if (argc < 2)
   {
      printf("usage:  %s server_host\n", argv[0]);
      exit(1);
   }

   /* Create RPC Client to send an fep_reply to ATMS */
   host = argv[1];
   FEPClient cli;
   cli.fep_program_32(host);

   return 0;
}
