#include "game.h"
#include "level/level.h"
#include <input.h>
#include <window.h>
#include <render/camera.h>
#include "manager.h"
#include "items.h"
#include "objects/pipe.h"
#include "objects/tank.h"
#include "objects/plant.h"
#include "objects/export.h"
#include <imgui/imgui.h>
#include <resources.h>

namespace game{

level *map;

render::camera2D gameCamera;

float money;
float distance;
int placeItem;
vec2f cameraCursor;

void init()
{
    money = 1000;

    readItems("data/items.xml");
    map = new level(vec2u(150));

    entityManager::init();
    distance = 20;
    gameCamera.screenSize = vec2f(-1, distance);
}

void free()
{
    entityManager::free();
    delete map;
}

void update()
{
    static vec2f oldCursor;
    static bool showSettings = true;
    withGui::showGui();

    if(ImGui::BeginMainMenuBar())
    {
        ImGui::Text("%.2f$", money);
        ImGui::SameLine();

        ImGui::PushID("pause");

        if(!entityManager::getGameSpeed())
        {
            ImGui::PushStyleColor(ImGuiCol_Button, color::uint2rgba(0xc0cbdc));
            if(ImGui::ImageButton(&resources::icons, ImVec2(16,16), ImVec2(0,0), ImVec2(0.25,1)))
                entityManager::setGameSpeed(0);
            ImGui::PopStyleColor(1);
        }else
            if(ImGui::ImageButton(&resources::icons, ImVec2(16,16), ImVec2(0,0), ImVec2(0.25,1)))
                entityManager::setGameSpeed(0);

        ImGui::PopID();
        ImGui::SameLine();

        ImGui::PushID("speed_1");

        if(entityManager::getGameSpeed() == 30000)
        {
            ImGui::PushStyleColor(ImGuiCol_Button, color::uint2rgba(0xb3e0a8));
            if(ImGui::ImageButton(&resources::icons, ImVec2(16,16), ImVec2(0.25,0), ImVec2(0.5,1)))
                entityManager::setGameSpeed(30000);
            ImGui::PopStyleColor(1);
        }else
            if(ImGui::ImageButton(&resources::icons, ImVec2(16,16), ImVec2(0.25,0), ImVec2(0.5,1)))
                entityManager::setGameSpeed(30000);

        ImGui::PopID();
        ImGui::SameLine();

        ImGui::PushID("speed_2");

        if(entityManager::getGameSpeed() == 15000)
        {
            ImGui::PushStyleColor(ImGuiCol_Button, color::uint2rgba(0xe8b796));
            if(ImGui::ImageButton(&resources::icons, ImVec2(16,16), ImVec2(0.5,0), ImVec2(0.75,1)))
                entityManager::setGameSpeed(15000);
            ImGui::PopStyleColor(1);
        }else
            if(ImGui::ImageButton(&resources::icons, ImVec2(16,16), ImVec2(0.5,0), ImVec2(0.75,1)))
                entityManager::setGameSpeed(15000);

        ImGui::PopID();
        ImGui::SameLine();

        ImGui::PushID("speed_3");

        if(entityManager::getGameSpeed() == 5000)
        {
            ImGui::PushStyleColor(ImGuiCol_Button, color::uint2rgba(0xf6757a));
            if(ImGui::ImageButton(&resources::icons, ImVec2(16,16), ImVec2(0.75,0), ImVec2(1,1)))
                entityManager::setGameSpeed(5000);
            ImGui::PopStyleColor(1);
        }else
            if(ImGui::ImageButton(&resources::icons, ImVec2(16,16), ImVec2(0.75,0), ImVec2(1,1)))
                entityManager::setGameSpeed(5000);


        ImGui::PopID();
        ImGui::SameLine();

        if(ImGui::BeginMenu(u8"Игра", false))
        {
            ImGui::MenuItem(u8"Сохранить");
            ImGui::MenuItem(u8"Загрузить");
            ImGui::EndMenu();
        }

        if(ImGui::BeginMenu(u8"Фабрика", false))
        {
            ImGui::MenuItem(u8"Статистика");
            ImGui::MenuItem(u8"Изменить");
            ImGui::EndMenu();
        }

        if(ImGui::BeginMenu(u8"Создать"))
        {
            if(ImGui::MenuItem(u8"Труба"))
                placeItem = 1;
            if(ImGui::MenuItem(u8"Цистерна"))
                placeItem = 2;
            if(ImGui::MenuItem(u8"Фабрика"))
                placeItem = 3;
            if(ImGui::MenuItem(u8"Экспорт"))
                placeItem = 4;
            ImGui::EndMenu();

            if(placeItem)
                input::setKey(0,0);
        }

        if(ImGui::BeginMenu(u8"Прочее", false))
        {
            if(ImGui::MenuItem(u8"Настройки"))
                showSettings = true;
            ImGui::EndMenu();
        }

        ImGui::Text("%f fps", window::fps);
    }

    ImGui::EndMainMenuBar();

    /*if(ImGui::Begin(u8"Настройки", &showSettings))
    {

    }
    ImGui::End();*/

    constexpr vec2u entitySizes[4] = {vec2u(1),vec2u(2),vec2u(3),vec2u(2)};

    if(!ImGui::IsAnyWindowHovered())
    {
        cameraCursor = gameCamera.screenToWorld(input::getCursor());

        if(input::getKey(2))
        {
            // дерьмецо

            vec2f diff = oldCursor-cameraCursor;
            gameCamera.position -= vec3f(diff);

            if(gameCamera.position.x > 10)
                gameCamera.position.x = 10;

            if(gameCamera.position.y > distance/2)
                gameCamera.position.y = distance/2;


            if(gameCamera.position.x < -150+distance)
                gameCamera.position.x = -150+distance;

            if(gameCamera.position.y < -150+distance/2)
                gameCamera.position.y = -150+distance/2;

            oldCursor = cameraCursor+diff;
        }else
            oldCursor = cameraCursor;

        if(input::getKey(1))
            placeItem = 0;

        if(placeItem)
        {
            if(map->selectCells(cameraCursor, entitySizes[placeItem-1]) && input::getKey(0))
            {
                switch(placeItem)
                {
                case 1:
                    new pipe(cameraCursor, map); break;
                case 2:
                    new tank(cameraCursor, map); break;
                case 3:
                    new plant(cameraCursor, map); break;
                case 4:
                    new exporter(cameraCursor, map); break;
                }
                if(placeItem != 1)
                    placeItem = 0;

                input::setKey(0,0);
            }
        }
        else
            map->selectCell(cameraCursor, input::getKey(0));

        if(input::getScroll())
        {
            distance -= input::getScroll();
            if(distance < 10)
                distance = 10;
            else if(distance > 30)
                distance = 30;
            gameCamera.screenSize = vec2f(-1, distance);
        }


    }
}

void draw()
{
    render::bindShader(0);
    gameCamera.bind();
    map->draw();
    if(placeItem)
    {
        resources::atlas.color = color::alpha(0.3);
        switch(placeItem)
        {
        case 1:
            resources::atlas.draw(vec2u(cameraCursor), vec2u(0)); break;
        case 2:
            resources::atlas.draw(vec2i(cameraCursor)-vec2i(0,1), vec2u(0,7),vec2u(2,3)); break;
        case 3:
            resources::atlas.draw(vec2i(cameraCursor), vec2u(0,4),vec2u(3)); break;
        case 4:
            resources::atlas.draw(vec2i(cameraCursor), vec2u(0,10),vec2u(2)); break;
        }
        resources::atlas.color = vec4f(1);
    }
    render::flush();
}

// лол

void getMoney(float m)
{
    money -= m;
}

void addMoney(float m)
{
    money += m;
}

}

