/*
 * Created by JFormDesigner on Fri Apr 04 18:03:27 CST 2025
 */

package gui.UI_LenheartUI;

import java.awt.event.*;
import gui.Start;
import gui.tw.AotuNpc;
import gui.tw.diaoyu.DiaoYuMap;
import gui.特殊控制台;

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

/**
 * @author dongj
 */
public class AutoScriptNpc extends JPanel {
    public AutoScriptNpc() {
        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) {
        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) {
        DefaultTableModel tableModel = (DefaultTableModel) table1.getModel();
        // 清空原有的数据列表
        Start.aotuNpcs.clear();
        // 遍历表格的每一行
        for (int i = 0; i < tableModel.getRowCount(); i++) {
            AotuNpc aotuNpc = new AotuNpc();
            // 从表格中获取数据并设置到对象中
            aotuNpc.setMapId(Integer.parseInt(tableModel.getValueAt(i, 0).toString()));
            aotuNpc.setNpcId(Integer.parseInt(tableModel.getValueAt(i, 1).toString()));
            aotuNpc.setScriptsName(tableModel.getValueAt(i, 2).toString());
            aotuNpc.setItemId(Integer.parseInt(tableModel.getValueAt(i, 3).toString()));
            aotuNpc.setItemNum(Integer.parseInt(tableModel.getValueAt(i, 4).toString()));
            aotuNpc.setEquId(Integer.parseInt(tableModel.getValueAt(i, 5).toString()));
            aotuNpc.setTime(Integer.parseInt(tableModel.getValueAt(i, 6).toString()));
            // 将对象添加到列表中
            Start.aotuNpcs.add(aotuNpc);
        }
        特殊控制台.setTwConfig();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        button1 = new JButton();
        button2 = new JButton();
        button3 = new JButton();

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

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

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null},
                },
                new String[] {
                    "\u5730\u56feID", "NPC_ID", "\u811a\u672c\u540d\u5b57", "\u9053\u5177ID", "\u9053\u5177\u6570\u91cf", "\u88c5\u5907ID", "\u95f4\u9694\u65f6\u95f4"
                }
            ));
            scrollPane1.setViewportView(table1);
        }
        add(scrollPane1);
        scrollPane1.setBounds(10, 20, 1285, scrollPane1.getPreferredSize().height);

        //---- button1 ----
        button1.setText("\u65b0\u589e");
        button1.addActionListener(e -> 新增按钮(e));
        add(button1);
        button1.setBounds(15, 465, 170, button1.getPreferredSize().height);

        //---- button2 ----
        button2.setText("\u5220\u9664");
        button2.addActionListener(e -> 删除按钮(e));
        add(button2);
        button2.setBounds(242, 465, 170, 34);

        //---- button3 ----
        button3.setText("\u4fdd\u5b58");
        button3.addActionListener(e -> 保存按钮(e));
        add(button3);
        button3.setBounds(469, 465, 170, 34);

        {
            // 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();
        tableModel.setRowCount(0);
        // 创建一个自定义的单元格渲染器，用于右对齐字符串
        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);
            }
        }

        for (AotuNpc aotuNpc : Start.aotuNpcs) {
            Object[] rowData = {aotuNpc.getMapId(), aotuNpc.getNpcId() ,aotuNpc.getScriptsName(),aotuNpc.getItemId(),aotuNpc.getItemNum(),aotuNpc.getEquId(),aotuNpc.getTime()};
            tableModel.addRow(rowData);
        }
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JScrollPane scrollPane1;
    private JTable table1;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
