package _VisualDVM.ComponentsServer;
import Common.Database.SQLITE.SQLiteDatabase;
import _VisualDVM.ComponentsServer.BugReport.BugReport;
import _VisualDVM.ComponentsServer.BugReport.BugReportsDBTable;
import _VisualDVM.ComponentsServer.BugReportFile.BugReportFilesDBTable;
import _VisualDVM.ComponentsServer.BugReportRecipient.BugReportRecipient;
import _VisualDVM.ComponentsServer.BugReportRecipient.BugReportRecipientsDBTable;
import _VisualDVM.ComponentsServer.BugReportSetting.BugReportSettingsDBTable;
import _VisualDVM.ComponentsServer.Recipient.RecipientsDataSet;
import _VisualDVM.Passes.PassCode;

import java.nio.file.Paths;
import java.util.Vector;
public class BugReportsDatabase extends SQLiteDatabase {
    public BugReportsDBTable bugReports;
    public BugReportSettingsDBTable bugReportSettings;
    public BugReportRecipientsDBTable bugReportRecipients;
    public BugReportFilesDBTable bugReportsFiles;
    public RecipientsDataSet recipients = new RecipientsDataSet();
    public BugReportsDatabase() {
        super(Paths.get(System.getProperty("user.dir"), "Data", "bug_reports.sqlite").toFile());
    }
    @Override
    protected void initAllTables() throws Exception {
        addTable(bugReports = new BugReportsDBTable());
        addTable(bugReportSettings = new BugReportSettingsDBTable());
        addTable(bugReportRecipients = new BugReportRecipientsDBTable());
        addTable(bugReportsFiles = new BugReportFilesDBTable());
    }
    @Override
    public void Init() throws Exception {
        DeleteDrafts();
        //--
        Patch();
    }
    @Override
    public PassCode getSynchronizePassCode() {
        return PassCode.SynchronizeBugReports;
    }
    public void DeleteDrafts() throws Exception {
        Vector<BugReport> drafts = bugReports.getAllDrafts();
        for (BugReport draft : drafts)
            Delete(draft);
    }
    @Override
    public void DropUI() {
        super.DropUI();
        bugReports.ClearUI();
        recipients.ClearUI();
    }
    @Override
    public void ResetUI() {
        bugReports.ShowUI();
        recipients.ShowUI(); //todo временно.
        super.ResetUI();
    }
    public void saveBugreportRecipients(BugReport bugReport) throws Exception {
        if (bugReport.recipients != null) {
            for (BugReportRecipient recipient : bugReport.recipients) {
                recipient.bugreport_id = bugReport.id;
                Insert(recipient);
            }
        }
    }
    public void Patch() throws Exception {
        /*
        int i = 0;
        Vector<BugReport> sortedBugs = new Vector<>(bugReports.Data.values());
        sortedBugs.sort(new Comparator<BugReport>() {
            @Override
            public int compare(BugReport o1, BugReport o2) {
                return Long.compare(o1.date,o2.date);
            }
        });
        //--
        for (BugReport bugReport: sortedBugs){
            bugReport.id_ = i;
            ++i;
            //--
            Update(bugReport);
        }
        //--
        for (BugReport bugReport: sortedBugs){
            Vector<BugReportSetting> bugSettings = getVectorByFK(bugReport, BugReportSetting.class);
            Vector<BugReportRecipient> bugReportRecipients = getVectorByFK(bugReport, BugReportRecipient.class);
            for (BugReportSetting setting: bugSettings){
                setting.bugreport_id_ = bugReport.id_;
                Update(setting);
            }
            for (BugReportRecipient recipient: bugReportRecipients){
                recipient.bugreport_id_ = bugReport.id_;
                Update(recipient);
            }
        }
        //перенос архивов
        for (BugReport bugReport: sortedBugs){
            System.out.println("bugreport_id="+bugReport.id+"/"+bugReport.id_);
            File new_home = new File(Global.BugReportsDirectory,String.valueOf(bugReport.id_));
            //--
            if (!new_home.exists())
                FileUtils.forceMkdir(new_home);
            //--
            Utils.CleanDirectory(new_home);
            //--
            File old_archive = new File(Global.BugReportsDirectory_OLD, bugReport.id);
            File new_archive = new File(new_home,String.valueOf(bugReport.id_)+".zip");
            if (old_archive.exists()) {
                FileUtils.copyFile(old_archive, new_archive);
            }
        }
        //--
        //упоминания других багов.
        for (BugReport bugReport: sortedBugs){
            //--
            Vector<String> mentioned_keys = new Vector<>();
            for (String id: bugReports.Data.keySet()){
                if ((bugReport.description.contains(id) || bugReport.comment.contains(id)) && !mentioned_keys.contains(id)){
                    mentioned_keys.add(id);
                }
            }
            //--
            for (String id: mentioned_keys){
                BugReport owner = bugReports.get(id);
                if (owner!=null) {
                    bugReport.comment = bugReport.comment.replace(id, String.valueOf(owner.id_));
                    bugReport.description = bugReport.description.replace(id, String.valueOf(owner.id_));
                    Update(bugReport);
                }
            }

        }
        */
    }
}
