package _VisualDVM.ComponentsServer.Component;
import Common.Database.Tables.DataSet;
import Common.MainModule_;
import Common.Visual.DataSetControlForm;
import _VisualDVM.ComponentsServer.Component.Sapfor.Sapfor_F;
import _VisualDVM.ComponentsServer.Component.UI.ComponentsForm;
import _VisualDVM.Current;
import _VisualDVM.Global;

import javax.swing.*;
public class ComponentsSet extends DataSet<ComponentType, Component> {
    public boolean bad_state = false;
    public boolean need_update = false;
    public boolean need_publish = false;
    public ComponentsSet() {
        super(ComponentType.class, Component.class);
        put(ComponentType.Visualiser, Global.visualiser = new Visualiser());
        put(ComponentType.Sapfor_F, (Component) MainModule_.instance.set(Current.Sapfor, new Sapfor_F()));
        put(ComponentType.Visualizer_2, Global.visualizer_2);
        //   put(ComponentType.PerformanceAnalyzer, Global.performanceAnalyzer = new PerformanceAnalyzer());
        put(ComponentType.Instruction, new Instruction());
    }
    public boolean needChanges() {
        return bad_state || need_update;
    }
    public String getButtonIconPath() {
        return needChanges() ? "/icons/ComponentsNeedUpdate.gif"
                : (need_publish ? "/icons/ComponentsNeedPublish_2023.gif" : "/icons/ComponentsActual.png");
    }
    @Override
    public String getSingleDescription() {
        return "компонент";
    }
    @Override
    public String getPluralDescription() {
        return "компоненты";
    }
    @Override
    protected DataSetControlForm createUI(JPanel mountPanel) {
        return new ComponentsForm(this, mountPanel);
    }
    //--
    public void refreshUpdatesStatus() {
        if (getUI() != null)
            getUI().RedrawControl();
        validateStates();
        if (Global.mainModule.getUI().hasMainWindow())
            Global.mainModule.getUI().getMainWindow().ShowUpdatesIcon();
    }
    public boolean validateStates() {
        bad_state = need_update = need_publish = false;
        for (Component component : Data.values()) {
            switch (component.getState()) {
                case Not_found:
                case Unknown_version:
                case Old_version:
                    if (component.isNecessary())
                        bad_state = true;
                    component.Select(true);
                    break;
                case Needs_update:
                    need_update = true;
                    component.Select(true);
                    break;
                case Needs_publish:
                    need_publish = true;
                    break;
                default:
                    component.Select(false);
                    break;
            }
            //    }
        }
        return !bad_state;
    }
    public void initialVersionsCheck() {
        for (Component component : Data.values()) {
            System.out.println(component.getComponentType() + " initial version checking...");
            component.InitialVersionCheck();
            System.out.println("done");
        }
    }
}
