#include "gui_fsm.hpp"
#include <iostream>
#include <chrono>
#include <numeric>
#include <functional>
#include "components.hpp"
#include <numbers>
#include "systems.hpp"
#include "events.hpp"
#include "sound.hpp"

namespace gui {
  using namespace components;

  FSM::FSM() :
    $window(sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), "Zed's Raycaster Thing"),
    $main_ui($window),
    $renderer($window),
    $level($levels.current()),
    $map_ui($level),
    $combat_ui($level),
    $status_ui($level),
    $font{FONT_FILE_NAME}
    {
    }

  void FSM::event(Event ev) {
    switch($state) {
      FSM_STATE(State, START, ev);
      FSM_STATE(State, MOVING, ev);
      FSM_STATE(State, ATTACKING, ev);
      FSM_STATE(State, MAPPING, ev);
      FSM_STATE(State, ROTATING, ev);
      FSM_STATE(State, IDLE, ev);
      FSM_STATE(State, IN_COMBAT, ev);
      FSM_STATE(State, COMBAT_ROTATE, ev);
      FSM_STATE(State, NEXT_LEVEL, ev);
      FSM_STATE(State, END, ev);
    }
  }

  void FSM::START(Event ) {
    $main_ui.update_level($level);
    $level.world->set_the<Debug>({});

    $main_ui.init();

    $combat_ui.init();
    $status_ui.init();
    $status_ui.log("Welcome to the game!");
    $status_ui.update();

    $boss_fight_ui = $levels.create_bossfight($level.world);
    $boss_fight_ui->init();

    $renderer.init_terminal();
    $map_ui.create_render();
    $map_ui.resize_canvas();
    $renderer.resize_grid(BASE_MAP_FONT_SIZE, $map_ui);

    run_systems();
    state(State::IDLE);
  }

  void FSM::MAPPING(Event ev) {
    using enum Event;
    // BUG: can't close window when in mapping
    switch(ev) {
      case MAP_OPEN:
        state(State::IDLE);
        break;
      case CLOSE:
        state(State::IDLE);
        break;
      case TICK:
        break;
      default:
        dbc::log("invalid event sent to MAPPING");
    }
  }

  void FSM::MOVING(Event ) {
    // this should be an optional that returns a point
    if(auto move_to = $main_ui.play_move()) {
      System::plan_motion(*$level.world, *move_to);
      run_systems();
      $main_ui.dirty();
      state(State::IDLE);
    }
  }

  void FSM::ATTACKING(Event ev) {
    using enum Event;
    switch(ev) {
      case TICK: {
        System::combat($level);
        run_systems();
        state(State::IN_COMBAT);
       } break;
      case STOP_COMBAT:
        dbc::log("Exiting ATTACKING STATE");
        state(State::IDLE);
        break;
      case ATTACK:
        // ignore these since they're just from SFML not having discrete events
        break;
      default:
        dbc::log(fmt::format("In ATTACKING state, unhandled event {}", (int)ev));
    }
  }

  void FSM::ROTATING(Event ) {
    if($main_ui.play_rotate()) {
      state(State::IDLE);
    }
  }

  void FSM::COMBAT_ROTATE(Event ) {
    if($main_ui.play_rotate()) {
      state(State::IN_COMBAT);
    }
  }

  void FSM::IDLE(Event ev) {
    using enum Event;

    sound::stop("walk");

    switch(ev) {
      case QUIT:
        $window.close();
        state(State::END);
        return; // done
      case MOVE_FORWARD:
        try_move(1, false);
        break;
      case MOVE_BACK:
        try_move(-1, false);
        break;
      case MOVE_LEFT:
        try_move(-1, true);
        break;
      case MOVE_RIGHT:
        try_move(1, true);
        break;
      case ROTATE_LEFT:
        $main_ui.plan_rotate(-1);
        state(State::ROTATING);
        break;
      case ROTATE_RIGHT:
        $main_ui.plan_rotate(1);
        state(State::ROTATING);
        break;
      case MAP_OPEN:
        $renderer.resize_grid(BASE_MAP_FONT_SIZE, $map_ui);
        $map_ui.resize_canvas();
        state(State::MAPPING);
        break;
      case ATTACK:
        state(State::ATTACKING);
        break;
      case START_COMBAT:
        $main_ui.$overlay_ui.show_sprite("top_right", "cinqueda");
        state(State::IN_COMBAT);
        break;
      case CLOSE:
        dbc::log("Nothing to close.");
        break;
      case STAIRS_DOWN:
        sound::stop("ambient");
        state(State::NEXT_LEVEL);
        break;
      case STOP_COMBAT:
      case TICK:
        // do nothing
        break;
      default:
        dbc::sentinel("unhandled event in IDLE");
    }
  }

  void FSM::NEXT_LEVEL(Event ev) {
    using enum Event;

    switch(ev) {
      case STAIRS_DOWN:
        sound::play("ambient");
        next_level();
        state(State::IDLE);
      default:
        break; // do nothing for now
    }
  }

  void FSM::IN_COMBAT(Event ev) {
    using enum Event;

    switch(ev) {
      case ATTACK:
        $main_ui.dirty();
        sound::play("Sword_Hit_1");
        state(State::ATTACKING);
        break;
      case ROTATE_LEFT:
        $main_ui.plan_rotate(-1);
        state(State::COMBAT_ROTATE);
        break;
      case ROTATE_RIGHT:
        $main_ui.plan_rotate(1);
        state(State::COMBAT_ROTATE);
        break;
      case STOP_COMBAT:
        $main_ui.$overlay_ui.close_sprite("top_right");
        state(State::IDLE);
        break;
      case QUIT:
        $window.close();
        state(State::END);
        return;
      default:
        break;
    }
  }

  void FSM::try_move(int dir, bool strafe) {
    using enum State;
    // prevent moving into occupied space
    Point move_to = $main_ui.plan_move(dir, strafe);

    if($level.map->can_move(move_to) && !$level.collision->occupied(move_to)) {
      sound::play("walk");
      state(MOVING);
    } else {
      state(IDLE);
      $main_ui.abort_plan();
    }
  }

  void FSM::END(Event ev) {
    fmt::println("END: received event after done: {}", int(ev));
  }

  void FSM::keyboard_mouse() {
    while(const auto ev = $window.pollEvent()) {
      if(ev->is<sf::Event::Closed>()) {
        event(Event::QUIT);
      }

      if(const auto* mouse = ev->getIf<sf::Event::MouseButtonPressed>()) {
        if(mouse->button == sf::Mouse::Button::Left) {
          sf::Vector2f pos = $window.mapPixelToCoords(mouse->position);
          if(in_state(State::NEXT_LEVEL)) {
            $boss_fight_ui->mouse(pos.x, pos.y);

            if($boss_fight_ui->boss_dead()) {
              event(Event::STAIRS_DOWN);
            }
          } else {
            $combat_ui.$gui.mouse(pos.x, pos.y);
            $status_ui.$gui.mouse(pos.x, pos.y);
            $main_ui.mouse(pos.x, pos.y);
          }
        }
      }

      if(const auto* key = ev->getIf<sf::Event::KeyPressed>()) {
        using KEY = sf::Keyboard::Scan;

        switch(key->scancode) {
          case KEY::W:
            event(Event::MOVE_FORWARD);
            break;
          case KEY::S:
            event(Event::MOVE_BACK);
            break;
          case KEY::Q:
            event(Event::ROTATE_LEFT);
            break;
          case KEY::E:
            event(Event::ROTATE_RIGHT);
            break;
          case KEY::D:
            event(Event::MOVE_RIGHT);
            break;
          case KEY::A:
            event(Event::MOVE_LEFT);
            break;
          case KEY::R:
            $main_ui.$stats.reset();
            break;
          case KEY::M:
            event(Event::MAP_OPEN);
            break;
          case KEY::Escape:
            event(Event::CLOSE);
            break;
          case KEY::Space:
            event(Event::ATTACK);
            break;
          case KEY::P:
            sound::mute(false);
            $main_ui.debug();
            break;
          case KEY::O:
            autowalking = true;
            break;
          case KEY::L:
            event(Event::STAIRS_DOWN);
            break;
          default:
            break; // ignored
        }
      }
    }
  }

  void FSM::draw_gui() {
    if(in_state(State::NEXT_LEVEL)) {
      $boss_fight_ui->render($window);
    } else {
      $main_ui.render();
      $status_ui.render($window);
      $combat_ui.render($window);
    }
  }

  void FSM::render() {
    if(in_state(State::MAPPING)) {
      $window.clear();
      $map_ui.render();
      $renderer.draw($map_ui);
    } else if(in_state(State::NEXT_LEVEL)) {
      $window.clear();
      $boss_fight_ui->render($window);
    } else {
      draw_gui();
    }

    $window.display();
  }

  void FSM::run_systems() {
    System::enemy_pathing($level);
    System::collision($level);
    System::motion($level);
    System::lighting($level);
    System::death($level, $levels.$components);
  }

  bool FSM::active() {
    return !in_state(State::END);
  }

  void FSM::handle_world_events() {
    using eGUI = Events::GUI;
    auto& world = *$level.world;

    while(world.has_event<eGUI>()) {
      auto [evt, entity, data] = world.recv<eGUI>();
      auto player = world.get_the<Player>();

      switch(evt) {
        case eGUI::COMBAT: {
            auto &damage = std::any_cast<Events::Combat&>(data);

            if(damage.enemy_did > 0) {
              $status_ui.log(fmt::format("Enemy HIT YOU for {} damage!", damage.enemy_did));
            } else {
              $status_ui.log("Enemy MISSED YOU.");
            }

            if(damage.player_did > 0) {
              $status_ui.log(fmt::format("You HIT enemy for {} damage!", damage.player_did));
            } else {
              $status_ui.log("You MISSED the enemy.");
            }
          }
          break;
        case eGUI::COMBAT_START:
          event(Event::START_COMBAT);
          break;
        case eGUI::NO_NEIGHBORS:
          event(Event::STOP_COMBAT);
          break;
        case eGUI::LOOT: {
            // auto &item = std::any_cast<InventoryItem&>(data);
            // $status_ui.log(fmt::format("You picked up a {}.",
            //      std::string(item.data["name"])));
            $status_ui.log("You picked up an item.");
            $status_ui.update();
          } break;
        case eGUI::ATTACK:
          event(Event::ATTACK);
          break;
        case eGUI::STAIRS_DOWN:
          event(Event::STAIRS_DOWN);
          break;
        case eGUI::DEATH: {
            $status_ui.update();
            if(entity != player.entity) {
              $main_ui.dead_entity(entity);
            }
          } break;
        case eGUI::NOOP: {
            if(data.type() == typeid(std::string)) {
              auto name = std::any_cast<std::string>(data);
              $status_ui.log(fmt::format("NOOP EVENT! {},{} name={}", evt, entity, name));
            }
            $status_ui.update();
          } break;
        default:
          $status_ui.log(fmt::format("INVALID EVENT! {},{}", evt, entity));
          $status_ui.update();
      }
    }
  }

  void FSM::next_level() {
    $levels.create_level($level.world);
    $level = $levels.next();

    $status_ui.update_level($level);
    $combat_ui.update_level($level);
    $map_ui.update_level($level);
    $main_ui.update_level($level);
    $boss_fight_ui = $levels.create_bossfight($level.world);
    $boss_fight_ui->init();

    run_systems();
  }
}
