#include "scripts.h"

#include "logging.h"

ScriptTrader::ScriptTrader(Director &d) : Dispatch(d) {
  BUGZ_LOG(fatal) << "ScriptTrader()";
};

ScriptTrader::~ScriptTrader() { BUGZ_LOG(fatal) << "~ScriptTrader()"; }

void ScriptTrader::activate(void) {
  // ok, lookup port1 port2
  BUGZ_LOG(fatal) << "ScriptTrader::activate " << port[0] << " & " << port[1];
  auto port_info = director.galaxy.ports.find(port[0]);
  int port0_type = port_info->second.type;
  port_info = director.galaxy.ports.find(port[1]);
  int port1_type = port_info->second.type;
  BUGZ_LOG(fatal) << port0_type << " and " << port1_type;
  auto ttr = trade_type_info(port0_type, port1_type);
  trades = ttr.trades;
  // Ok, what do we do first here?
  // I - Info
  state = 1;
  to_server("I");
}

void ScriptTrader::deactivate(void) { notify(); }

void ScriptTrader::server_line(const std::string &line,
                               const std::string &raw_line) {
  // FUTURE:  powering up weapons check
}

void ScriptTrader::server_prompt(const std::string &prompt) {
  // FUTURE:  check for "Surrender/Attack"

  if (at_command_prompt(prompt)) {
    if (state == 1) {
      // Ok, decision time!
      if (director.galaxy.meta["ship"]["holds"]["c"]) {
        // holds contain colonists
        to_client("ScriptTrader FAIL: holds contain colonists.");
        deactivate();
        return;
      }

      // Which port to trade with first?  examine trades
      BUGZ_LOG(fatal) << "trades: " << trades;
      BUGZ_LOG(fatal) << "port0:" << text_from_buysell(port_buysell[0]);
      BUGZ_LOG(fatal) << "port1:" << text_from_buysell(port_buysell[1]);

      // Do we have what they are buying?
      bool have_buy = false;
      char foe[4] = "foe";
      int active_buy = 0;
      int active_sell = 0;

      for (int x = 0; x < 3; ++x) {
        if (trades.foe[x]) {
          // this is what they will be trading...

          if (director.galaxy.meta["ship"]["holds"][foe[x]]) {
            // key exists ...
            have_buy = true;
            // which port is buying?
            if (port_buysell[0].foe[x]) {
              active_buy = 0;
            } else {
              active_buy = 1;
            }
          }

          if (port_buysell[0].foe[x]) {
            active_sell = 0;
          } else {
            active_sell = 1;
          }
        }
      }

      if (have_buy) {
        BUGZ_LOG(fatal) << "have_buy: port " << active_buy;
        active_port = port[active_buy];
      } else {
        // which port is selling?

        // if they aren't buying what I have in my holds, should I check to see
        // if my holds are full?  This could be the "not buying" what I'm
        // setting bug!

        BUGZ_LOG(fatal) << "!have_buy: port " << active_sell;
        active_port = port[active_sell];
      }

      state = 2;
      if (director.current_sector == active_port) {
        // begin state 3
        state = 3;
        to_client("Trade...\n\r");
        to_server("PT");
        return;
      } else {
        // initiate move
        std::string move = std::to_string(active_port);
        to_client("Moving...\n\r");
        move.append("\r");
        to_server(move);
        return;
      }

      // for now...
      deactivate();
    }
    if (state == 3) {
      if (director.current_sector == active_port) {
        // We're here
        state = 3;
        to_server("PT");
        return;
      } else {
        // we failed to move to where we wanted to go?!
        BUGZ_LOG(fatal) << "Expecting: " << active_port << " but got "
                        << director.current_sector;
        deactivate();
        return;
      }
    }
  }

  if (state == 3) {
    if (in(prompt, " to sell ")) {
      // always sell everything
      to_server("\r");
      return;
    }

  }
}
void ScriptTrader::client_input(const std::string &cinput) { deactivate(); };