#pragma once

#include "Theme.hpp"
#include "ochess.hpp"
#include <map>
#include <tuple>
#include <utility>
#include <vector>
#include <wx/artprov.h>
#include <wx/dcbuffer.h>

// Local events
wxDECLARE_EVENT(PLAY_MOVE_EVENT, wxCommandEvent);

#define REFRESH_MOUSE_LOCATION()                                               \
  {                                                                            \
    const wxPoint pt = wxGetMousePosition();                                   \
    mouseX = pt.x - this->GetScreenPosition().x;                               \
    mouseY = pt.y - this->GetScreenPosition().y;                               \
  }

#define INIT_CURRENT_SQUARE()                                                  \
  std::uint32_t file = 7 - (mouseX - boardX) / square_width;                   \
  std::uint32_t rank = (mouseY - boardY) / square_width;                       \
  if (!black_side) {                                                           \
    file = 7 - file;                                                           \
    rank = 7 - rank;                                                           \
  }                                                                            \
  bool IsCurrentSquareValid = file >= 0 && file <= 7 && rank >= 0 && rank <= 7;

#define MOUSE_ON(x, y, width, height)                                          \
  (mouseX >= (x) && mouseX <= ((x) + (width)) && mouseY >= (y) &&              \
   mouseY <= ((y) + (height)))

#define CAPTURE_FACTOR 0.35
#define SQUARE_NUM_PADDING 5
typedef std::tuple<short, short, short> ClockTime;

// Drawing buffer (ANIMATIONS)
typedef struct AnimState {
  /// @brief Temporary buffer to reduce latency
  wxBitmap *buffer;
  /// @brief Should *buffer be used?
  bool reuseBuffer;
  /// @brief Current animated frame
  int frame;
  /// @brief Total number of frames for the animation
  int frames;
  /// @brief Animation durations (in ms)
  int duration,duration_fast;
  /// @brief Animation FPS
  std::uint8_t fps;
  /// @brief Current animated piece
  char piece_moved;
  /// @brief Starting point of the animated piece
  wxPoint src;
  /// @brief Translation vector of the animated piece
  wxPoint transVect;
} AnimState;

class BoardCanvas : public wxPanel {
  // *t is theme for board+pieces and
  // *t_captures is theme for captured pieces (scale down version of t)
  Theme *t, *t_captures;
  // Board to draw (char version)
  std::string board;
  std::string white_player,black_player;

  // Various canvas state variables
  bool black_side, is_dragging, valid_drag, is_black_turn;
  std::uint32_t boardX, boardY, square_width, mouseX, mouseY, lastClickX,
      lastClickY;
  wxSize canvas_size;
  wxPoint active_square;
  std::map<char, std::uint8_t> captures;
  ClockTime black_time, white_time;
  bool frozen,lock_square_size;

  // Current animation state
  AnimState adata;

public:
  BoardCanvas(wxFrame *parent);
  BoardCanvas(wxFrame *parent,std::uint32_t square_width, bool frozen);
  ~BoardCanvas();
  void ApplyPreferences();
  void DrawBoard(wxDC &dc);
  void OnPaint(wxPaintEvent &event);
  void MouseEvent(wxMouseEvent &event);
  void Zoom(std::int32_t zoom);
  void Swap();
  void OnResize(wxSizeEvent &e);
  void SetupBoard(std::string board, bool is_black_turn,
                  std::map<char, std::uint8_t> captures,
                  std::string white_player, std::string black_player);
  void Animate(const std::string &board, bool is_black_turn, std::map<char, std::uint8_t> captures, std::string src, std::string dst,bool faster);
  void SetClockTime(short hours, short min, short sec, bool IsBlack);
};