package _VisualDVM.Passes.All;
import _VisualDVM.ComponentsServer.Component.Component;
import _VisualDVM.ComponentsServer.Component.ComponentType;
import _VisualDVM.ComponentsServer.Component.Json.ComponentVersionsInfoJson;
import _VisualDVM.Global;
import _VisualDVM.Passes.Server.ComponentsServerPass;
import _VisualDVM.Repository.Server.ServerCode;

import java.util.Vector;
public class GetComponentsActualVersions extends ComponentsServerPass<Object> {
    Vector<ComponentVersionsInfoJson> versions;
    @Override
    public String getIconPath() {
        return "/icons/Components.png";
    }
    @Override
    protected boolean isSafe() {
        return false;
    }
    @Override
    protected boolean canStart(Object... args) throws Exception {
        versions = null;
        Vector<String> types = new Vector<>();
        for (ComponentType key : Global.components.Data.keySet())
            types.add(key.toString());
        if (SendRequest(ServerCode.GetComponentsVersionsInfo, "", types)) {
            versions = (Vector<ComponentVersionsInfoJson>) request.target;
            return true;
        }
        return false;
    }
    @Override
    protected void body() throws Exception {
        for (ComponentVersionsInfoJson info : versions) {
            Global.components.get(info.componentType).unpackActualVersion(info.actual_version);
            Global.components.get(info.componentType).unpackMinimalVersion(info.minimal_version);
        }
        for (Component component : Global.components.Data.values()) {
            if (component.CanBeUpdated())
                component.CheckIfNeedsUpdateOrPublish();
        }
    }
}
