/*
 * Created by JFormDesigner on Mon Mar 03 22:18:15 CST 2025
 */

package gui.UI_LenheartUI;

import com.alibaba.fastjson.JSONObject;
import gui.Start;
import gui.特殊控制台;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

/**
 * @author dongj
 */
public class DamageControl extends JPanel {
    //伤害控制配置
    public static JSONObject Config;
    //JTableList
    JTable[] Tables;
    JButton[] Buttons;
    //数组
    String[] T = new String[]{"ddbb","ddzb","ddxs","dbbb","dbzb","dbxs","qgbb","qgzb","qgxs"};

    public void SetRightL(JTable tableobj){
        DefaultTableModel tableModel = (DefaultTableModel)tableobj.getModel();

        DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
        rightRenderer.setHorizontalAlignment(JLabel.RIGHT);

        for (int i = 0; i < tableobj.getColumnCount(); i++) {
            if (tableobj.getColumnClass(i) == String.class) {
                tableobj.getColumnModel().getColumn(i).setCellRenderer(rightRenderer);
            }
        }
    }

    public DamageControl() {
        Config = Start.ConfigValuesJson;
        initComponents();
        //储存表
        Tables = new JTable[]{table1,table2,table3,table4,table5,table6,table7,table8,table9};
        Buttons = new JButton[]{button1,button2,button3,button4,button5,button6,button7,button8,button9,button10,button11,button12,button13,button14,button15,button16,button17,button18};

        //读取开关
        toggleButton1.setSelected(Config.getInteger("isdd") == 1 ? true : false);
        toggleButton2.setSelected(Config.getInteger("isdb") == 1 ? true : false);
        toggleButton3.setSelected(Config.getInteger("isqg") == 1 ? true : false);

        //设置属性
        for (int i = 0; i < 9; i++) {
            SetRightL(Tables[i]);
            settableui(i,T[i]);
        }
    }

    private void settableui(Integer Index,String TableName){
        Object JsonObject = Config.get(TableName);
        JSONObject JsonBuffer = (JSONObject) JsonObject;
        // 使用 entrySet() 遍历键值对
        for (Map.Entry<String, Object> entry : JsonBuffer.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue().toString();
            DefaultTableModel tableModel = (DefaultTableModel) Tables[Index].getModel();
            Object[] rowData = {Integer.valueOf(key), value};
            tableModel.addRow(rowData);
        }
    }


    private void 多段开关回调(ActionEvent e) {
        boolean selected = toggleButton1.isSelected();
        Config.put("isdd", selected ? 1 : 0);
    }

    private void 多倍开关回调(ActionEvent e) {
        boolean selected = toggleButton2.isSelected();
        Config.put("isdb", selected ? 1 : 0);
    }

    private void 切割开关回调(ActionEvent e) {
        boolean selected = toggleButton3.isSelected();
        Config.put("isqg", selected ? 1 : 0);
    }

    private void InitConfig(Integer Index,String TableName){
        JSONObject Buffer = new JSONObject();
        DefaultTableModel tableModel = (DefaultTableModel) Tables[Index].getModel();
        int rowCount = tableModel.getRowCount();

        for (int i = 0; i < rowCount; i++) {
            String Key = tableModel.getValueAt(i, 0).toString();
            String Value =  tableModel.getValueAt(i, 1).toString();
            Buffer.put(Key,Value);
        }
        Config.put(TableName, Buffer);
    }

    private void 保存配置(ActionEvent e) {
        //设置属性
        for (int i = 0; i < 9; i++) {
            InitConfig(i,T[i]);
        }

        Start.ConfigValuesJson = Config;
        特殊控制台.setTwConfig();
    }


     /**
     * 按换行符分组，再在组内按逗号分隔成两个数据一组
     * @param text 输入的文本
     * @return 分组后的数据列表，外层 List 表示按行分组，内层 List 表示每行内的两个数据一组
     */
    private List<List<String[]>> groupByLineAndSplit(String text) {
        List<List<String[]>> result = new ArrayList<>();
        String[] lines = text.split("\n");
        for (String line : lines) {
            List<String[]> pairs = new ArrayList<>();
            String[] parts = line.split(",");
            for (int i = 0; i < parts.length; i += 2) {
                if (i + 1 < parts.length) {
                    pairs.add(new String[]{parts[i].trim(), parts[i + 1].trim()});
                } else {
                    pairs.add(new String[]{parts[i].trim()});
                }
            }
            result.add(pairs);
        }
        return result;
    }

    private void 新增按钮回调(ActionEvent e) {
        for (int i = 0; i < 18; i++) {
            if (e.getSource() == Buttons[i]) {
                int buttonNumber = i / 2;
                DefaultTableModel tableModel = (DefaultTableModel) Tables[buttonNumber].getModel();
                // 判断 Shift 键是否被按下
                boolean isShiftPressed = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
                if(isShiftPressed){
                    JTextPane textPane = new JTextPane();
                    JScrollPane scrollPane = new JScrollPane(textPane);
                    scrollPane.setPreferredSize(new Dimension(300, 200));

                    // 弹出包含富文本框的对话框
                    int result = JOptionPane.showConfirmDialog(
                            null,
                            scrollPane,
                            "请输入文本 以  ,  分隔",
                            JOptionPane.OK_CANCEL_OPTION,
                            JOptionPane.PLAIN_MESSAGE
                    );

                    // 检查用户是否点击了确定按钮
                    if (result == JOptionPane.OK_OPTION) {
                        // 获取富文本框中的文本
                        String inputText = textPane.getText();

                        // 按换行符分组，再在组内按逗号分隔成两个数据一组
                        List<List<String[]>> groupedData = groupByLineAndSplit(inputText);
                        for (List<String[]> group : groupedData) {
                            for (String[] pair : group) {
                                if (pair.length == 2) {
                                    Object[] rowData = {pair[0], pair[1]};
                                    tableModel.addRow(rowData);
                                } else if (pair.length == 1) {
                                    System.out.println("分组数据错误: " + pair[0] + ", 无对应数据");
                                }
                            }
                        }
                    }
                }else{
                    Object[] rowData = {0, 0};
                    tableModel.addRow(rowData);
                }
                break;
            }
        }
    }

    private void 删除按钮回调(ActionEvent e) {
        for (int i = 0; i < 18; i++) {
            if (e.getSource() == Buttons[i]) {
                int buttonNumber = i / 2;
                int selectedRow = Tables[buttonNumber].getSelectedRow();
                if (selectedRow != -1) {
                DefaultTableModel tableModel = (DefaultTableModel) Tables[buttonNumber].getModel();
                    tableModel.removeRow(selectedRow);
                    if(tableModel.getRowCount() > selectedRow){
                        Tables[buttonNumber].setRowSelectionInterval(selectedRow, selectedRow);
                    }
                }
                break;
            }
        }
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        scrollPane2 = new JScrollPane();
        table2 = new JTable();
        scrollPane3 = new JScrollPane();
        table3 = new JTable();
        scrollPane4 = new JScrollPane();
        table4 = new JTable();
        scrollPane5 = new JScrollPane();
        table5 = new JTable();
        scrollPane6 = new JScrollPane();
        table6 = new JTable();
        scrollPane7 = new JScrollPane();
        table7 = new JTable();
        scrollPane8 = new JScrollPane();
        table8 = new JTable();
        scrollPane9 = new JScrollPane();
        table9 = new JTable();
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        toggleButton1 = new JToggleButton();
        button1 = new JButton();
        button2 = new JButton();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        label7 = new JLabel();
        label8 = new JLabel();
        label9 = new JLabel();
        button3 = new JButton();
        button4 = new JButton();
        button5 = new JButton();
        button6 = new JButton();
        button7 = new JButton();
        button8 = new JButton();
        button9 = new JButton();
        button10 = new JButton();
        button11 = new JButton();
        button12 = new JButton();
        button13 = new JButton();
        button14 = new JButton();
        button15 = new JButton();
        button16 = new JButton();
        button17 = new JButton();
        button18 = new JButton();
        toggleButton2 = new JToggleButton();
        toggleButton3 = new JToggleButton();
        button19 = new JButton();

        //======== this ========
        setBorder(null);
        setLayout(null);

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                    "\u4ee3\u7801", "\u6bb5\u6570"
                }
            ) {
                Class<?>[] columnTypes = new Class<?>[] {
                    Integer.class, String.class
                };
                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return columnTypes[columnIndex];
                }
            });
            scrollPane1.setViewportView(table1);
        }
        add(scrollPane1);
        scrollPane1.setBounds(18, 30, 164, 520);

        //======== scrollPane2 ========
        {

            //---- table2 ----
            table2.setModel(new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                    "\u4ee3\u7801", "\u6bb5\u6570"
                }
            ) {
                Class<?>[] columnTypes = new Class<?>[] {
                    Integer.class, String.class
                };
                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return columnTypes[columnIndex];
                }
            });
            scrollPane2.setViewportView(table2);
        }
        add(scrollPane2);
        scrollPane2.setBounds(185, 30, 164, 520);

        //======== scrollPane3 ========
        {

            //---- table3 ----
            table3.setModel(new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                    "\u6280\u80fd\u4ee3\u7801", "\u7cfb\u6570"
                }
            ) {
                Class<?>[] columnTypes = new Class<?>[] {
                    Integer.class, String.class
                };
                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return columnTypes[columnIndex];
                }
            });
            scrollPane3.setViewportView(table3);
        }
        add(scrollPane3);
        scrollPane3.setBounds(352, 30, 164, 520);

        //======== scrollPane4 ========
        {

            //---- table4 ----
            table4.setModel(new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                    "\u4ee3\u7801", "\u500d\u6570"
                }
            ) {
                Class<?>[] columnTypes = new Class<?>[] {
                    Integer.class, String.class
                };
                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return columnTypes[columnIndex];
                }
            });
            scrollPane4.setViewportView(table4);
        }
        add(scrollPane4);
        scrollPane4.setBounds(516, 30, 164, 520);

        //======== scrollPane5 ========
        {

            //---- table5 ----
            table5.setModel(new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                    "\u4ee3\u7801", "\u500d\u6570"
                }
            ) {
                Class<?>[] columnTypes = new Class<?>[] {
                    Integer.class, String.class
                };
                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return columnTypes[columnIndex];
                }
            });
            scrollPane5.setViewportView(table5);
        }
        add(scrollPane5);
        scrollPane5.setBounds(680, 30, 164, 520);

        //======== scrollPane6 ========
        {

            //---- table6 ----
            table6.setModel(new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                    "\u6280\u80fd\u4ee3\u7801", "\u7cfb\u6570"
                }
            ) {
                Class<?>[] columnTypes = new Class<?>[] {
                    Integer.class, String.class
                };
                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return columnTypes[columnIndex];
                }
            });
            scrollPane6.setViewportView(table6);
        }
        add(scrollPane6);
        scrollPane6.setBounds(844, 30, 164, 520);

        //======== scrollPane7 ========
        {

            //---- table7 ----
            table7.setModel(new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                    "\u4ee3\u7801", "\u4f24\u5bb3"
                }
            ) {
                Class<?>[] columnTypes = new Class<?>[] {
                    Integer.class, String.class
                };
                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return columnTypes[columnIndex];
                }
            });
            scrollPane7.setViewportView(table7);
        }
        add(scrollPane7);
        scrollPane7.setBounds(1008, 30, 164, 520);

        //======== scrollPane8 ========
        {

            //---- table8 ----
            table8.setModel(new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                    "\u4ee3\u7801", "\u4f24\u5bb3"
                }
            ) {
                Class<?>[] columnTypes = new Class<?>[] {
                    Integer.class, String.class
                };
                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return columnTypes[columnIndex];
                }
            });
            scrollPane8.setViewportView(table8);
        }
        add(scrollPane8);
        scrollPane8.setBounds(1172, 30, 164, 520);

        //======== scrollPane9 ========
        {

            //---- table9 ----
            table9.setModel(new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                    "\u6280\u80fd\u4ee3\u7801", "\u7cfb\u6570"
                }
            ) {
                Class<?>[] columnTypes = new Class<?>[] {
                    Integer.class, String.class
                };
                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return columnTypes[columnIndex];
                }
            });
            scrollPane9.setViewportView(table9);
        }
        add(scrollPane9);
        scrollPane9.setBounds(1336, 30, 164, 520);

        //---- label1 ----
        label1.setText("\u591a\u6bb5\u6570\u636e-\u80cc\u5305");
        add(label1);
        label1.setBounds(25, 5, 105, 30);

        //---- label2 ----
        label2.setText("\u591a\u6bb5\u6570\u636e-\u88c5\u5907");
        add(label2);
        label2.setBounds(190, 5, 105, 30);

        //---- label3 ----
        label3.setText("\u591a\u6bb5\u6570\u636e-\u7cfb\u6570");
        add(label3);
        label3.setBounds(355, 5, 105, 30);

        //---- toggleButton1 ----
        toggleButton1.setText("\u591a\u6bb5\u5f00\u5173");
        toggleButton1.setIcon(new ImageIcon(getClass().getResource("/image/OFF3.png")));
        toggleButton1.setSelectedIcon(new ImageIcon(getClass().getResource("/image/ON3.png")));
        toggleButton1.setFocusPainted(false);
        toggleButton1.setContentAreaFilled(false);
        toggleButton1.addActionListener(e -> 多段开关回调(e));
        add(toggleButton1);
        toggleButton1.setBounds(20, 645, 160, 35);

        //---- button1 ----
        button1.setText("\u65b0\u589e");
        button1.addActionListener(e -> 新增按钮回调(e));
        add(button1);
        button1.setBounds(20, 565, 160, button1.getPreferredSize().height);

        //---- button2 ----
        button2.setText("\u5220\u9664");
        button2.addActionListener(e -> 删除按钮回调(e));
        add(button2);
        button2.setBounds(20, 605, 160, 34);

        //---- label4 ----
        label4.setText("\u591a\u500d\u6570\u636e-\u80cc\u5305");
        add(label4);
        label4.setBounds(520, 5, 105, 30);

        //---- label5 ----
        label5.setText("\u591a\u500d\u6570\u636e-\u88c5\u5907");
        add(label5);
        label5.setBounds(685, 5, 105, 30);

        //---- label6 ----
        label6.setText("\u591a\u500d\u6570\u636e-\u7cfb\u6570");
        add(label6);
        label6.setBounds(850, 5, 105, 30);

        //---- label7 ----
        label7.setText("\u5207\u5272\u6570\u636e-\u80cc\u5305");
        add(label7);
        label7.setBounds(1010, 5, 105, 30);

        //---- label8 ----
        label8.setText("\u5207\u5272\u6570\u636e-\u88c5\u5907");
        add(label8);
        label8.setBounds(1175, 5, 105, 30);

        //---- label9 ----
        label9.setText("\u5207\u5272\u6570\u636e-\u7cfb\u6570");
        add(label9);
        label9.setBounds(1340, 5, 105, 30);

        //---- button3 ----
        button3.setText("\u65b0\u589e");
        button3.addActionListener(e -> 新增按钮回调(e));
        add(button3);
        button3.setBounds(187, 565, 160, 34);

        //---- button4 ----
        button4.setText("\u5220\u9664");
        button4.addActionListener(e -> 删除按钮回调(e));
        add(button4);
        button4.setBounds(187, 605, 160, 34);

        //---- button5 ----
        button5.setText("\u65b0\u589e");
        button5.addActionListener(e -> 新增按钮回调(e));
        add(button5);
        button5.setBounds(354, 565, 160, 34);

        //---- button6 ----
        button6.setText("\u5220\u9664");
        button6.addActionListener(e -> 删除按钮回调(e));
        add(button6);
        button6.setBounds(354, 605, 160, 34);

        //---- button7 ----
        button7.setText("\u65b0\u589e");
        button7.addActionListener(e -> 新增按钮回调(e));
        add(button7);
        button7.setBounds(520, 565, 160, 34);

        //---- button8 ----
        button8.setText("\u5220\u9664");
        button8.addActionListener(e -> 删除按钮回调(e));
        add(button8);
        button8.setBounds(520, 605, 160, 34);

        //---- button9 ----
        button9.setText("\u65b0\u589e");
        button9.addActionListener(e -> 新增按钮回调(e));
        add(button9);
        button9.setBounds(685, 565, 160, 34);

        //---- button10 ----
        button10.setText("\u5220\u9664");
        button10.addActionListener(e -> 删除按钮回调(e));
        add(button10);
        button10.setBounds(685, 605, 160, 34);

        //---- button11 ----
        button11.setText("\u65b0\u589e");
        button11.addActionListener(e -> 新增按钮回调(e));
        add(button11);
        button11.setBounds(848, 565, 160, 34);

        //---- button12 ----
        button12.setText("\u5220\u9664");
        button12.addActionListener(e -> 删除按钮回调(e));
        add(button12);
        button12.setBounds(848, 605, 160, 34);

        //---- button13 ----
        button13.setText("\u65b0\u589e");
        button13.addActionListener(e -> 新增按钮回调(e));
        add(button13);
        button13.setBounds(1012, 565, 160, 34);

        //---- button14 ----
        button14.setText("\u5220\u9664");
        button14.addActionListener(e -> 删除按钮回调(e));
        add(button14);
        button14.setBounds(1012, 605, 160, 34);

        //---- button15 ----
        button15.setText("\u65b0\u589e");
        button15.addActionListener(e -> 新增按钮回调(e));
        add(button15);
        button15.setBounds(1176, 565, 160, 34);

        //---- button16 ----
        button16.setText("\u5220\u9664");
        button16.addActionListener(e -> 删除按钮回调(e));
        add(button16);
        button16.setBounds(1176, 605, 160, 34);

        //---- button17 ----
        button17.setText("\u65b0\u589e");
        button17.addActionListener(e -> 新增按钮回调(e));
        add(button17);
        button17.setBounds(1340, 565, 160, 34);

        //---- button18 ----
        button18.setText("\u5220\u9664");
        button18.addActionListener(e -> 删除按钮回调(e));
        add(button18);
        button18.setBounds(1340, 605, 160, 34);

        //---- toggleButton2 ----
        toggleButton2.setText("\u591a\u500d\u5f00\u5173");
        toggleButton2.setIcon(new ImageIcon(getClass().getResource("/image/OFF3.png")));
        toggleButton2.setSelectedIcon(new ImageIcon(getClass().getResource("/image/ON3.png")));
        toggleButton2.setFocusPainted(false);
        toggleButton2.setContentAreaFilled(false);
        toggleButton2.addActionListener(e -> 多倍开关回调(e));
        add(toggleButton2);
        toggleButton2.setBounds(187, 645, 160, 35);

        //---- toggleButton3 ----
        toggleButton3.setText("\u5207\u5272\u5f00\u5173");
        toggleButton3.setIcon(new ImageIcon(getClass().getResource("/image/OFF3.png")));
        toggleButton3.setSelectedIcon(new ImageIcon(getClass().getResource("/image/ON3.png")));
        toggleButton3.setFocusPainted(false);
        toggleButton3.setContentAreaFilled(false);
        toggleButton3.addActionListener(e -> 切割开关回调(e));
        add(toggleButton3);
        toggleButton3.setBounds(355, 645, 160, 35);

        //---- button19 ----
        button19.setText("\u4fdd\u5b58\u914d\u7f6e");
        button19.addActionListener(e -> 保存配置(e));
        add(button19);
        button19.setBounds(520, 645, 160, 35);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < getComponentCount(); i++) {
                Rectangle bounds = getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            setMinimumSize(preferredSize);
            setPreferredSize(preferredSize);
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JScrollPane scrollPane1;
    private JTable table1;
    private JScrollPane scrollPane2;
    private JTable table2;
    private JScrollPane scrollPane3;
    private JTable table3;
    private JScrollPane scrollPane4;
    private JTable table4;
    private JScrollPane scrollPane5;
    private JTable table5;
    private JScrollPane scrollPane6;
    private JTable table6;
    private JScrollPane scrollPane7;
    private JTable table7;
    private JScrollPane scrollPane8;
    private JTable table8;
    private JScrollPane scrollPane9;
    private JTable table9;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JToggleButton toggleButton1;
    private JButton button1;
    private JButton button2;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JLabel label7;
    private JLabel label8;
    private JLabel label9;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    private JButton button6;
    private JButton button7;
    private JButton button8;
    private JButton button9;
    private JButton button10;
    private JButton button11;
    private JButton button12;
    private JButton button13;
    private JButton button14;
    private JButton button15;
    private JButton button16;
    private JButton button17;
    private JButton button18;
    private JToggleButton toggleButton2;
    private JToggleButton toggleButton3;
    private JButton button19;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
