package _VisualDVM.Visual.Windows;
import Common.Utils.Utils_;
import _VisualDVM.Global;
import _VisualDVM.ProjectData.Files.DBProjectFile;
import _VisualDVM.ProjectData.Files.ProjectFile;
import _VisualDVM.ProjectData.Project.db_project_info;
import _VisualDVM.Utils;

import javax.swing.*;
import java.util.Vector;
public class VersionsComparisonForm extends ComparisonForm<db_project_info> {
    private final JComboBox<DBProjectFile> cbFile;
    protected DBProjectFile file = null;
    public VersionsComparisonForm(VersionsComparisonForm slave_in) {
        super(db_project_info.class, slave_in);
        cbFile = new JComboBox<>();
        tools.add(cbFile, 3);
        file = (cbFile.getSelectedItem() instanceof DBProjectFile) ?
                ((DBProjectFile) cbFile.getSelectedItem()) : null;
        cbFile.addActionListener(e -> {
            file = (cbFile.getSelectedItem() instanceof DBProjectFile) ?
                    ((DBProjectFile) cbFile.getSelectedItem()) : null;
            //-->>
            if (isMaster()) {
                if (isReady() && !getSlave().selectSameFile(file))
                    DoShowPass(true);
            } else {
                if (isReady()) {
                    if (getMaster().isReady()) {
                        boolean ExtensionsOn = (Global.normalProperties.ExtensionsOn);
                        String name1 = ExtensionsOn ? getMaster().file.file.getName() : Utils_.getFileNameWithoutExtension(getMaster().file.file);
                        String name2 = ExtensionsOn ? file.file.getName() : Utils_.getFileNameWithoutExtension(file.file);
                        if (Global.normalProperties.ComparsionDiffMergeOn) {
                            if (name1.equalsIgnoreCase(name2))
                                master.DoComparePass(true);
                        } else
                            master.DoShowPass(true);
                    } else {
                        master.DoShowPass(true);
                    }
                }
            }
        });
    }
    private VersionsComparisonForm getMaster() {
        return (VersionsComparisonForm) master;
    }
    private VersionsComparisonForm getSlave() {
        return (VersionsComparisonForm) slave;
    }
    @Override
    public Object getDefaultCurrentObject() {
        return Global.mainModule.getVersion();
    }
    @Override
    protected String getText() {
        return isReady() ? Utils.ReadAllText(file.file) : "объект не назначен";
    }
    @Override
    protected void removeObject() {
        cbFile.removeAllItems();
        file = null;
    }
    @Override
    public boolean isReady() {
        return super.isReady() && file != null;
    }
    @Override
    protected void showObject() {
        lObjectName.setText(object.name);
        lObjectName.setToolTipText(
                object.getVersionTooltip()
        );
        cbFile.removeAllItems();
        Vector<DBProjectFile> files = object.getFilesForComparsion();
        for (DBProjectFile file : files)
            cbFile.addItem(file);
    }
    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.normalProperties.ExtensionsOn) {
                //если учитываем расширения, ищем полное совпадение
                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);
    }
    public void CheckVersion(db_project_info version_in) {
        if ((object != null) && version_in.Home.equals(object.Home)) {
            RemoveObject();
        }
    }
    @Override
    protected boolean fortranWrapsOn() {
        return Global.normalProperties.FortranWrapsOn;
    }
}
