#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;


struct RenderConfig {
  int video_x = 1600;
  int video_y = 900;
  int ui_font_size=30;
  int base_map_font_size=90;
  wchar_t bg_tile = L'█';
  wchar_t ui_base_char = L'█';
  int bg_box_offset=5;
  int game_map_x=40;
  int game_map_y=40;
};

struct SFMLRender {
  RenderConfig $config;
  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::Sprite &get_text_sprite(wchar_t tile);
  bool resize_grid(int new_size, Panel &panel_out);
  void render_grid(const std::wstring &text, sf::Color default_fg, sf::Color default_bg, float x, float y);
  void render_text(const std::wstring &text, sf::Color default_fg, sf::Color default_bg, float x, float y);

  void draw(Panel &panel, float x_offset=0.0f, float y_offset=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(); }
};
