package com.handoitasdf.drive_checker.ui;

import com.handoitasdf.drive_checker.DriveChecker;

import javax.annotation.Nonnull;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * Created by icand on 2017/8/31.
 */
public class ControlPane extends JPanel {
    private final JFileChooser fileChooser = new JFileChooser();
    private final JTextField testFilePathField = new JTextField();
    private ControlPaneListener listener;
    private final Component parent;

    public ControlPane(@Nonnull Component parent) {
        super(new GridBagLayout());
        this.parent = parent;
        initTestFileLabel();
        initTestFilePathField();
        initSelectFileButton();
        initStartButton();
    }

    public void setControlPaneListener(@Nonnull ControlPaneListener listener) {
        this.listener = listener;
    }

    private void initStartButton() {
        JButton button = new JButton("Run");
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 3;
        constraints.gridy = 0;
        constraints.weightx = 0.0;
        constraints.weighty = 0.0;
        add(button, constraints);
        button.addActionListener(e -> {
            File testFile = new File(testFilePathField.getText());
            if (!testFile.isFile()) {
                JOptionPane.showMessageDialog(
                        parent,
                        "Fail to open file \"" + testFile.getPath() + "\"");
                return;
            }
            listener.onRun(testFile);
        });
    }

    private void initTestFileLabel() {
        JLabel label = new JLabel("Test file: ");
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.weightx = 0.0;
        constraints.weighty = 0.0;
        add(label, constraints);
    }

    private void initSelectFileButton() {
        JButton button = new JButton("Open");
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 2;
        constraints.gridy = 0;
        constraints.weightx = 0.0;
        constraints.weighty = 0.0;
        add(button, constraints);

        button.addActionListener(e -> {
            int state = fileChooser.showOpenDialog(parent);
            if (state != JFileChooser.APPROVE_OPTION) {
                return;
            }
            File selectedFile = fileChooser.getSelectedFile();
            testFilePathField.setText(selectedFile.getAbsolutePath());
        });
    }

    private void initTestFilePathField() {
        testFilePathField.setEditable(true);
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 1;
        constraints.gridy = 0;
        constraints.weightx = 1.0;
        constraints.weighty = 1.0;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        add(testFilePathField, constraints);
    }
}

@@ -1528,32 +1528,294 @@
 Listener(e -%3E %7B%0A
+            String path = testFilePathField.getText();%0A            if (path.isEmpty()) %7B%0A                JOptionPane.showMessageDialog(%0A                        parent,%0A                        %22Please select the test file%22);%0A                return;%0A            %7D%0A
             File
@@ -1861,24 +1861,24 @@
 getText());%0A
-
             
@@ -2011,26 +2011,8 @@
    %22
-Fail to open file 
 %5C%22%22 
@@ -2025,16 +2025,24 @@
 File.get
+Absolute
 Path() +
@@ -2045,16 +2045,29 @@
 () + %22%5C%22
+ isn't a file
 %22);%0A    

Check empty test file path.