package Visual_DVM_2021.UI.Main;
import Common.Current;
import Common.Global;
import Common.Utils.Utils;
import GlobalData.Settings.SettingName;
import ProjectData.Files.ProjectFile;
import SapforTestingSystem.Json.SapforVersion_json;

import javax.swing.*;
import java.util.Vector;
public class SapforVersionsComparisonForm extends ComparisonForm<SapforVersion_json> {
    //почти полный клон VersionsComparsionForm. В будущем нужен рефакторинг. Наверное.
    Current current;
    private final JComboBox<ProjectFile> cbFile;
    protected ProjectFile file = null;
    private SapforVersionsComparisonForm getMaster() {
        return (SapforVersionsComparisonForm) master;
    }
    private SapforVersionsComparisonForm getSlave() {
        return (SapforVersionsComparisonForm) slave;
    }
    public SapforVersionsComparisonForm(SapforVersionsComparisonForm slave_in, Current current_in) {
        super(SapforVersion_json.class, slave_in);
        current = current_in;
        bApplyObject.setVisible(false);
        bClose.setVisible(false);
        cbFile = new JComboBox<>();
        tools.add(cbFile, 3);
        //--
        cbFile.addActionListener(e -> {
            file = (cbFile.getSelectedItem() instanceof ProjectFile) ?
                    ((ProjectFile) cbFile.getSelectedItem()) : null;
            //-->>
            if (isMaster()) {
                if (isReady() && !getSlave().selectSameFile(file))
                    DoShowPass(true);
            } else {
                if (isReady()) {
                    if (getMaster().isReady()) {
                        boolean ExtensionsOn = Global.db.settings.get(SettingName.ExtensionsOn).toBoolean();
                        String name1 = ExtensionsOn ? getMaster().file.file.getName() : Utils.getFileNameWithoutExtension(getMaster().file.file);
                        String name2 = ExtensionsOn ? file.file.getName() : Utils.getFileNameWithoutExtension(file.file);
                        if (Global.db.settings.get(SettingName.ComparsionDiffMergeOn).toBoolean()) {
                            if (name1.equalsIgnoreCase(name2))
                                master.DoComparePass(true);
                        } else
                            master.DoShowPass(true);
                    } else {
                        master.DoShowPass(true);
                    }
                }
            }
        });
    }
    @Override
    protected Current getCurrentObjectName() {
        return current;
    }
    @Override
    protected String getText() {
        return isReady() ? Utils.ReadAllText(file.file) : "объект не назначен";
    }
    @Override
    public boolean isReady() {
        return super.isReady() && file != null;
    }
    @Override
    protected void showObject() {
        lObjectName.setText(object.toString());
        lObjectName.setToolTipText(object.toString());
        cbFile.removeAllItems();
        Vector<ProjectFile> files = object.files;
        for (ProjectFile file : files)
            cbFile.addItem(file);
    }
    @Override
    protected void removeObject() {
        cbFile.removeAllItems();
        file = null;
    }
    public boolean selectSameFile(ProjectFile file_in) {
        file = null;
        cbFile.setSelectedIndex(-1);
        for (int i = 0; i < cbFile.getItemCount(); ++i) {
            ProjectFile projectFile = cbFile.getItemAt(i);
            if (Global.db.settings.get(SettingName.ExtensionsOn).toBoolean()) {
                //если учитываем расширения, ищем полное совпадение
                if (projectFile.file.getName().equals(file_in.file.getName())) {
                    cbFile.setSelectedIndex(i);
                    return true;
                }
            } else {
                if (Utils.getNameWithoutExtension(projectFile.file.getName()).equals(
                        Utils.getNameWithoutExtension(file_in.file.getName()))) {
                    cbFile.setSelectedIndex(i);
                    return true;
                }
            }
        }
        return (cbFile.getSelectedItem() != null) && (cbFile.getSelectedItem() instanceof ProjectFile);
    }
}
