#include <catch2/catch_test_macros.hpp>
#include <fmt/core.h>
#include <ftxui/screen/terminal.hpp>  // for ColorSupport, Color, Palette16, Palette256, TrueColor
#include <ftxui/dom/elements.hpp>  // for hflow, paragraph, separator, hbox, vbox, filler, operator|, border, Element
#include "panel.hpp"
#include "ansi_parser.hpp"
#include "render.hpp"

using namespace ftxui;
using namespace fmt;
using std::string;

void test_ansi_parsing(Panel &panel) {
  sf::Color default_fg(0,0,0);
  sf::Color default_bg(100,100,100);
  // this sets the Truecolor so need to do it first
  ANSIParser ansi(default_fg, default_bg);

  bool good = ansi.parse(panel.to_string(),
    [&](sf::Color color, sf::Color bgcolor){
      // ignore color
    },
    [&](wchar_t ch) {
      // ignore char
  });

  REQUIRE(good == true);
}

TEST_CASE("can render a simple text panel", "[panel]") {
  SFMLRender::init_terminal();
  Panel text_panel(0, 0, 20, 5);

  bool show_modal = false;

  auto buttons = Container::Horizontal({
      Button("OK", [&]{ show_modal = false; }),
      Button("CANCEL", [&]{ show_modal = false; }),
  });

  auto text_box = Renderer([buttons]{
      return hbox({
        hflow(
            vbox(text("I AM TEXT")),
            buttons->Render()
        )});
    });

  text_panel.set_renderer(text_box);
  text_panel.add(buttons);

  text_panel.resize(10,10);
  text_panel.render();
  test_ansi_parsing(text_panel);

  const Screen &screen = text_panel.screen();
  REQUIRE(screen.dimx() == 10);
  REQUIRE(screen.dimx() == 10);
}

TEST_CASE("can render a simple grid panel", "[panel]") {
  SFMLRender::init_terminal();

  Panel grid_panel(20, 20, 20, 5, true);

  auto text_box = Renderer([]{
      return hbox({
        hflow(vbox(text("I AM TEXT")))});
    });

  grid_panel.set_renderer(text_box);
  grid_panel.resize(10,10);
  grid_panel.render();
  test_ansi_parsing(grid_panel);
}
