package com.ruoyi.common.dev;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.dev.gui.FileField;
import com.ruoyi.common.dev.gui.InputField;

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

public class ExtractGeneratedSourceGUI extends JFrame
{
    private ExtractGeneratedSource engine;
    private FileField              zipPath;
    private FileField              javaProjectPath;
    private FileField  webProjectPath;
    private InputField module;
    private JTextPane  result;
    private JButton writeBtn;
    private boolean canWrite;

    public ExtractGeneratedSourceGUI()
    {
        super();
        engine = new ExtractGeneratedSource();
        Setup();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("代码解压器");
        pack();
    }

    private void Reset()
    {
        engine.Reset();
        zipPath.Reset();
        javaProjectPath.Reset();
        module.Reset();
        webProjectPath.Reset();
        canWrite = false;
        writeBtn.setEnabled(false);
    }

    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, "解压生成的代码压缩包", "关于", JOptionPane.INFORMATION_MESSAGE));
        otherMenu.add(menuItem);
        menuBar.add(fileMenu);
        menuBar.add(otherMenu);
        zipPath = new FileField("源码zip文件(可拖拽)");
        javaProjectPath = new FileField("后端项目根目录(可拖拽)");
        webProjectPath = new FileField("前端项目根目录(可拖拽)");
        String path;
        path = System.getenv("RUOYI_SOURCE_DIRECTORY_PATH");
        if(StringUtils.isNotEmpty(path))
            zipPath.SetLastPath(path);
        path = System.getenv("RUOYI_JAVA_PROJECT_PATH");
        if(StringUtils.isNotEmpty(path))
            javaProjectPath.SetPath(path);
        path = System.getenv("RUOYI_WEB_PROJECT_PATH");
        if(StringUtils.isNotEmpty(path))
            webProjectPath.SetPath(path);
        module = new InputField("模块");
        module.SetToolTipText("例如. home(为空则自动从zip中获取)");
        zipPath.SetFilter(".zip");
        javaProjectPath.SetMode(JFileChooser.DIRECTORIES_ONLY);
        webProjectPath.SetMode(JFileChooser.DIRECTORIES_ONLY);
        Box formPanel = Box.createVerticalBox();
        formPanel.setAutoscrolls(true);
        formPanel.add(zipPath);
        formPanel.add(javaProjectPath);
        formPanel.add(webProjectPath);
        formPanel.add(module);
        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 -> Copy());
        writeBtn.setEnabled(false);
        toolbar.add(writeBtn);
        JButton startBtn = new JButton("测试");
        startBtn.addActionListener(e -> Test());
        toolbar.add(startBtn);
        JButton clearBtn = new JButton("清空");
        clearBtn.addActionListener(e -> Reset());
        toolbar.add(clearBtn);
        panel.add(toolbar, BorderLayout.SOUTH);
        setContentPane(panel);
    }

    private void Test()
    {
        engine.Set(zipPath.Path(), javaProjectPath.Path(), webProjectPath.Path(), module.Text());
        if(!engine.IsValid())
        {
            JOptionPane.showMessageDialog(this, "必须设置源码zip和后端/前端项目目录", "警告", JOptionPane.ERROR_MESSAGE);
            return;
        }
        engine.Begin();
        String[] res = {null};
        canWrite = engine.Test(res);
        result.setText(res[0]);
        writeBtn.setEnabled(canWrite);
    }

    private void Copy()
    {
        if(canWrite)
        {
            boolean r = engine.Extract((src, dst) -> {
                int res = JOptionPane.showConfirmDialog(this, "是否覆盖已存在文件: " + dst + "(原文件: " + src + ")", "警告", JOptionPane.YES_NO_CANCEL_OPTION);
                if(res == JOptionPane.YES_OPTION)
                    return 1;
                else if(res == JOptionPane.NO_OPTION)
                    return 0;
                else// if(res == JOptionPane.CANCEL_OPTION)
                    return -1;
            });
            if(r)
                JOptionPane.showMessageDialog(this, "复制文件成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
            else
                JOptionPane.showMessageDialog(this, "复制文件失败!", "错误", JOptionPane.ERROR_MESSAGE);
        }
        engine.End();
    }
}