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

        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;
        特殊控制台.setTwConfig();
    }

    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();

        //======== 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, 620, 100, 35);

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

        //---- button25 ----
        button25.setText("\u65b0\u589e\u914d\u7f6e");
        button25.addActionListener(e -> 新增配置(e));
        add(button25);
        button25.setBounds(270, 620, 100, 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

        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);
        });
    }

    // 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;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
