#include "plant.h"
#include "../items.h"
#include <cstdio>
#include <imgui/imgui.h>
#include <color.h>

namespace game{

void plant::select() noexcept
{
    _needShowGui = true;
    currentlyShown = this;
    //ImGui::SetWindowPos(u8"Фабрика", ImVec2(80,80));
}

void plant::_drawWindow()
{
    char buffer[64];
    if(ImGui::Begin(u8"Фабрика", &_needShowGui, ImVec2(384,180), -1, ImGuiWindowFlags_NoSavedSettings))
    {
        if(_receipt)
        {
            for(uint32_t i = 0; i < _input.size(); i++)
            {
                ImGui::Text(getItemById(_input[i].id)->name.c_str());
                ImGui::SameLine();

                sprintf(buffer, "%.2f/%.2f", _input[i].amount, _receipt->input[i].amount);

                if(_input[i].amount < _receipt->input[i].amount)
                    ImGui::PushStyleColor(ImGuiCol_PlotHistogram,color::uint2rgba(0x9e2835));

                ImGui::ProgressBar(_input[i].amount/(_receipt->input[i].amount*5), ImVec2(-1,0), buffer);

                if(_input[i].amount < _receipt->input[i].amount)
                    ImGui::PopStyleColor(1);
            }

            ImGui::ProgressBar(_receiptTicks/float(_receipt->ticks), ImVec2(-1,4), " ");

            for(uint32_t i = 0; i < _output.size(); i++)
            {
                ImGui::Text(getItemById(_output[i].id)->name.c_str());
                ImGui::SameLine();
                ImGui::ProgressBar(_output[i].amount/(_receipt->output[i].amount*40));
            }

            if(ImGui::Button(u8"Выбрать рецепт"))
                setReceipt(0);

            if(ImGui::CollapsingHeader(u8"Статистика"))
            {
                float stats[3] = {0,0,0};
                for(int i = 0; i < 0xFFFF; i++)
                {
                    if(_stat[i] == -1) continue;
                    stats[_stat[i]]++;
                }

                float m = stats[0]+stats[1]+stats[2];

                ImGui::Text(u8"Работа:");ImGui::SameLine();
                ImGui::ProgressBar(stats[0]/m);
                ImGui::Text(u8"Переполнение ресурсами:");ImGui::SameLine();
                ImGui::ProgressBar(stats[1]/m);
                ImGui::Text(u8"Ожидание ресурсов:");ImGui::SameLine();
                ImGui::ProgressBar(stats[2]/m);

            }

        }else
        {
            int currentReceipt = 0;
            if(ImGui::ListBox(u8"Выбрать рецепт", &currentReceipt, getReceiptNames(), receipts.size()))
                setReceipt(&receipts[currentReceipt]);
        }

        //ImGui::ProgressBar(getFilledSpace());
        ImGui::NewLine();
        ImGui::Separator();
        ImGui::NewLine();
        ImGui::PushStyleColor(ImGuiCol_Button, color::uint2rgba(0xe43b44));
        ImGui::PushStyleColor(ImGuiCol_ButtonHovered, color::uint2rgba(0xe43b44));
        ImGui::PushStyleColor(ImGuiCol_ButtonActive, color::uint2rgba(0xe43b44));
        if(ImGui::Button(u8"Сломать"))
            onCellDestroyed();
        ImGui::PopStyleColor(3);
    }
    else
    {
        if(!ImGui::IsWindowCollapsed())
            _needShowGui = false;
    }

    ImGui::End();
}

void plant::_drawShortInfo()
{
    char buffer[64];
    if(ImGui::Begin(u8"Фабрика ", 0, ImVec2(160,0), -1, ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_NoCollapse))
    {
        ImGui::SetWindowSize(ImVec2(160,0), ImGuiCond_Always);
        ImGui::SetWindowPos(ImVec2(0,20), ImGuiCond_Always);
        if(_receipt)
        {
            for(uint32_t i = 0; i < _input.size(); i++)
            {
                ImGui::Text(getItemById(_input[i].id)->name.c_str());

                sprintf(buffer, "%.2f/%.2f", _input[i].amount, _receipt->input[i].amount);

                if(_input[i].amount < _receipt->input[i].amount)
                    ImGui::PushStyleColor(ImGuiCol_PlotHistogram,color::uint2rgba(0x9e2835));

                ImGui::ProgressBar(_input[i].amount/(_receipt->input[i].amount*5), ImVec2(-1,0), buffer);

                if(_input[i].amount < _receipt->input[i].amount)
                    ImGui::PopStyleColor(1);
            }

            ImGui::ProgressBar(_receiptTicks/float(_receipt->ticks), ImVec2(-1,4), " ");

            for(uint32_t i = 0; i < _output.size(); i++)
            {
                ImGui::Text(getItemById(_output[i].id)->name.c_str());
                ImGui::ProgressBar(_output[i].amount/(_receipt->output[i].amount*40));
            }
        }else
            ImGui::Text(u8"Рецепт не выбран");
    }
    ImGui::End();
}

}
