#pragma once
#include "game/game-const.hpp"
#include "a83/random.hpp"
#include "UI.hpp"
#include "enity/enity.hpp"
//! радар игрока
class Radar {
private:
  constx auto max_distance = Game::SHIP_FIX_DIST;
  Image bg = {}; ///< фон радара
  constx std::array<float, 4> v_dist = {
    max_distance,
    Game::BULLET_KILL_DIST * 2.5f,
    Game::BULLET_KILL_DIST,
    Game::Y * 3.0f
  };
  size_t dist_mode = 0; ///< текущее разрешение радара
  constx Vec pos = Vec(Game::X / 2.0f, Game::Y / 2.0f); ///< центр радара
  constx float radius = Game::Y / 2.0f - 32.0f; ///< радиус радара
  float get_cur_scale() const { return (1.0f / v_dist[dist_mode]) * radius; }
public:
  a83::Str target = {};
  Radar() {
    // создание картинки фона радара
    bg.init(Game::X, Game::Y, 0);
    target = "Destroy all enemy"; // TODO seted by level wave's
  }
  ~Radar() = default;
  void draw(Image& dst) const {
    cauto col = Color::get_gray(a83::irand() % 112);
    draw_radar(dst, col);
    draw_screen_rect(get_cur_scale(), dst, col);
    draw_text(dst);
    draw_bars(dst);
  } // draw
  //! меняет разрешение радара
  void next_dist_mode() { dist_mode = (dist_mode + 1) % v_dist.size(); }
private:
  void draw_points(Image& dst) const {
    for (nauto p: vp_enity) {
      if (p->is_live()) {
        auto pos2 = pos2radar(p->phys.pos);
        if (pos2.test()) {
          pos2 += pos;
          Color col;
          // выбор цвета для сущности
          switch (p->type) {
            case Enity_t::bonus: a83::error("Radar.draw_points: bonus impl"); break;
            case Enity_t::player:
            case Enity_t::boss:
            case Enity_t::enemy: col = Color::WHITE; break;
            case Enity_t::bullet: col = Color::RED; break;
            case Enity_t::particle: col = Color::WHITE2/2; break;
            default: col = Color::BLACK; break;
          }
          p->draw_mask_resized(pos2.x, pos2.y, get_cur_scale(), dst, col);
        } // if pos vec != null
      } // if live
    } // for vp_enity
  } // draw_points
  //! преобразование кординат объектов в корды радара
  Vec pos2radar(Vec val) const {
    if (distance(val, p_plr->phys.pos) >= v_dist[dist_mode]) {
      // когда объект за радаром, его точка рисуется на краю
      val.normalize();
      return val * v_dist[dist_mode] * get_cur_scale();
    }
    return val * get_cur_scale();
  } // pos2radar
  //! рисует радар
  void draw_radar(Image& dst, CN(Color) ring_col) const {
    // bground:
      bg.fast_print(dst);
      const int num_rings = v_dist.size() - dist_mode;
      FOR (i, num_rings)
        dst.draw_circle(ring_col, pos.x, pos.y,
          radius * (v_dist[i] / max_distance));
      draw_points(dst);
      // cross:
      if (dist_mode > 1) { // маленькие крестики не рисовать
        cauto cross_size = 2 * (dist_mode + 1) + 1;
        FOR (x, cross_size)
          dst.fast_set(x + int(pos.x) - (cross_size / 2), pos.y, ring_col);
        FOR (y, cross_size)
          dst.fast_set(pos.x, y + int(pos.y) - (cross_size / 2), ring_col);
      }
  } // draw_radar
  //! рисует прямоугольник экрана
  void draw_screen_rect(float scale, Image& dst, CN(Color) col) const {
    cauto mx = Game::X * scale;
    cauto my = Game::Y * scale;
    cauto x0 = std::ceil(pos.x - mx / 2.0f);
    cauto y0 = std::ceil(pos.y - my / 2.0f);
    dst.draw_rect(x0, y0, mx, my, col);
  } // draw_screen_rect
  //! рисует текст в радаре
  void draw_text(Image& dst) const {
    constexpr int pos_text_x = 5;
    constexpr int pos_text2_x = 200;
    constexpr int pos_time_y = 5;
    constexpr int pos_res_y = pos_time_y + 11;
    constexpr int pos_target_y = 5;
    // TODO localize
    Game::font->print(dst, pos_text_x, pos_time_y,
      sconv<utf32>("TIME: " + a83::s2t(Game::game_time)) );
    Game::font->print(dst, pos_text_x, pos_res_y,
      sconv<utf32>("RES: " + a83::n2s(v_dist[dist_mode], 2)) );
    Game::font->print(dst, pos_text2_x, pos_target_y,
      sconv<utf32>("TARGET:\n" + target) );
  } // draw_text
  //! рисует полоски HP и EN
  void draw_bars(Image& dst) const {
    constexpr int hp_x = 20;
    constexpr int hp_y = 230;
    constexpr int en_x = hp_x;
    constexpr int en_y = hp_y + 13;
    constexpr int hp_mx = 100;
    constexpr int hp_my = 5;
    Game::font->print(dst, hp_x - 15, hp_y - 2, U"HP ");
    UI::draw_loadbar(dst, hp_x, hp_y, hp_mx, hp_my,
      p_plr->hp, dcast(CP(Ship_info), p_plr->p_info)->hp,
      Color::BLACK, Color::WHITE);
    Game::font->print(dst, en_x - 15, en_y - 2, U"EN ");
    UI::draw_loadbar(dst, en_x, en_y, hp_mx, hp_my,
      p_plr->en.val, p_plr->en.size, Color::BLACK, Color::WHITE);
  } // draw_bars
}; // Radar
namespace UI {
  Radar radar = {};
  bool radar_enabled = false;
}