#pragma once
#include <string>
#include <unordered_map>
#include <functional>
#include <optional>
#include <vector>

namespace lel {

  struct Cell {
    int x = 0;
    int y = 0;
    int w = 0;
    int h = 0;
    int mid_x = 0;
    int mid_y = 0;
    int max_w = 0;
    int max_h = 0;
    int col = 0;
    int row = 0;
    bool right = false;
    bool bottom = false;
    bool expand = false;
    bool center = false;
    bool percent = false;

    Cell(int col, int row) : col(col), row(row) {}
    Cell() {}
  };

  using Row = std::vector<std::string>;
  using CellMap = std::unordered_map<std::string, Cell>;

  struct Parser {
    int grid_x = 0;
    int grid_y = 0;
    int grid_w = 0;
    int grid_h = 0;
    Cell cur;
    std::vector<Row> grid;
    CellMap cells;

    Parser(int x, int y, int width, int height);
    Parser();

    void position(int x, int y, int width, int height);
    void id(std::string name);
    void reset();
    bool parse(std::string input);
    void finalize();
    std::optional<std::string> hit(int x, int y);
  };

  Cell center(int width, int height, Cell &parent);
}