module gui.manager;

import app;

import cimgui.cimgui;

import ecs.std;
import ecs.system;
import ecs.vector;
import ecs.entity;

import gui.system;
import gui.template_;

extern(C):

struct GUIManager
{
    Vector!SystemGUI systems;
    Vector!TemplateGUI templates;

    uint selected_tempalte = 0;
    
    void clear()
    {
        foreach(tmpl; templates)
        {
            launcher.manager.freeTemplate(tmpl.tmpl);
        }

        systems.clear();
        templates.clear();
    }

    EntityTemplate* getSelectedTemplate()
    {
        if(templates.length > selected_tempalte)return templates[selected_tempalte].tmpl;
        else return null;
    }

    void addSystem(ushort id, const (char)* name, bool enabled = true)
    {
        System* system = launcher.manager.getSystem(id);
        //const (char)* name = 
        systems.add(SystemGUI(name,system,enabled));
    }

    void addTemplate(ushort[] components, const (char)* name)
    {
        templates.add(TemplateGUI(name, launcher.manager.allocateTemplate(components)));
    }

    void addTemplate(EntityTemplate* tmpl, const (char)* name)
    {
        templates.add(TemplateGUI(name, tmpl));
    }

    void gui()
    {
        if(igCollapsingHeader("Systems", ImGuiTreeNodeFlags_Framed | ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_FramePadding))
        {
            bool true_ = true;
            igIndent(8);
            foreach(ref SystemGUI system;systems)
            {
                if(igCheckbox(system.name,&system.enabled))
                {
                    if(system.enabled)system.system.enable();
                    else system.system.disable();
                }
            }
            igUnindent(8);
        }
    }

    void toolGui()
    {
        if(templates.length)
        {
            if(igBeginCombo("Template",templates[selected_tempalte].name,0))
            {
                foreach(i, tmpl; templates)
                {
                    if(igSelectable(tmpl.name,false,0,ImVec2(0,0)))
                    {
                        selected_tempalte = cast(uint)i;
                    }
                }
                igEndCombo();
            }
        }
        else 
        {
            if(igBeginCombo("Template",null,0))igEndCombo();
        }
    }
}