package application.views;

import application.ApplicationWindow;
import application.editor.Editor;
import application.editor.IStageChangeListener;
import application.editor.Stage;
import application.editor.stages.DataFlowModelingStage;
import application.editor.stages.PushPullSelectionStage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class NavigationWindow extends JDialog implements IStageChangeListener {

    private static final String TITLE = "Navigation";
    private final Editor editor;

    private final JToggleButton dataFlowModelingButton;
    private final JToggleButton pushPullSelectionButton;

    private boolean forbidReentry = false;

    public NavigationWindow(ApplicationWindow owner, Editor editor) {
        super(owner);
        this.editor = editor;
        dataFlowModelingButton = new JToggleButton("Data Flow Modeling");
        pushPullSelectionButton = new JToggleButton("Push Pull Selection");
        dataFlowModelingButton.addActionListener(new DataFlowModelingButtonListener());
        pushPullSelectionButton.addActionListener(new PushPullSelectionButtonListener());
        pushPullSelectionButton.setEnabled(false);
        dataFlowModelingButton.setSelected(true);

        setTitle(TITLE);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        Container panel = getContentPane();
        panel.setLayout(new GridLayout(2, 1));

        ButtonGroup group = new ButtonGroup();
        group.add(dataFlowModelingButton);
        group.add(pushPullSelectionButton);
        panel.add(dataFlowModelingButton);
        panel.add(pushPullSelectionButton);

        pack();

        Point location = new Point(owner.getX() + (owner.getWidth() / 2) - (this.getWidth() / 2), owner.getY() + (owner.getHeight() / 2) - (this.getHeight() / 2));
        setLocation(location);

        setResizable(false);
    }

    @Override
    public void stageChanged(Stage newStage) {
        if (forbidReentry) {
            return;
        }
        if (newStage instanceof DataFlowModelingStage) {
            dataFlowModelingButton.setSelected(true);
            pushPullSelectionButton.setEnabled(editor.canChange(Editor.STAGE_PUSH_PULL_SELECTION));
        } else if (newStage instanceof PushPullSelectionStage) {
            pushPullSelectionButton.setSelected(true);
            dataFlowModelingButton.setEnabled(editor.canChange(Editor.STAGE_DATA_FLOW_MODELING));
        }
    }

    private class DataFlowModelingButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            forbidReentry = true;
            editor.changeStage(Editor.STAGE_DATA_FLOW_MODELING);
            forbidReentry = false;
            pushPullSelectionButton.setEnabled(editor.canChange(Editor.STAGE_PUSH_PULL_SELECTION));
        }
    }

    private class PushPullSelectionButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            forbidReentry = true;
            editor.changeStage(Editor.STAGE_PUSH_PULL_SELECTION);
            forbidReentry = false;
            dataFlowModelingButton.setEnabled(editor.canChange(Editor.STAGE_DATA_FLOW_MODELING));
        }
    }
}
