package _VisualDVM.Passes.All;
import Common.Utils.Utils_;
import _VisualDVM.ComponentsServer.Component.Component;
import _VisualDVM.ComponentsServer.Component.ComponentType;
import _VisualDVM.ComponentsServer.Component.Json.ComponentPublicationInfoJson;
import _VisualDVM.ComponentsServer.Component.UI.PublishForm;
import _VisualDVM.ComponentsServer.UserAccount.AccountRole;
import _VisualDVM.Constants;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.Passes.Server.ComponentsServerPass;
import _VisualDVM.Repository.Server.ServerCode;

import java.util.Date;
public class PublishComponent extends ComponentsServerPass<Component> {
    String version_mail_header = "";
    String version_text = "";
    PublishForm f = new PublishForm();
    @Override
    public String getIconPath() {
        return "/Common/icons/Publish.png";
    }
    @Override
    protected boolean isSafe() {
        return false;
    }
    @Override
    protected boolean canStart(Object... args) throws Exception {
        if (!Utils_.isWindows()) {
            Log.Writeln_("Публикация компонент разрешена только для Windows");
            return false;
        }
        if (!Global.mainModule.getAccount().role.equals(AccountRole.Admin)) {
            Log.Writeln_("Вы не являетесь администратором");
        }
        ;
        if (Global.components.getUI().CheckCurrent(Log)) {
            target = Global.components.getUI().getCurrent();
            f.fields.cbUpdateMinimalVersion.setSelected(false);
            f.fields.lMinimalVersion.setText(String.valueOf(target.minimal_version));
            f.fields.lPublishingVersion.setText(String.valueOf(target.version));
            if (!target.isValidVersion(Log, "публикуемого")) {
                return false;
            }
            if (target.getComponentType().equals(ComponentType.Sapfor_F)) {
                f.fields.cbAssemblyOnServer.setVisible(true);
                f.fields.cbAssemblyOnServer.setSelected(false);
            } else {
                f.fields.cbAssemblyOnServer.setVisible(false);
                f.fields.cbAssemblyOnServer.setSelected(false);
            }
            if (f.ShowDialog(getDescription())) {
                String change_description = (f.Result == null) ? "не указаны" : f.Result;
                String change_record_header = String.join(" ",
                        Utils_.Brackets(Utils_.print_date(new Date())) + ":",
                        Global.mainModule.getAccount().name + Utils_.RBrackets(Global.mainModule.getAccount().email),
                        "публикует версию", Utils_.DQuotes(target.version)
                );
                String change_record = String.join("\n", change_record_header, "Изменения:",
                        change_description,
                        Constants.separator, ""
                );
                //--
                ComponentPublicationInfoJson info = new ComponentPublicationInfoJson(target);
                info.changeRecord = change_record;
                info.needsUpdateMinimalVersion = f.fields.cbUpdateMinimalVersion.isSelected();
                info.needsEmail = f.fields.cbNeedsBroadcast.isSelected();
                info.needsSendFile = f.fields.cbForceMail.isSelected();
                //--
                return SendRequest(ServerCode.UpdateComponent, "", info);
            }
        }
        return false;
    }
    @Override
    protected void performDone() throws Exception {
        target.actual_version = target.version;
        target.CheckIfNeedsUpdateOrPublish();
    }
    @Override
    protected void showDone() throws Exception {
        Global.components.refreshUpdatesStatus();
        if (target.getComponentType().equals(ComponentType.Sapfor_F) && f.fields.cbAssemblyOnServer.isSelected()) {
            Global.mainModule.getUI().getMainWindow().FocusTesting();
            Global.mainModule.getUI().getMainWindow().getTestingWindow().FocusSapforTesting();
            Global.mainModule.getPass(PassCode.CompileServerSapfor).Do();
        }
    }
}

