package org.bdware.sc.contractGen;

import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class ContractGeneratorUI {
    public static void main(String[] args) {
        try {
            BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;
            org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        InitGlobalFont(new Font("alias", Font.PLAIN, 15));

        JFrame frame = new JFrame("索引生成");//创建Frame窗口
        JPanel checkbox_panel;
        final JCheckBox checkBox_time;
        final JCheckBox checkBox_account;
        checkbox_panel = new JPanel();//面板索引选择
        checkBox_time = new JCheckBox("时间索引");
        checkBox_account = new JCheckBox("账户索引");

        checkbox_panel.add(checkBox_time);
        checkbox_panel.add(checkBox_account);

        final JPanel input_sdk = new JPanel();//SDK包名;
        final JLabel sdk_label = new JLabel("SDK类名");
        final JTextField sdk = new JTextField(10);
        final JLabel datalen_label = new JLabel("数据长度");
        final JTextField data_length = new JTextField(10);
        final JCheckBox height_sync_cbox = new JCheckBox("使用高度同步");
        input_sdk.add(sdk_label);
        input_sdk.add(sdk);
        input_sdk.add(datalen_label);
        input_sdk.add(data_length);
        input_sdk.add(height_sync_cbox);

        final JTabbedPane tab = new JTabbedPane();//选项卡
        final JPanel cards = new JPanel();    //卡片式布局的面板
        cards.add(input_sdk);
        cards.add(checkbox_panel);
        cards.add(tab);
        frame.add(cards);
        frame.setBounds(100, 200, 800, 800);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final Map<String, MainFrame> framemap = new HashMap<>();
        checkBox_time.addActionListener(e -> {
            System.out.println("performed");
            if (checkBox_time.isSelected()) {
                if (sdk.getText().length() != 0 && data_length.getText().length() != 0) {
                    Map<String, String> map = new HashMap<>();
                    map.put("indexData", "./RocksDB");
                    map.put("datalen", data_length.getText());
                    map.put("sdkpath", sdk.getText());
                    if (height_sync_cbox.isSelected()) {
                        map.put("syncbyheight", "true");
                    }
                    String res = ContractGenerator.generateAPIContract(map);
                    MainFrame time_panel = new MainFrame();
                    framemap.put("time", time_panel);
                    time_panel.textfield.setText(res);
                    tab.add(time_panel, "时间索引");
                    try {
                        File file = new File("./output/timeindex.yjs");
                        file.createNewFile();
                        FileWriter writer = new FileWriter(file);
                        BufferedWriter out = new BufferedWriter(writer);
                        out.write(res);
                        out.close();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            } else {
                tab.remove(framemap.get("time"));
            }
        });
        checkBox_account.addActionListener(e -> {
            if (checkBox_account.isSelected()) {
                Map<String, String> map = new HashMap<>();
                map.put("indexData", "./RocksDB");
                String res = ContractGenerator.generateAPIContract(map);
                MainFrame account_panel = new MainFrame();
                framemap.put("account", account_panel);
                account_panel.textfield.setText(res);
                tab.add(account_panel, "账户索引");
                try {
                    File file = new File("./output/accountindex.yjs");
                    file.createNewFile();
                    FileWriter writer = new FileWriter(file);
                    BufferedWriter out = new BufferedWriter(writer);
                    out.write(res);
                    out.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            } else {
                tab.remove(framemap.get("account"));
            }
        });
    }

    /**
     * 统一设置字体，父界面设置之后，所有由父界面进入的子界面都不需要再次设置字体
     */
    private static void InitGlobalFont(Font font) {
        FontUIResource fontRes = new FontUIResource(font);
        for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements(); ) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof FontUIResource) {
                UIManager.put(key, fontRes);
            }
        }
    }
}
