package com.bsphp.car;

import com.google.gson.JsonObject;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

final class ConsoleDialog extends JDialog {
    private final AccountBSPHPClient client;
    private final JTextArea out = new JTextArea(18, 64);
    private final JLabel state = new JLabel("控制台就绪");
    private static final DateTimeFormatter TIME_FMT = DateTimeFormatter.ofPattern("HH:mm:ss");

    ConsoleDialog(Frame owner, AccountBSPHPClient client) {
        super(owner, "BSPHP 控制台", false);
        this.client = client;
        out.setEditable(false);
        out.setLineWrap(true);
        out.setWrapStyleWord(true);

        JPanel root = new JPanel(new BorderLayout(8, 8));
        root.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        JPanel btns = new JPanel(new GridLayout(0, 4, 8, 8));
        btns.add(btn("服务器时间", () -> client.getServerDate()));
        btns.add(btn("预设URL", () -> client.getPresetURL()));
        btns.add(btn("Web地址", () -> client.getWebURL()));
        btns.add(btn("全局配置", () -> client.getGlobalInfo()));

        btns.add(btn("验证码开关", () -> client.getCodeEnabled()));
        btns.add(btn("逻辑值A", () -> client.getLogicA()));
        btns.add(btn("逻辑值B", () -> client.getLogicB()));
        btns.add(btn("逻辑A内容", () -> client.getLogicInfoA()));

        btns.add(btn("逻辑B内容", () -> client.getLogicInfoB()));
        btns.add(btn("软件配置", () -> client.getAppCustom("myapp")));
        btns.add(btn("VIP配置", () -> client.getAppCustom("myvip")));
        btns.add(btn("登录配置", () -> client.getAppCustom("mylogin")));

        btns.add(btn("检测到期", () -> client.getEndTime()));
        btns.add(btn("用户信息", () -> client.getUserInfo()));
        btns.add(btn("用户特征Key", () -> client.getUserKey()));
        btns.add(btn("心跳包", () -> client.heartbeat()));

        btns.add(btn("注销登录", () -> client.logout()));

        JPanel top = new JPanel(new BorderLayout(6, 6));
        top.add(btns, BorderLayout.CENTER);
        root.add(top, BorderLayout.NORTH);
        root.add(new JScrollPane(out), BorderLayout.CENTER);

        JPanel south = new JPanel(new FlowLayout(FlowLayout.LEFT, 8, 4));
        south.add(state);
        root.add(south, BorderLayout.SOUTH);

        setContentPane(root);
        pack();
        setLocationRelativeTo(owner);
    }

    @FunctionalInterface
    private interface Api {
        JsonObject run() throws Exception;
    }

    private JButton btn(String title, Api api) {
        JButton b = new JButton(title);
        b.addActionListener(e -> new SwingWorker<Void, Void>() {
            @Override
            protected Void doInBackground() throws Exception {
                JsonObject r = api.run();
                javax.swing.SwingUtilities.invokeLater(() -> showResult(title, r));
                return null;
            }
        }.execute());
        return b;
    }

    private void showResult(String title, JsonObject r) {
        String msg = r == null ? "请求失败" : AccountBSPHPClient.msg(r.get("data"));
        String code = "-";
        if (r != null && r.has("code") && !r.get("code").isJsonNull()) code = r.get("code").getAsString();
        String line = "[" + LocalTime.now().format(TIME_FMT) + "] [" + title + "] code=" + code + " " + msg;
        out.append(line + "\n");
        out.setCaretPosition(out.getDocument().getLength());
        state.setText("最近操作: " + title + " (code=" + code + ")");
    }
}

