package TestingSystem.SAPFOR.SapforTask;
import Common.Constants;
import Common.Current;
import Common.Database.DBObject;
import Common.Utils.Utils;
import GlobalData.Tasks.TaskState;
import TestingSystem.SAPFOR.Json.SapforVersionState;
import TestingSystem.SAPFOR.Json.SapforVersion_json;
import TestingSystem.SAPFOR.SapforTasksPackage.UI.VersionSummary;
import Visual_DVM_2021.Passes.PassCode_2021;
import com.google.gson.annotations.Expose;
import com.sun.org.glassfish.gmbal.Description;

import javax.swing.tree.DefaultMutableTreeNode;
import java.io.File;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Vector;
public class SapforTask extends DBObject {
    //------------------------------------>>
    @Description("PRIMARY KEY, UNIQUE")
    @Expose
    public long id = Constants.Nan;
    @Description("DEFAULT '-1'")
    @Expose
    public int sapfor_configuration_id = Constants.Nan;
    @Expose
    @Description("DEFAULT '-1'")
    public long sapfortaskspackage_id = Constants.Nan;
    //------------------------------------->>
    @Description("DEFAULT ''")
    @Expose
    public String test_description = "";
    @Description("DEFAULT ''")
    @Expose
    public String group_description = "";
    @Description("DEFAULT ''")
    @Expose
    public String flags = "";
    @Description("DEFAULT ''")
    @Expose
    public String codes = "";
    @Expose
    public TaskState state = TaskState.Inactive;
    @Expose
    public long StartDate = 0; //дата начала выполнения
    @Expose
    public long ChangeDate = 0;//дата окончания выполнения
    //в json не выносить. это только для БД.
    @Description("DEFAULT ''")
    public String versionsDescription = "";
    //------
    @Description("IGNORE")
    @Expose
    public Vector<SapforVersion_json> versions = new Vector<>();
    //----------
    @Description("IGNORE")
    @Expose
    public Vector<SapforVersion_json> variants = new Vector<>();
    //----------
    @Description("IGNORE")
    public MatchState match = MatchState.Unknown;
    //-----------
    public String getUniqueKey() {
        return sapfor_configuration_id + "_" + group_description + "_" + test_description;
    }
    public String getSummary() {
        Vector<String> lines = new Vector<>();
        lines.add(group_description);
        lines.add(test_description);
        lines.add(codes);
        lines.add(flags);
        return String.join(" ", lines);
    }
    //-----------
    public SapforTask() {
    }
    public DefaultMutableTreeNode getVersionsTree(File configurationRoot) {
        VersionSummary root = null;
        VersionSummary child = null;
        VersionSummary parent = null;
        //--
        for (SapforVersion_json version_json : versions) {
            version_json.task = this;
            //-
            child = new VersionSummary(version_json, version_json.init(configurationRoot));
            if (parent == null) {
                root = child;
                parent = child;
            } else {
                if (!child.state.equals(SapforVersionState.Empty)) {
                    parent.add(child);
                    parent = child;
                }
            }
            //-
        }
        if (parent != null) {
            for (SapforVersion_json version_json : variants) {
                version_json.task = this;
                if (!child.state.equals(SapforVersionState.Empty)) {
                    parent.add(new VersionSummary(version_json, version_json.init(configurationRoot)));
                }
            }
        }
        //--
        return root;
    }
    public void Reset() {
        state = TaskState.Inactive;
        versions.clear();
        variants.clear();
    }
    public SapforTask(SapforTask src) {
        this.SynchronizeFields(src);
    }
    @Override
    public Object getPK() {
        return id;
    }
    @Override
    public void SynchronizeFields(DBObject object) {
        super.SynchronizeFields(object);
        SapforTask t = (SapforTask) object;
        id = t.id;
        sapfor_configuration_id = t.sapfor_configuration_id;
        sapfortaskspackage_id = t.sapfortaskspackage_id;
        //-
        test_description = t.test_description;
        group_description = t.group_description;
        versionsDescription = t.versionsDescription;
        //--
        codes = t.codes;
        state = t.state;
        //--
    }
    public String getVersionsChain() {
        Vector<String> versionsLines = new Vector<>();
        for (int i = 1; i < versions.size(); ++i) {
            versionsLines.add(Utils.Brackets(versions.get(i).description));
        }
        if (!variants.isEmpty()) {
            versionsLines.add(Utils.Brackets(PassCode_2021.CreateParallelVariants.getDescription()));
        }
        return String.join("→", versionsLines);
    }
    /*
    @Override
    public boolean isVisible() {
        return Current.HasSapforTasksPackage() && Current.getSapforTasksPackage().id == this.sapfortaskspackage_id;
    }
     */
    public LinkedHashMap<String, SapforVersion_json> getSortedVersions() {
        LinkedHashMap<String, SapforVersion_json> res = new LinkedHashMap<>();
        for (SapforVersion_json version_json : versions)
            res.put(version_json.version, version_json);
        //--
        for (SapforVersion_json version_json : variants)
            res.put(version_json.version, version_json);
        return res;
    }
    public void checkMatch(SapforTask task2) {
        if (!state.equals(task2.state)) {
            System.out.println("Не совпадение цепочки версий в задаче " + getUniqueKey());
        } else if (versions.size() != task2.versions.size()) {
            System.out.println("Не совпадение длины цепочки версий в задаче " + getUniqueKey());
        } else if (variants.size() != task2.variants.size()) {
            System.out.println("Не совпадение длины цепочки вариантов в задаче " + getUniqueKey());
        } else {
            LinkedHashMap<String, SapforVersion_json> versions1 = getSortedVersions();
            LinkedHashMap<String, SapforVersion_json> versions2 = task2.getSortedVersions();
            //---
            for (String name1 : versions1.keySet()) {
                if (!versions2.containsKey(name1)) {
                    System.out.println("Не совпадение имен версий в задаче " + getUniqueKey());
                    return;
                }
            }
            System.out.println("сравнение версий.");
            //--
            for (String name1 : versions1.keySet()) {
                System.out.println("version name=" + name1);
                SapforVersion_json version1 = versions1.get(name1);
                SapforVersion_json version2 = versions2.get(name1);
                //---
                if (!version1.isMatch(version2)) {
                    System.out.println("Не совпадение версий в задаче " + getUniqueKey());
                    return;
                }
            }
            match = MatchState.Match;
            task2.match = MatchState.Match;
        }
    }
    public Date getStartDate() {
        return new Date(StartDate);
    }
    public Date getChangeDate() {
        return new Date(ChangeDate);
    }
}
