package com.ruoyi.common.dev;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.dev.gui.InputField;

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

public class SQLQueryFuncGenGUI extends JFrame
{
    private SQLQueryFuncGen engine;
    private InputField      queryId;
    private InputField      entity;
    private InputField      module;
    private InputField      packageName;
    private InputField      comment;
    private InputField      parameterType;
    private InputField      returnType;
    private JTextPane       result;
    private JButton writeBtn;
    private ActionListener inputListener = new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            writeBtn.setEnabled(false);
            result.setText("");
        }
    };

    public SQLQueryFuncGenGUI()
    {
        super();
        engine = new SQLQueryFuncGen();
        Setup();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Mybatis查询代码生成器");
        pack();
    }

    private void Setup()
    {
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        JMenu fileMenu = new JMenu("文件");
        JMenuItem menuItem = new JMenuItem("清空");
        menuItem.addActionListener(x -> Reset());
        fileMenu.add(menuItem);
        menuItem = new JMenuItem("退出");
        menuItem.addActionListener(x -> System.exit(0));
        fileMenu.add(menuItem);
        JMenu otherMenu = new JMenu("其他");
        menuItem = new JMenuItem("关于");
        menuItem.addActionListener(x -> JOptionPane.showMessageDialog(this, "通过Mybatis查询ID自动生成mapper/service/serviceImpl源码", "关于", JOptionPane.INFORMATION_MESSAGE));
        otherMenu.add(menuItem);
        menuBar.add(fileMenu);
        menuBar.add(otherMenu);
        queryId = new InputField("Mybatis查询ID: ");
        entity = new InputField("实体类名(短): ");
        module = new InputField("模块: ");
        packageName = new InputField("包名: ");
        parameterType = new InputField("参数类型: ");
        returnType = new InputField("返回类型: ");
        comment = new InputField("注释: ");
        queryId.AddActionListener(inputListener);
        entity.AddActionListener(inputListener);
        module.AddActionListener(inputListener);
        packageName.AddActionListener(inputListener);
        comment.AddActionListener(inputListener);
        parameterType.AddActionListener(inputListener);
        returnType.AddActionListener(inputListener);
        queryId.SetToolTipText("例如. selectTHomeapplyYdjfsqList");
        entity.SetToolTipText("例如. THomeapplyYdjfsq");
        module.SetToolTipText("例如. house");
        packageName.SetToolTipText("例如. home(如果和模块名称相同则可为空)");
        comment.SetToolTipText("例如. 查询宅基地申请列表");
        parameterType.SetToolTipText("例如. THomeapplyYdjfsq");
        returnType.SetToolTipText("例如. List<THomeapplyYdjfsq>");
        Box formPanel = Box.createVerticalBox();
        formPanel.setAutoscrolls(true);
        formPanel.add(queryId);
        formPanel.add(entity);
        formPanel.add(module);
        formPanel.add(packageName);
        formPanel.add(parameterType);
        formPanel.add(returnType);
        formPanel.add(comment);
        result = new JTextPane();
        JScrollPane scrollPane = new JScrollPane(result);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        result.setFont(Font.getFont(Font.MONOSPACED));
        formPanel.add(scrollPane);
        BorderLayout mainLayout = new BorderLayout();
        JPanel panel = new JPanel();
        panel.setLayout(mainLayout);
        panel.add(formPanel, BorderLayout.CENTER);
        Box toolbar = Box.createHorizontalBox();
        toolbar.add(Box.createHorizontalGlue());
        writeBtn = new JButton("写入");
        writeBtn.addActionListener(e -> Write());
        writeBtn.setEnabled(false);
        toolbar.add(writeBtn);
        JButton startBtn = new JButton("生成");
        startBtn.addActionListener(e -> Gen());
        toolbar.add(startBtn);
        JButton clearBtn = new JButton("清空");
        clearBtn.addActionListener(e -> Reset());
        toolbar.add(clearBtn);
        panel.add(toolbar, BorderLayout.SOUTH);
        setContentPane(panel);
    }

    public String PreviewSourceCode()
    {
        engine.ScanFilePath();
        if(!engine.IsValid())
        {
            JOptionPane.showMessageDialog(this, "缺失必要的信息!", "错误", JOptionPane.ERROR_MESSAGE);
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append(engine.getMapper_java()).append(": \n");
        sb.append(engine.GenMapperCode()).append("\n\n");
        sb.append(engine.getService_java()).append(": \n");
        sb.append(engine.GenServiceCode()).append("\n\n");
        sb.append(engine.getServiceImpl_java()).append(": \n");
        sb.append(engine.GenServiceImplCode()).append("\n\n");
        return sb.toString();
    }

    private void Reset()
    {
        engine.Reset();
        queryId.Reset();
        entity.Reset();
        module.Reset();
        packageName.Reset();
        comment.Reset();
        parameterType.Reset();
        returnType.Reset();
        result.setText("");
        writeBtn.setEnabled(false);
    }

    private void Write()
    {
        if(engine.Write())
        {
            JOptionPane.showMessageDialog(this, "写入源码文件成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
        }
        else
        {
            JOptionPane.showMessageDialog(this, "写入源码文件失败!", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void Gen()
    {
        engine.setQueryId(queryId.Text());
        engine.setEntity(entity.Text());
        engine.setModule(module.Text());
        engine.setPackageName(packageName.Text());
        engine.setComment(comment.Text());
        engine.SetParameterType(parameterType.Text());
        engine.SetReturnType(returnType.Text());
        String code = PreviewSourceCode();
        result.setText(code);
        writeBtn.setEnabled(StrUtil.isNotEmpty(code));
    }
}

