#pragma once
#include <string>
#include <chrono>
#include <vector>
#include <sstream>
#include <type_traits>
using namespace std::chrono_literals;
namespace easygame {
enum Unit { pix }; enum FillMode { full, outline }; enum TimeRef { after_spawning, until_closed };
enum Shape { circle, rect }; enum Anchor { top_centre, centre, bottom_centre, top_left, top_right };
enum UnitHz { hz }; enum PlayMode { async, sync }; enum WaveType { sine, square, saw, noise };
enum Preset { coin, laser, jump, explosion, win, gameover };

struct name { std::string val; explicit name(std::string v) : val(std::move(v)) {} };
struct win_name { std::string val; explicit win_name(std::string v) : val(std::move(v)) {} };
struct disp_name { std::string val; explicit disp_name(std::string v) : val(std::move(v)) {} };
struct color { std::string val; explicit color(std::string v) : val(std::move(v)) {} };
struct text { std::string val; explicit text(std::string v) : val(std::move(v)) {} };
struct dpi { int val; explicit dpi(int v) : val(v) {} };
struct fill { FillMode mode; explicit fill(FillMode m) : mode(m) {} };
struct radius { int val; explicit radius(int v) : val(v) {} };
struct thickness { float val; explicit thickness(float v) : val(v) {} };
struct key { std::string val; explicit key(std::string v) : val(std::move(v)) {} };
struct size { int x = 0, y = 0; size(int w, int h, Unit = pix) : x(w), y(h) {} size(const std::string& s); };
struct pos {
    int x = 0, y = 0; Anchor anchor = top_left; bool use_anchor = false;
    pos(int a, int b, Unit = pix) : x(a), y(b) {} pos(Anchor a) : anchor(a), use_anchor(true) {}
    pos(const size& s) : x(s.x), y(s.y) {} pos(const std::string& s) { size t(s); x = t.x; y = t.y; }
};
struct pos_from { int x = 0, y = 0; pos_from(int a, int b, Unit = pix) : x(a), y(b) {} pos_from(const pos& p) : x(p.x), y(p.y) {} };
struct pos_to { int x = 0, y = 0; pos_to(int a, int b, Unit = pix) : x(a), y(b) {} pos_to(const pos& p) : x(p.x), y(p.y) {} };
struct wait {
    std::chrono::milliseconds duration{0}; TimeRef ref;
    template<class R, class P> wait(std::chrono::duration<R, P> d, TimeRef r = after_spawning) : duration(std::chrono::duration_cast<std::chrono::milliseconds>(d)), ref(r) {}
    wait(TimeRef r) : ref(r) {}
};
struct pos3d { float x = 0, y = 0, z = 0; pos3d(float a, float b, float c) : x(a), y(b), z(c) {} };
struct size3d { float x = 1, y = 1, z = 1; size3d(float a, float b, float c) : x(a), y(b), z(c) {} };
struct rot3d { float rx = 0, ry = 0, rz = 0; rot3d(float a, float b, float c) : rx(a), ry(b), rz(c) {} };
struct radius3d { float val = 1.0f; explicit radius3d(float v) : val(v) {} };
struct look_at { float x = 0, y = 0, z = 0; look_at(float a, float b, float c) : x(a), y(b), z(c) {} };
struct fov { float val = 60.0f; explicit fov(float v) : val(v) {} };
struct button_name { std::string val; explicit button_name(std::string v) : val(std::move(v)) {} };
struct button_text { std::string val; explicit button_text(std::string v) : val(std::move(v)) {} };
using buttontext = button_text;
struct button_shape { Shape val; explicit button_shape(Shape s) : val(s) {} };
struct button_color { std::string val; explicit button_color(std::string v) : val(std::move(v)) {} };
struct button_text_color { std::string val; explicit button_text_color(std::string v) : val(std::move(v)) {} };
struct set_button_color { std::string val; explicit set_button_color(std::string v) : val(std::move(v)) {} };
struct if_clicked { std::string new_color; bool* flag = nullptr; bool val = true; if_clicked() = default; if_clicked(set_button_color c, bool& f, bool v = true) : new_color(c.val), flag(&f), val(v) {} if_clicked(bool& f, bool v = true) : flag(&f), val(v) {} };
struct button_else { bool* flag = nullptr; bool val = false; button_else() = default; button_else(bool& f, bool v = false) : flag(&f), val(v) {} };
struct input_box_text { std::string val; explicit input_box_text(std::string v) : val(std::move(v)) {} };
struct input_box_color { std::string val; explicit input_box_color(std::string v) : val(std::move(v)) {} };
struct input_box_text_color { std::string val; explicit input_box_text_color(std::string v) : val(std::move(v)) {} };
struct take_only { std::string mode; explicit take_only(std::string m) : mode(std::move(m)) {} };
struct input_to { int* i_ptr = nullptr; std::string* s_ptr = nullptr; input_to() = default; explicit input_to(int& v) : i_ptr(&v) {} explicit input_to(std::string& v) : s_ptr(&v) {} };
struct InputCond { bool expected = true; };
struct InputGotTag { InputCond operator==(bool b) const { return InputCond{b}; } };
inline constexpr InputGotTag input_got{};
struct do_after { bool wait_input = false; do_after() = default; explicit do_after(InputCond c) : wait_input(c.expected) {} };
struct file { std::string val; explicit file(std::string v) : val(std::move(v)) {} };
struct mode { PlayMode val; explicit mode(PlayMode m) : val(m) {} };
struct freq { double val; explicit freq(double v, UnitHz = hz) : val(v) {} };
struct wave { WaveType val; explicit wave(WaveType w) : val(w) {} };
struct sound { Preset val; explicit sound(Preset p) : val(p) {} };
struct wait_done { bool val; explicit wait_done(bool v) : val(v) {} };
struct volume { float val; explicit volume(float v) : val(v) {} };
struct duration { std::chrono::milliseconds ms; template<class R, class P> duration(std::chrono::duration<R, P> d) : ms(std::chrono::duration_cast<std::chrono::milliseconds>(d)) {} };

template<class T> text get_text_from(const T& val) { if constexpr (std::is_same_v<T, std::string>) return text(val); else return text(std::to_string(val)); }
namespace detail { template<class T> T ex(T d) { return d; } template<class T, class F, class... R> T ex(T d, F&& f, R&&... r) { if constexpr (std::is_same_v<T, std::decay_t<F>>) return f; else if constexpr (sizeof...(R) > 0) return ex<T>(d, std::forward<R>(r)...); else return d; } }

void make_window_impl(name, size, disp_name); void window_color_fill_impl(win_name, color, fill);
void window_text_print_impl(win_name, text, pos, dpi, color); void window_draw_rect_impl(win_name, pos, size, color, fill);
void window_draw_circle_impl(win_name, pos, radius, color, fill); void window_draw_line_impl(win_name, pos_from, pos_to, color, thickness);
void clear_window_impl(win_name, wait, do_after); void kill_window_impl(win_name, wait);
void win_button_impl(win_name, button_name, button_text, button_shape, pos, size, button_color, button_text_color, if_clicked, button_else);
void remove_button_impl(win_name, button_name); void make_input_box_impl(win_name, name, size, pos, input_box_text, input_box_color, input_box_text_color, input_to, take_only);
void set_camera_impl(win_name, pos3d, look_at, fov); void draw_cube_impl(win_name, pos3d, size3d, rot3d, color, fill);
void draw_pyramid_impl(win_name, pos3d, size3d, rot3d, color, fill); void draw_sphere_impl(win_name, pos3d, radius3d, color, fill);
void draw_grid_impl(win_name, int, float, color);
void play_file_impl(file, mode); void beep_impl(freq, duration, wave, volume, mode); void play_preset_impl(sound, volume, mode); void stop_all_sounds();
bool window_is_open(win_name wn = win_name("")); bool is_key_pressed(key k, win_name wn = win_name(""));
bool is_button_clicked(button_name bn, win_name wn = win_name("")); void frame_delay(std::chrono::milliseconds ms = 16ms);

template<class... A> void make_window(A&&... a) { auto n = detail::ex(name("default"), a...); make_window_impl(n, detail::ex(size(800, 600), a...), detail::ex(disp_name(n.val), a...)); }
template<class... A> void window_color_fill(A&&... a) { window_color_fill_impl(detail::ex(win_name(""), a...), detail::ex(color("black"), a...), detail::ex(fill(full), a...)); }
template<class... A> void window_text_print(A&&... a) { auto p = detail::ex(pos(detail::ex(size(10, 20), a...)), a...); window_text_print_impl(detail::ex(win_name(""), a...), detail::ex(text(""), a...), p, detail::ex(dpi(14), a...), detail::ex(color("white"), a...)); }
template<class... A> void window_draw_rect(A&&... a) { window_draw_rect_impl(detail::ex(win_name(""), a...), detail::ex(pos(0, 0), a...), detail::ex(size(50, 50), a...), detail::ex(color("white"), a...), detail::ex(fill(full), a...)); }
template<class... A> void window_draw_circle(A&&... a) { window_draw_circle_impl(detail::ex(win_name(""), a...), detail::ex(pos(50, 50), a...), detail::ex(radius(25), a...), detail::ex(color("white"), a...), detail::ex(fill(full), a...)); }
template<class... A> void window_draw_line(A&&... a) { window_draw_line_impl(detail::ex(win_name(""), a...), detail::ex(pos_from(0, 0), a...), detail::ex(pos_to(100, 100), a...), detail::ex(color("white"), a...), detail::ex(thickness(2.0f), a...)); }
template<class... A> void clear_window(A&&... a) { clear_window_impl(detail::ex(win_name(""), a...), detail::ex(wait(0s), a...), detail::ex(do_after(), a...)); }
template<class... A> void kill_window(A&&... a) { kill_window_impl(detail::ex(win_name(""), a...), detail::ex(wait(0s), a...)); }
template<class... A> void win_button(A&&... a) { win_button_impl(detail::ex(win_name(""), a...), detail::ex(button_name("btn"), a...), detail::ex(button_text("Button"), a...), detail::ex(button_shape(rect), a...), detail::ex(pos(centre), a...), detail::ex(size(140, 60), a...), detail::ex(button_color("blue"), a...), detail::ex(button_text_color("white"), a...), detail::ex(if_clicked(), a...), detail::ex(button_else(), a...)); }
template<class... A> void remove_button(A&&... a) { remove_button_impl(detail::ex(win_name(""), a...), detail::ex(button_name("btn"), a...)); }
template<class... A> void make_input_box(A&&... a) { make_input_box_impl(detail::ex(win_name(""), a...), detail::ex(name("input"), a...), detail::ex(size(200, 50), a...), detail::ex(pos(centre), a...), detail::ex(input_box_text(""), a...), detail::ex(input_box_color("white"), a...), detail::ex(input_box_text_color("black"), a...), detail::ex(input_to(), a...), detail::ex(take_only("all"), a...)); }
template<class... A> void set_camera(A&&... a) { set_camera_impl(detail::ex(win_name(""), a...), detail::ex(pos3d(0, 2, 7), a...), detail::ex(look_at(0, 0, 0), a...), detail::ex(fov(60.0f), a...)); }
template<class... A> void draw_cube(A&&... a) { draw_cube_impl(detail::ex(win_name(""), a...), detail::ex(pos3d(0, 0, 0), a...), detail::ex(size3d(1, 1, 1), a...), detail::ex(rot3d(0, 0, 0), a...), detail::ex(color("white"), a...), detail::ex(fill(full), a...)); }
template<class... A> void draw_pyramid(A&&... a) { draw_pyramid_impl(detail::ex(win_name(""), a...), detail::ex(pos3d(0, 0, 0), a...), detail::ex(size3d(1, 1, 1), a...), detail::ex(rot3d(0, 0, 0), a...), detail::ex(color("white"), a...), detail::ex(fill(full), a...)); }
template<class... A> void draw_sphere(A&&... a) { draw_sphere_impl(detail::ex(win_name(""), a...), detail::ex(pos3d(0, 0, 0), a...), detail::ex(radius3d(1.0f), a...), detail::ex(color("white"), a...), detail::ex(fill(full), a...)); }
template<class... A> void draw_grid(A&&... a) { draw_grid_impl(detail::ex(win_name(""), a...), 10, 1.0f, detail::ex(color("gray"), a...)); }
template<class... A> void play_file(A&&... a) { play_file_impl(detail::ex(file(""), a...), detail::ex(mode(async), a...)); }
template<class... A> void beep(A&&... a) { auto wd = detail::ex(wait_done(false), a...); beep_impl(detail::ex(freq(440, hz), a...), detail::ex(duration(200ms), a...), detail::ex(wave(sine), a...), detail::ex(volume(0.5f), a...), detail::ex(mode(wd.val ? sync : async), a...)); }
template<class... A> void play_preset(A&&... a) { auto wd = detail::ex(wait_done(false), a...); play_preset_impl(detail::ex(sound(coin), a...), detail::ex(volume(0.6f), a...), detail::ex(mode(wd.val ? sync : async), a...)); }
}
using namespace easygame;

// Дополнение v2.0: 3D-лучи, FPS-камера 360° и хитбоксы
namespace easygame {
struct pos3d_from { float x=0,y=0,z=0; pos3d_from(float a,float b,float c):x(a),y(b),z(c){} pos3d_from(const pos3d& p):x(p.x),y(p.y),z(p.z){} };
struct pos3d_to { float x=0,y=0,z=0; pos3d_to(float a,float b,float c):x(a),y(b),z(c){} pos3d_to(const pos3d& p):x(p.x),y(p.y),z(p.z){} };

void draw_line_3d_impl(win_name, pos3d_from, pos3d_to, color, thickness);
void enable_fps_camera(bool enable = true, float speed = 0.15f, float sens = 0.15f, win_name wn = win_name(""));
pos3d get_camera_pos(win_name wn = win_name(""));
pos3d get_camera_dir(win_name wn = win_name(""));
bool ray_hit_box(pos3d ray_origin, pos3d ray_dir, pos3d box_center, size3d box_size);

template<class... A> void draw_line_3d(A&&... a) {
    draw_line_3d_impl(detail::ex(win_name(""), a...), detail::ex(pos3d_from(0,0,0), a...), detail::ex(pos3d_to(0,1,0), a...), detail::ex(color("red"), a...), detail::ex(thickness(3.0f), a...));
}
}
