#include "create_unit.h"
#include "rosa/config/config.h"
#include <stdio.h>
//#include "rlutil.h"

// using namespace rlutil;

Agent create_agent() { return create_agent(NO_NAME); }

Agent create_agent(const char *name) {
  Agent agent(name);

  printf(" > Agent ");
  // setColor(TXTCOLOR_LIGHTCYAN);
  printf("%s ", agent.get_name());
  // setColor(TXTCOLOR_GREY);
  printf("(id: %03u) ", agent.get_id());
  // setColor(TXTCOLOR_LIGHTGREEN);
  printf("created\n");
  // setColor(TXTCOLOR_GREY);

  return agent;
}

Sensor create_sensor() { return create_sensor(NO_NAME); }

Sensor create_sensor(const char *name) {
  Sensor sensor(name);

  printf(" > Sensor ");
  // setColor(TXTCOLOR_LIGHTCYAN);
  printf("%s ", sensor.get_name());
  // setColor(TXTCOLOR_GREY);
  printf("(id: %03u) ", sensor.get_id());
  // setColor(TXTCOLOR_LIGHTGREEN);
  printf("created\n");
  // setColor(TXTCOLOR_GREY);

  return sensor;
}

HistoryModule create_historyModule(unsigned int history_length,
                                   int delimitation_mode) {
  return create_historyModule(NO_NAME, history_length, delimitation_mode);
}

HistoryModule create_historyModule(const char *name,
                                   unsigned int history_length,
                                   int delimitation_mode) {

  HistoryModule historyModule(name);

  printf(" > History ");
  // setColor(TXTCOLOR_LIGHTCYAN);
  printf("%s ", historyModule.get_name());
  // setColor(TXTCOLOR_GREY);
  printf("(id: %03u) ", historyModule.get_id());
  // setColor(TXTCOLOR_LIGHTGREEN);
  printf("created\n");
  // setColor(TXTCOLOR_GREY);
  if (historyModule.set_maxHistoryLength(history_length)) {
    printf("   > History length ");
    // setColor(TXTCOLOR_LIGHTGREEN);
    printf("set ");
    // setColor(TXTCOLOR_GREY);
    printf("to %u\n", history_length);
  } else {
    // setColor(TXTCOLOR_LIGHTRED);
    printf("   > historyLength could not set (out of allowed range).");
    // setColor(TXTCOLOR_GREY);
  }
  if (historyModule.set_delimitationMode(delimitation_mode)) {
    printf("   > Delimitation Mode ");
    // setColor(TXTCOLOR_LIGHTGREEN);
    printf("set ");
    // setColor(TXTCOLOR_GREY);
    printf("to %u\n", delimitation_mode);
  } else {
    // setColor(TXTCOLOR_LIGHTRED);
    printf("   > Delimitation Mode could not set (out of allowed range).");
    // setColor(TXTCOLOR_GREY);
  }

  return historyModule;
}

Channel create_channel(unsigned int transfer_rate) {
  return create_channel(NO_NAME, transfer_rate);
}

Channel create_channel(const char *name, unsigned int transfer_rate) {

  Channel channel(name);

  printf(" > Channel ");
  // setColor(TXTCOLOR_LIGHTCYAN);
  printf("%s ", channel.get_name());
  // setColor(TXTCOLOR_GREY);
  printf("(id: %03u) ", channel.get_id());
  // setColor(TXTCOLOR_LIGHTGREEN);
  printf("created\n");
  // setColor(TXTCOLOR_GREY);

  if (channel.set_transferRate(transfer_rate)) {
    if (transfer_rate != 0) {
      printf("   > transfer rate ");
      // setColor(TXTCOLOR_LIGHTGREEN);
      printf("set ");
      // setColor(TXTCOLOR_GREY);
      printf("to %i\n", transfer_rate);
    } else {
      printf("   > transfer ");
      // setColor(TXTCOLOR_LIGHTGREEN);
      printf("set ");
      // setColor(TXTCOLOR_GREY);
      printf("to immediately transportation\n" /*, transfer_rate*/);
    }
  } else {
    // setColor(TXTCOLOR_LIGHTRED);
    printf("   > Transfer Rate out of allowed bounds!\n");
    // setColor(TXTCOLOR_GREY);
  }

  return channel;
}

Testbench create_testbench() {
  Testbench testbench;
  printf(" > ");
  // setColor(TXTCOLOR_LIGHTCYAN);
  printf("%s ", testbench.get_name());
  // rlutil::setColor(TXTCOLOR_GREY);
  printf("(id: %03u) ", testbench.get_id());
  // rlutil::setColor(TXTCOLOR_LIGHTGREEN);
  printf("created\n");
  // rlutil::setColor(TXTCOLOR_GREY);
  return testbench;
}

Testbench create_testbench(const char *name) {
  Testbench testbench(name);
  printf(" > ");
  // rlutil::setColor(TXTCOLOR_LIGHTCYAN);
  printf("%s ", testbench.get_name());
  // rlutil::setColor(TXTCOLOR_GREY);
  printf("(id: %03u) ", testbench.get_id());
  // rlutil::setColor(TXTCOLOR_LIGHTGREEN);
  printf("created\n");
  // rlutil::setColor(TXTCOLOR_GREY);
  return testbench;
}

FILE *make_file_pointer(const char *filepath, int mode) {

  FILE *fpointer;
  //	bool file_opened;

  if (mode == CSV_MODE_READ) {
#ifdef ROSA_WINDOWS
    fopen_s(&fpointer, filepath, "r");
#else
    fpointer = fopen(filepath, "r");
#endif
    // file_opened = fopen_s(&fpointer, filepath, "r");
  } else if (mode == CSV_MODE_WRITE) {
#ifdef ROSA_WINDOWS
    fopen_s(&fpointer, filepath, "r");
#else
    fpointer = fopen(filepath, "r");
#endif
    // file_opened = fopen_s(&fpointer, filepath, "w");
  } else {
    printf("File pointer mode for \"%s\" ", filepath);
    // rlutil::setColor(TXTCOLOR_LIGHTRED);
    printf("is not supported!\n");
    // rlutil::setColor(TXTCOLOR_GREY);

    return NULL;
  }

  if (fpointer /*file_opened == 0*/) {
    return fpointer;
  }

  printf("File pointer \"%s\" ", filepath);
  // rlutil::setColor(TXTCOLOR_LIGHTRED);
  printf("could not created!\n");
  // rlutil::setColor(TXTCOLOR_GREY);

  return NULL;
}

void print_csv_reader(CSVreaderModule csvReaderModule, const char *filepath) {
  printf(" > ");
  // rlutil::setColor(TXTCOLOR_LIGHTCYAN);
  printf("%s ", csvReaderModule.get_name());
  // rlutil::setColor(TXTCOLOR_GREY);
  printf("(id: %03u) for \"%s\" ", csvReaderModule.get_id(), filepath);
  // rlutil::setColor(TXTCOLOR_LIGHTGREEN);
  printf("created\n");
  // rlutil::setColor(TXTCOLOR_GREY);
}

CSVreaderModule create_CSVreaderModule(const char *filepath,
                                       unsigned int column,
                                       unsigned int start_row) {

  FILE *fpointer = make_file_pointer(filepath, CSV_MODE_READ);

  if (fpointer) {
    CSVreaderModule csvr(fpointer, column, start_row);
    print_csv_reader(csvr, filepath);

    return csvr;
  } else {
    CSVreaderModule csvr;

    return csvr;
  }
}

CSVreaderModule create_CSVreaderModule(const char *name, const char *filepath,
                                       unsigned int column,
                                       unsigned int start_row) {

  FILE *fpointer = make_file_pointer(filepath, CSV_MODE_READ);

  if (fpointer) {
    CSVreaderModule csvr(name, fpointer, column, start_row);
    print_csv_reader(csvr, filepath);

    return csvr;
  } else {
    CSVreaderModule csvr;

    return csvr;
  }
}
