package _VisualDVM.GlobalData.Module.UI;
import Common.MainModule_;
import Common.Passes.Pass;
import Common.Visual.Controls.StyledTextComboBox;
import Common.Visual.UI;
import Common.Visual.Windows.Dialog.DialogFields;
import _VisualDVM.Global;
import _VisualDVM.GlobalData.Compiler.Compiler;
import _VisualDVM.GlobalData.CompilerOption.Json.OptionsJson;
import _VisualDVM.GlobalData.Machine.Machine;
import _VisualDVM.GlobalData.Makefile.Makefile;
import _VisualDVM.GlobalData.Module.Module;
import _VisualDVM.GlobalData.Module.ModuleAnchestor;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.ProjectData.LanguageName;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedHashMap;
public class ModuleAnchestorFields implements DialogFields {
    public JPanel content;
    public JComboBox<Compiler> cbCompilers;
    public JComboBox<String> cbCommands;
    public JComboBox<String> cbFlags;
    private JButton bHelp;
    private JButton BPickOptions;
    private Compiler compiler;
    //считаем что машина есть.
    public ModuleAnchestorFields() {
        //-
        LinkedHashMap<Integer, Compiler> compilers = MainModule_.instance.getDb().getTable(Machine.class).getUI().getCurrent().getCompilers();
        compilers.values().forEach(compiler -> cbCompilers.addItem(compiler));
        bHelp.addActionListener(e -> {
            if (cbCompilers.getSelectedItem() != null) {
                Global.mainModule.getPass(PassCode.ShowCompilerHelp).Do(compiler, true);
            } else UI.Info("Компилятор не выбран");
        });
        BPickOptions.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Pass pass = Global.mainModule.getPass(PassCode.PickCompilerOptions);
                if (pass.Do(compiler)) {
                    UI.TrySelect(cbFlags, ((OptionsJson) pass.target).toLine());
                }
            }
        });
        cbCompilers.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                compiler = (Compiler) cbCompilers.getSelectedItem();
            }
        });
    }
    public void setListeners(ModuleAnchestor target) {
        boolean linker = (target instanceof Makefile);
        LanguageName languageName = linker ?
                (Global.mainModule.HasProject() ? Global.mainModule.getProject().languageName : LanguageName.n) :
                ((Module) target).language;
        cbCompilers.addActionListener(e -> {
            if (cbCompilers.getSelectedItem() instanceof Compiler) {
                Compiler compiler = ((Compiler) cbCompilers.getSelectedItem());
                UI.TrySelect(cbCommands,
                        linker ? compiler.getSpecialLinkCommand(languageName) : compiler.getSpecialCompilationCommand(languageName));
            }
        });
    }
    @Override
    public Component getContent() {
        return content;
    }
    private void createUIComponents() {
        // TODO: place custom component creation code here
        cbFlags = new StyledTextComboBox();
    }
}
