package _VisualDVM.Passes.All;
import _VisualDVM.ComponentsServer.BugReport.BugReport;
import _VisualDVM.ComponentsServer.BugReportRecipient.BugReportRecipient;
import _VisualDVM.ComponentsServer.ComponentsServer;
import _VisualDVM.ComponentsServer.Recipient.Recipient;
import _VisualDVM.Global;
import _VisualDVM.Passes.Server.EditServerObject;

import java.util.Date;
import java.util.Vector;
public class SaveBugReportRecipients extends EditServerObject<ComponentsServer, BugReport> {
    public SaveBugReportRecipients() {
        super(Global.componentsServer, BugReport.class);
    }
    @Override
    public String getIconPath() {
        return "/Common/icons/Save.png";
    }
    @Override
    protected boolean canStart(Object... args) throws Exception {
        if (server.db.bugReports.getUI().CheckCurrent(Log)) {
            target = server.db.bugReports.getUI().getCurrent();
            target.recipients = new Vector<>();
            for (Recipient recipient : server.db.recipients.getUI().getSelectedItems()) {
                target.recipients.add(new BugReportRecipient(recipient.email));
            }
            switch (target.state) {
                case draft:
                    break;
                default:
                    for (BugReportRecipient recipient : target.recipients)
                        recipient.bugreport_id = target.id;
                    target.change_date = new Date().getTime();
                    return true;
            }
        }
        return false;
    }
}
