/*
 * Created by JFormDesigner on Wed Mar 12 16:10:03 CST 2025
 */

package gui.UI_LenheartUI;

import gui.Start;
import gui.tw.BlConfig;
import gui.tw.ZLConfig;
import gui.特殊控制台;

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

/**
 * @author dongj
 */
public class FightControl extends JPanel {
    public FightControl() {
        initComponents();
    }

    private void 新增配置(ActionEvent e) {
        DefaultTableModel tableModel = (DefaultTableModel)this.table1.getModel();
        Object[] rowData = {0, 0 ,0,0,0, 0};
        tableModel.addRow(rowData);
    }

    private void 删除配置(ActionEvent e) {
        // TODO add your code here
        int selectedRow = table1.getSelectedRow();
        if (selectedRow != -1) {
            DefaultTableModel tableModel = (DefaultTableModel) table1.getModel();
            tableModel.removeRow(selectedRow);
            if(tableModel.getRowCount() > selectedRow)table1.setRowSelectionInterval(selectedRow, selectedRow);
        }
    }

    private void 保存配置(ActionEvent e) {
        Start.zlkg = this.toggleButton3.isSelected() ? 1 : 0;

        List<ZLConfig> Buf = new ArrayList<>();
        DefaultTableModel tableModel = (DefaultTableModel) table1.getModel();
        int rowCount = tableModel.getRowCount();
        for (int i = 0; i < rowCount; i++) {

            Integer Count = (int) tableModel.getValueAt(i, 0);
            String NameFront = tableModel.getValueAt(i, 1).toString();
            String NameBack = tableModel.getValueAt(i, 2).toString();

            ZLConfig buffer = new ZLConfig();
            buffer.setNum(Count);
            buffer.setName(NameFront);
            buffer.setName2(NameBack);
            Buf.add(buffer);
        }
        Start.zlConfigs = Buf;

        String[] originalArray = this.textArea1.getText().split(","); // 按逗号切割成数组
        Start.shxs = originalArray;
        特殊控制台.setTwConfig();
    }

    private void 战力开关(ActionEvent e) {
        // TODO add your code here
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        button21 = new JButton();
        button19 = new JButton();
        button25 = new JButton();
        toggleButton3 = new JToggleButton();
        label3 = new JLabel();
        label1 = new JLabel();
        scrollPane2 = new JScrollPane();
        textArea1 = new JTextArea();

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

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

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                    "\u8f6c\u751f\u6b21\u6570", "\u540d\u5b57\u524d\u7f00", "\u540d\u5b57\u540e\u7f00"
                }
            ) {
                Class<?>[] columnTypes = new Class<?>[] {
                    Integer.class, String.class, String.class
                };
                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return columnTypes[columnIndex];
                }
            });
            scrollPane1.setViewportView(table1);
        }
        add(scrollPane1);
        scrollPane1.setBounds(18, 10, scrollPane1.getPreferredSize().width, 595);

        //---- button21 ----
        button21.setText("\u5220\u9664\u914d\u7f6e");
        button21.addActionListener(e -> 删除配置(e));
        add(button21);
        button21.setBounds(375, 615, 100, 35);

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

        //---- button25 ----
        button25.setText("\u65b0\u589e\u914d\u7f6e");
        button25.addActionListener(e -> 新增配置(e));
        add(button25);
        button25.setBounds(270, 615, 100, 35);

        //---- toggleButton3 ----
        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(20, 615, 100, 35);

        //---- label3 ----
        label3.setText("\u6218\u529b\u5f00\u5173");
        add(label3);
        label3.setBounds(125, 620, 185, 30);

        //---- label1 ----
        label1.setText("\u4f24\u5bb3\u663e\u793a\u5355\u4f4d");
        add(label1);
        label1.setBounds(new Rectangle(new Point(490, 10), label1.getPreferredSize()));

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

            //---- textArea1 ----
            textArea1.setLineWrap(true);
            scrollPane2.setViewportView(textArea1);
        }
        add(scrollPane2);
        scrollPane2.setBounds(490, 35, 940, 110);

        {
            // 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


        this.toggleButton3.setSelected(Start.zlkg == 1 ? true : false);

        DefaultTableModel tableModel = (DefaultTableModel)this.table1.getModel();
        // 创建一个自定义的单元格渲染器，用于右对齐字符串
        DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
        rightRenderer.setHorizontalAlignment(JLabel.RIGHT);
        // 遍历表格的所有列，将字符串类型的列设置为右对齐
        for (int i = 0; i < table1.getColumnCount(); i++) {
            if (table1.getColumnClass(i) == String.class) {
                table1.getColumnModel().getColumn(i).setCellRenderer(rightRenderer);
            }
        }

        Start.zlConfigs.forEach((obj) -> {
            Integer Count = obj.getNum();
            String NameFront = obj.getName();
            String NameBack = obj.getName2();

            Object[] rowData = {Count, NameFront ,NameBack};
            tableModel.addRow(rowData);
        });


        String XB = "";
        for (String shx : Start.shxs) {
            XB += shx;
            XB += ",";
        }
        XB = XB.substring(0, XB.length() - 1);
        this.textArea1.setText(XB);
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JScrollPane scrollPane1;
    private JTable table1;
    private JButton button21;
    private JButton button19;
    private JButton button25;
    private JToggleButton toggleButton3;
    private JLabel label3;
    private JLabel label1;
    private JScrollPane scrollPane2;
    private JTextArea textArea1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
