#include "guecs/sfml/backend.hpp"
#include "guecs/sfml/components.hpp"
#include "guecs/ui.hpp"
#include <fmt/xchar.h>
#include <deque>
#include <iostream>
#include <nlohmann/json.hpp>
#include "dbc.hpp"
#include <memory>
#include <filesystem>
#include <fstream>
#include <iostream>

constexpr const int WINDOW_WIDTH=1920;
constexpr const int WINDOW_HEIGHT=1080;
constexpr const int FRAME_LIMIT=60;
constexpr const bool VSYNC=true;
constexpr const int TITLE_SIZE=124;
constexpr const int CONTENT_SIZE=94;

using std::string, std::wstring, std::shared_ptr, std::make_shared;
using nlohmann::json;
namespace fs = std::filesystem;

struct Slide {
  guecs::UI $gui;
  wstring $title;
  wstring $content;
  json $config;
  bool $initialized = false;

  Slide(const string& title, const string& content, json& config) :
    $title(guecs::to_wstring(title)),
    $content(guecs::to_wstring(content)),
    $config(config)
  {
  }

  void init(lel::Cell& cell) {
    if(!$initialized) {
      $initialized = true;

      $gui.position(cell.x, cell.y, cell.w, cell.h);
      $gui.layout(
          "[=*%(300,200)title|_|_]"
          "[_|_|_]"
          "[=*%(300,300)content|_|_]"
          "[_|_|_]"
          "[_|_|_]");

      auto title = $gui.entity("title");
      $gui.set<guecs::Text>(title, {$title, TITLE_SIZE});

      auto content = $gui.entity("content");
      $gui.set<guecs::Text>(content, {
          $content,
          CONTENT_SIZE,
          guecs::THEME.TEXT_COLOR, 20});

      $gui.init();
    }
  }

  void render(sf::RenderWindow& window) {
    $gui.render(window);
    // $gui.debug_layout(window);
  }
};

using SlideDeck = std::vector<Slide>;


struct SlidesUI {
  guecs::UI $gui;
  shared_ptr<SlideDeck> $slides = nullptr;
  size_t $current = 0;

  SlidesUI(shared_ptr<SlideDeck> slides) {
    dbc::check(slides != nullptr, "slides is null");
    dbc::check(slides->size() > 0, "slide deck is empy");
    $slides = slides;
    $current = 0;

    $gui.position(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
    $gui.layout(
        "[t_left|t_center|t_right]"
        "[m_left|*%(300,400)slide|_|_|m_right]"
        "[_|_|_|_|_]"
        "[_|_|_|_|_]"
        "[_|_|_|_|_]"
        "[_|_|_|_|_]"
        "[b_left|b_center|b_right]"
    );
  }

  void init() {
    guecs::Background bg{$gui.$parser, };
    bg.set_color(guecs::THEME.BG_COLOR_DARK);
    $gui.set<guecs::Background>($gui.MAIN, bg);

    show_slide();

    $gui.init();
  }

  Slide& current() {
    return $slides->at($current);
  }

  void next_slide() {
    if($current < $slides->size() - 1) {
      $current++;
      show_slide();
    }
  }

  void prev_slide() {
    if($current > 0) {
      $current--;
      show_slide();
    }
  }

  void show_slide() {
    auto& slide = current();
    auto& bg = $gui.get<guecs::Background>($gui.MAIN);
    sf::Color color = guecs::THEME.FILL_COLOR;

    if(slide.$config.contains("bg_color")) {
      auto color_conf = slide.$config["bg_color"];
      color = {color_conf[0], color_conf[1], color_conf[2], color_conf[3]};
    }

    bg.set_color(color);
    bg.init();

    auto& cell = $gui.cell_for("slide");
    slide.init(cell);
  }

  void render(sf::RenderWindow& window) {
    $gui.render(window);

    auto& slide = current();
    slide.render(window);

    // $gui.debug_layout(window);
  }

  void mouse(float x, float y, guecs::Modifiers mods) {
    $gui.mouse(x, y, mods);

    if(mods.test(guecs::ModBit::left)) {
      next_slide();
    } else if(mods.test(guecs::ModBit::right)) {
      prev_slide();
    }
  }
};

struct ControlUI {
  guecs::UI $gui;
  guecs::Text* $status;
  sf::RenderWindow& $presenter;

  ControlUI(sf::RenderWindow& presenter) :
    $presenter(presenter)
  {
    $gui.position(0, 0, 400, 400);
    $gui.layout(
        "[status]"
        "[docs]"
        );
  }

  void init() {
    auto status_id = $gui.entity("status");
    $gui.set<guecs::Text>(status_id, {L""});

    auto docs_id = $gui.entity("docs");
    $gui.set<guecs::Text>(docs_id, {L"A: win left\nD: win right\nQ: quit"});

    $status = $gui.get_if<guecs::Text>(status_id);

    $gui.init();
  }

  void render(sf::RenderWindow& window) {
    auto pos = $presenter.getPosition();
    auto size = $presenter.getSize();

    $status->update(fmt::format(L"pos={},{}\nsize={},{}",
          pos.x, pos.y, size.x, size.y));

    window.clear();
    $gui.render(window);
  }
};


shared_ptr<SlideDeck> parse_slides(const string& md_file) {
  shared_ptr<SlideDeck> slides = make_shared<SlideDeck>();

  dbc::check(fs::exists(md_file), "md file missing");

  auto size = fs::file_size(md_file);
  string line(size, '\0');
  bool started = false;
  json config;

  if(std::ifstream in_file{md_file, std::ios::binary}) {
    while(std::getline(in_file, line)) {
      if(line == "{") {
        string json_data;

        do {
          json_data += line;
        } while (std::getline(in_file, line) && line != "}");

        json_data += "}";

        config = json::parse(json_data);
        std::cout << "JSON: " << config << '\n';
      } else if(line == "===") {
        fmt::println("START");
        started = true;
      } else if(line == "---") {
        fmt::println("START SLIDE");
      } else {
        if(started) {
          string title = line;
          string content;

          while(std::getline(in_file, line) && line != "---") {
            content += line + "\n";
          }

          slides->emplace_back(title, content, config);
          config = json::parse("{}");
        } else {
          fmt::println("JUNK: {}", line);
        }
      }
    }
  }

  return slides;
}

int main(int argc, char *argv[]) {
  dbc::check(argc >= 2, "USAGE: besos <monitor_count> my_fucking_slides.md");

  sfml::Backend backend;
  guecs::init(&backend);

  auto& modes = sf::VideoMode::getFullscreenModes();
  auto screen_mode = std::find_if(modes.begin(), modes.end(), [=](const auto& a) -> bool {
      return a.size.x == WINDOW_WIDTH && a.size.y == WINDOW_HEIGHT;
  });

  sf::RenderWindow controller(sf::VideoMode({400,400}), "Besos Loves Control");

  sf::RenderWindow presenter(*screen_mode,
      "Besos Loves Slides", sf::Style::None, sf::State::Windowed);

  presenter.setFramerateLimit(FRAME_LIMIT);
  presenter.setVerticalSyncEnabled(VSYNC);

  int monitor_count = argc == 3 ? atoi(argv[1]) : 1;
  auto data = argc == 3 ? parse_slides(argv[2]) : parse_slides(argv[1]);

  SlidesUI slides(data);
  slides.init();

  ControlUI control_ui(presenter);
  control_ui.init();

  while(controller.isOpen()) {
    while (const auto event = presenter.pollEvent()) {
      if(const auto* mouse = event->getIf<sf::Event::MouseButtonPressed>()) {
        sf::Vector2f pos = presenter.mapPixelToCoords(mouse->position);

        if(mouse->button == sf::Mouse::Button::Left) {
          slides.mouse(pos.x, pos.y, {1 << guecs::ModBit::left});
        } else if(mouse->button == sf::Mouse::Button::Right) {
          slides.mouse(pos.x, pos.y, {1 << guecs::ModBit::right});
        }
      }
    }

    while (const auto event = controller.pollEvent()) {
      if(event->is<sf::Event::Closed>()) {
        controller.close();
      }

      if(const auto* key = event->getIf<sf::Event::KeyPressed>()) {
        auto pos = presenter.getPosition();
        auto size = presenter.getSize();

        using KEY = sf::Keyboard::Scan;
        switch(key->scancode) {
          case KEY::A:
            if(true) {
              pos.x -= size.x;
              presenter.setPosition(pos);
            } break;
          case KEY::D:
            if(pos.x < int(size.x) * monitor_count) {
              pos.x += int(size.x);
              presenter.setPosition(pos);
            } break;
          case KEY::Q:
            controller.close();
            break;
          default:
            break;
        }

        fmt::println("window pos: {},{}", pos.x, pos.y);
      }
    }

    slides.render(presenter);
    control_ui.render(controller);
    presenter.display();
    controller.display();
  }
}
