#include "event_router.hpp"
#include "dbc.hpp"
#include "events.hpp"

namespace gui {
  namespace routing {
    using enum Event;
    using enum State;

    game::Event Router::process_event(std::optional<sf::Event> ev) {
      $next_event = game::Event::TICK;

      if(ev->is<sf::Event::Closed>()) {
        return game::Event::QUIT;
      }

      if(const auto* mouse = ev->getIf<sf::Event::MouseButtonPressed>()) {
        if(mouse->button == sf::Mouse::Button::Left || mouse->button == sf::Mouse::Button::Right) {
          left_button = mouse->button == sf::Mouse::Button::Left;
          position = mouse->position;
          event(MOUSE_DOWN);
        }
      } else if(const auto* mouse = ev->getIf<sf::Event::MouseButtonReleased>()) {
        // need to sort this out but if you don't do this it thinks you're always pressing it
        if(mouse->button == sf::Mouse::Button::Left || mouse->button == sf::Mouse::Button::Right) {
          left_button = mouse->button == sf::Mouse::Button::Left;
          position = mouse->position;
          event(MOUSE_UP);
        }
      } else if(const auto* mouse = ev->getIf<sf::Event::MouseMoved>()) {
        position = mouse->position;
        event(MOUSE_MOVE);
      }

      if(const auto* key = ev->getIf<sf::Event::KeyPressed>()) {
        scancode = key->scancode;
        event(KEY_PRESS);
      }

      return $next_event;
    }

    void Router::event(Event ev) {
      switch($state) {
        FSM_STATE(State, START, ev);
        FSM_STATE(State, IDLE, ev);
        FSM_STATE(State, MOUSE_ACTIVE, ev);
        FSM_STATE(State, MOUSE_MOVING, ev);
        FSM_STATE(State, MOUSE_DRAGGING, ev);
      }
    }

    void Router::START(Event ) {
      state(State::IDLE);
    }

    void Router::IDLE(Event ev) {
      switch(ev) {
        case MOUSE_DOWN:
          move_count=0;
          set_event(game::Event::TICK);
          state(State::MOUSE_ACTIVE);
          break;
        case MOUSE_UP:
          set_event(game::Event::MOUSE_CLICK);
          state(State::IDLE);
          break;
        case MOUSE_MOVE:
          set_event(game::Event::MOUSE_MOVE);
          break;
        case KEY_PRESS:
          set_event(game::Event::KEY_PRESS);
          break;
        default:
          dbc::sentinel($F("invalid event: {}", int(ev)));
      }
    }

    void Router::MOUSE_ACTIVE(Event ev) {
      switch(ev) {
        case MOUSE_UP:
          set_event(game::Event::MOUSE_CLICK);
          state(State::IDLE);
          break;
        case MOUSE_MOVE:
          move_count++;
          set_event(game::Event::MOUSE_DRAG);
          state(State::MOUSE_MOVING);
          break;
        case KEY_PRESS:
          set_event(game::Event::KEY_PRESS);
          state(State::IDLE);
          break;
        default:
          dbc::sentinel("invalid event");
      }
    }

    void Router::MOUSE_MOVING(Event ev) {
      switch(ev) {
        case MOUSE_UP: {
          dbc::check(move_count < $drag_tolerance, "mouse up but not in dragging state");
          set_event(game::Event::MOUSE_CLICK);
          state(State::IDLE);
         } break;
        case MOUSE_MOVE:
          move_count++;

          if(move_count < $drag_tolerance) {
            set_event(game::Event::MOUSE_DRAG);
          } else {
            set_event(game::Event::MOUSE_DRAG_START);
            state(State::MOUSE_DRAGGING);
          }
          break;
        case KEY_PRESS:
          set_event(game::Event::KEY_PRESS);
          break;
        default:
          dbc::sentinel("invalid event");
      }
    }

    void Router::MOUSE_DRAGGING(Event ev) {
      switch(ev) {
        case MOUSE_UP:
          set_event(game::Event::MOUSE_DROP);
          state(State::IDLE);
          break;
        case MOUSE_MOVE:
          move_count++;
          set_event(game::Event::MOUSE_DRAG);
          break;
        case KEY_PRESS:
          set_event(game::Event::KEY_PRESS);
          break;
        default:
          // invalid events: 1
          dbc::sentinel($F("invalid events: {}", int(ev)));
      }
    }
  }
}
