#pragma once

#include <ftxui/screen/screen.hpp>
#include <ftxui/dom/canvas.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Rect.hpp>
#include "point.hpp"
#include <codecvt>
#include "ansi_parser.hpp"
#include "panel.hpp"

using ftxui::Canvas, ftxui::Screen;

const int VIDEO_X = 1600;
const int VIDEO_Y = 900;
const int MIN_FONT_SIZE = 20;
const int MAX_FONT_SIZE = 140;
const int GAME_MAP_POS = 600;
const int UI_FONT_SIZE=30;
const int BASE_MAP_FONT_SIZE=90;
const wchar_t BG_TILE = L'█';
const wchar_t UI_BASE_CHAR = L'█';
const int BG_BOX_OFFSET=5;

enum class Value {
  BLACK=0, DARK_DARK, DARK_MID,
  DARK_LIGHT, MID, LIGHT_DARK, LIGHT_MID,
  LIGHT_LIGHT, WHITE, TRANSPARENT
};

struct SFMLRender {
  sf::RenderWindow $window;
  int $map_font_size;
  float $line_spacing;
  std::unordered_map<wchar_t, sf::Sprite> $sprites;
  sf::Font $font;
  sf::Texture $font_texture;
  sf::Glyph $base_glyph;
  sf::Sprite $bg_sprite;
  sf::FloatRect $bg_bounds;
  sf::Text $ui_text;
  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter;
  sf::Color $default_fg;
  sf::Color $default_bg;
  ANSIParser $ansi;
  sf::FloatRect $ui_bounds;

  SFMLRender();

  // disable copy
  SFMLRender(SFMLRender &other) = delete;

  sf::Color color(int val);
  sf::Color color(Value val);
  sf::Sprite &get_text_sprite(wchar_t tile);
  bool resize_map(int new_size, Point &view_port);
  void render_grid(const std::wstring &text, float x, float y);
  void render_text(const std::wstring &text, float x, float y);
  void draw_text(Panel &panel, bool with_border=false);
  void draw_grid(Panel &panel, float map_off_x=0.0f, float map_off_y=0.0f);

  bool poll_event(sf::Event &event) {
    return $window.pollEvent(event);
  }

  void close() { return $window.close(); }

  bool is_open() { return $window.isOpen(); }

  int font_size() { return $map_font_size; }
  void clear() { $window.clear(); }
  void display() { $window.display(); }
};
