package _VisualDVM.Passes.All;
import Common.Utils.Utils_;
import _VisualDVM.ComponentsServer.BugReport.BugReport;
import _VisualDVM.ComponentsServer.BugReport.Json.BugReportAdditionJson;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.Passes.Server.ComponentsServerPass;
import _VisualDVM.Repository.EmailMessage;
import _VisualDVM.Repository.Server.ServerCode;

import java.util.Date;
public class AppendBugReportField extends ComponentsServerPass<BugReport> {
    String fieldName;
    String oldValue;
    String addition;
    String newValue;
    BugReport actual;
    @Override
    public String getIconPath() {
        return "/icons/Append.png";
    }
    @Override
    protected boolean canStart(Object... args) throws Exception {
        actual = null;
        if (getServer().db.getTable(BugReport.class).getUI().CheckCurrent(Log)) {
            target = getServer().db.getTable(BugReport.class).getUI().getCurrent();
            if (!target.CheckNotDraft(Log))
                return false;
            fieldName = (String) args[0];
            addition = (String) args[1];
            if (addition.isEmpty()) {
                Log.Writeln_("Дополнение не может быть пустым.");
                return false;
            }
            addition = Utils_.Brackets(Utils_.print_date(
                    new Date())) + " " + Global.mainModule.getAccount().name
                    + " : " + addition;
            if (canUpdate() && (SendRequest(ServerCode.AppendBugReportTextField, "", new BugReportAdditionJson(target, fieldName, addition)))) {
                actual = (BugReport) request.target;
                return true;
            }
        }
        return false;
    }
    protected boolean canUpdate() {
        return target.canModify(Global.mainModule.getAccount(), Log);
    }
    @Override
    protected void body() throws Exception {
        target.SynchronizeFields(actual);
        Global.componentsServer.db.Update(target);
    }
    @Override
    protected void showFinish() throws Exception {
        Global.componentsServer.db.bugReports.getUI().RedrawControl();
        Global.mainModule.getUI().getMainWindow().getCallbackWindow().ShowCurrentBugReport();
    }
    @Override
    protected void performDone() throws Exception {
        String message_header = target.getMailTitlePrefix();
        String message_text = "";
        switch (fieldName) {
            default:
                return;
            case "description":
                message_header += "описание дополнено";
                message_text = target.description
                ;
                break;
            case "comment":
                message_header += "комментарий дополнен";
                message_text = target.comment;
                break;
        }
        EmailMessage message = new EmailMessage(
                message_header + " " + Utils_.Brackets(Global.mainModule.getAccount().name),
                message_text
        );

        Global.mainModule.getPass(PassCode.Email).Do(
                message,
                Global.componentsServer.db.recipients.getSelectedMails());
    }
}
