package com.liang.novel.plugin.ui;

import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.*;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import org.jetbrains.annotations.Nullable;

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

public class EditDialogUI extends DialogWrapper {

    private JPanel contentPanel;
    private JTextField bookName;
    private TextFieldWithBrowseButton url;

    public EditDialogUI() {
        super(true);
        setTitle("编辑书籍");
        init();

        //禁止对话框大小可变
        setResizable(false);

        //设置TextFieldWithBrowseButton跟JTextField相同样式
        bookName.setColumns(15);
        url.setPreferredSize(bookName.getPreferredSize());

        FileChooserDescriptor descriptor = new FileChooserDescriptor(true, false, false, false, false, false);
        descriptor.withFileFilter(file -> file.getName().toLowerCase().endsWith(".txt")); //只要txt
        TextBrowseFolderListener listener = new TextBrowseFolderListener(descriptor);
        url.addBrowseFolderListener(listener);
    }

    @Override
    protected @Nullable JComponent createCenterPanel() {
        return contentPanel;
    }

    @Override
    protected void doOKAction() {
        ValidationInfo validationInfo = doValidate();
        if (validationInfo != null) {
            setErrorText(validationInfo.message);
        } else {
            super.doOKAction();
        }
    }

    @Override
    protected @Nullable ValidationInfo doValidate() {
        String urlText = url.getText();
        if (!urlText.endsWith("txt")) {
            return new ValidationInfo("请选择txt文件", url);
        }
        return null; //返回 null 表示验证通过
    }

    public String getBookName() {
        return bookName.getText();
    }

    public void setBookName(String bookName) {
        this.bookName.setText(bookName);
    }

    public String getUrl() {
        return url.getText();
    }

    public void setUrl(String url) {
        this.url.setText(url);
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        contentPanel = new JPanel();
        contentPanel.setLayout(new GridLayoutManager(2, 2, new Insets(0, 0, 0, 0), -1, -1));
        final JLabel label1 = new JLabel();
        label1.setText("书名：");
        contentPanel.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        bookName = new JTextField();
        contentPanel.add(bookName, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        final JLabel label2 = new JLabel();
        label2.setText("路径：");
        contentPanel.add(label2, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        url = new TextFieldWithBrowseButton();
        contentPanel.add(url, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return contentPanel;
    }

}
