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

namespace game{

void pipe::select() noexcept
{
    _needShowGui = true;
    currentlyShown = this;
    //ImGui::SetWindowPos(u8"Труба", ImVec2(80,80));
}

void pipe::_drawWindow()
{
    ImGui::SetNextWindowSizeConstraints(ImVec2(256,160), ImVec2(256,800));
    if(ImGui::Begin(u8"Труба", &_needShowGui, ImVec2(256,160)))
    {
        if(_item != 0xFFFFFFFF)
        {
            ImGui::Text(getItemById(_item)->name.c_str());
            ImGui::SameLine();
        }

        ImGui::ProgressBar(getFilledSpace());

        if(ImGui::CollapsingHeader(u8"Фильтр"))
        {
            int oldLock = _lockedItem;
            ImGui::RadioButton(u8"Пусто", &_lockedItem, -1);
            for(int i = 0; i < itemsCount(); i++)
                ImGui::RadioButton(getItemById(i)->name.c_str(), &_lockedItem, i);

            if(oldLock != _lockedItem)
                _mathMask();
        }

        ImGui::Separator();

        ImGui::Text(u8"Подземные коммуникации:");

        const char *directions[4] = {u8"Вправо", u8"Вниз", u8"Влево", u8"Вверх"};

        for(int i = 0; i < 4; i++)
        {
            if(_undergroundConnections[i])
            {
                ImGui::PushStyleColor(ImGuiCol_Button, color::uint2rgba(0x0095e9));

                if(ImGui::Button(directions[i]))
                {
                    _undergroundConnections[i]->_removeUndergroundPipe(this,i,true);
                    _removeUndergroundPipe(_undergroundConnections[i],i,false);
                }
                ImGui::PopStyleColor(1);
            }else
                if(ImGui::Button(directions[i]))
                    if(pipe *p = _findPipe(i))
                        if(p->_addUndergroundPipe(this,i,true))
                            _addUndergroundPipe(p,i,false);

            ImGui::SameLine();
        }
        ImGui::NewLine();
        ImGui::Separator();

        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::SameLine();
        if(ImGui::Button(u8"Очистить"))
            _amount = 0;

        ImGui::PopStyleColor(3);
    }
    else
    {
        if(!ImGui::IsWindowCollapsed())
            _needShowGui = false;
    }

    ImGui::End();
}

void pipe::_drawShortInfo()
{
    if(ImGui::Begin(u8"Труба ", 0, ImVec2(160,80), -1, ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_NoCollapse))
    {
        ImGui::SetWindowSize(ImVec2(160,0), ImGuiCond_Always);
        ImGui::SetWindowPos(ImVec2(0,20), ImGuiCond_Always);
        if(_item != 0xFFFFFFFF)
            ImGui::Text(getItemById(_item)->name.c_str());

        ImGui::ProgressBar(getFilledSpace());
    }
    ImGui::End();
}

}
