#pragma once

#include "icons.hpp"
#include "vertex_engine/utils/utils.hpp" // IWYU pragma: keep

namespace VE::style::fonts
{
  inline std::string font_name = "/Montserrat/Montserrat-";
  inline std::vector<float> font_size = { 8.f,  9.f,  10.f, 11.f, 12.f, 13.f, 14.f, 15.f, 16.f, 17.f, 18.f, 19.f, 20.f, 21.f, 22.f };

  enum font_type
  { 
    BOLD, BOLD_ITALIC, EXTRA_BOLD, EXTRA_BOLD_ITALIC, EXTRA_LIGHT, EXTRA_LIGHT_ITALIC,
    ITALIC, LIGHT, LIGHT_ITALIC, MEDIUM, MEDIUM_ITALIC, REGULAR, SEMI_BOLD, SEMI_BOLD_ITALIC, THIN, THIN_ITALIC, ICON
  };

  inline std::vector<std::string> fonts_path 
  {
    var::FONT_PATH + font_name + "Bold.ttf",
    var::FONT_PATH + font_name + "BoldItalic.ttf",

    var::FONT_PATH + font_name + "ExtraBold.ttf",
    var::FONT_PATH + font_name + "ExtraBoldItalic.ttf",

    var::FONT_PATH + font_name + "ExtraLight.ttf",
    var::FONT_PATH + font_name + "ExtraLightItalic.ttf",

    var::FONT_PATH + font_name + "Italic.ttf",

    var::FONT_PATH + font_name + "Light.ttf",
    var::FONT_PATH + font_name + "LightItalic.ttf",
    
    var::FONT_PATH + font_name + "Medium.ttf",
    var::FONT_PATH + font_name + "MediumItalic.ttf",
    
    var::FONT_PATH + font_name + "Regular.ttf",

    var::FONT_PATH + font_name + "SemiBold.ttf",
    var::FONT_PATH + font_name + "SemiBoldItalic.ttf",
    
    var::FONT_PATH + font_name + "Thin.ttf",
    var::FONT_PATH + font_name + "ThinItalic.ttf"
  };

  inline std::map<font_type, int> font_step
  {
    { font_type::BOLD,                 0   },
    { font_type::BOLD_ITALIC,          15  },
    { font_type::EXTRA_BOLD,           30  },
    { font_type::EXTRA_BOLD_ITALIC,    45  },
    { font_type::EXTRA_LIGHT,          60 },
    { font_type::EXTRA_LIGHT_ITALIC,   75 },
    { font_type::ITALIC,               90 },
    { font_type::LIGHT,                105 },
    { font_type::LIGHT_ITALIC,         120 },
    { font_type::MEDIUM,               135 },
    { font_type::MEDIUM_ITALIC,        150 },
    { font_type::REGULAR,              165 },
    { font_type::SEMI_BOLD,            180 },
    { font_type::SEMI_BOLD_ITALIC,     195 },
    { font_type::THIN,                 210 },
    { font_type::THIN_ITALIC,          225 },
    { font_type::ICON,                 240 },
  };                                   
                                      
  inline void init()
  {
    ImGuiIO& io = ImGui::GetIO();

    for (auto& p : fonts_path)
      for (auto size : font_size)
        io.Fonts->AddFontFromFileTTF(p.c_str(), size, NULL, io.Fonts->GetGlyphRangesCyrillic());

    // add icon font size
    static const ImWchar icon_ranges[] = { ICON_MIN_FK, ICON_MAX_FK, 0 };
    for (auto size : font_size)
      io.Fonts->AddFontFromFileTTF(var::ICONS_PATH.c_str(), size, NULL, icon_ranges);
  };

  inline ImFont* get_font(font_type type = font_type::REGULAR, int size = 16)
  {
    if (size < 0) size = 8;
    if (size > 33) size = 33;
    size -= 8; // т.к. font_size начинается с 8.f

    ImGuiIO& io = ImGui::GetIO();
    ImFontAtlas* atlas = io.Fonts;

    auto pos = font_step[type] + size;
    return atlas->Fonts[pos];
  };
}
