/*
 * Created by JFormDesigner on Tue Apr 15 19:19:53 CST 2025
 */

package gui.UI_LenheartUI;

import java.awt.event.*;
import gui.Start;
import gui.特殊控制台;
import server.BossInMap;

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

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

    private void 新增(ActionEvent e) {
        // TODO add your code here
        DefaultTableModel tableModel = (DefaultTableModel)this.table1.getModel();
        Object[] rowData = {0,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();
            int id = Integer.valueOf(tableModel.getValueAt(selectedRow, 2).toString());
            Start.diaoLuoTiShis.remove(id);
            tableModel.removeRow(selectedRow);
            if(tableModel.getRowCount() > selectedRow)table1.setRowSelectionInterval(selectedRow, selectedRow);
        }
    }

    private void 保存配置(ActionEvent e) {
        // TODO add your code here
        DefaultTableModel tableModel = (DefaultTableModel) table1.getModel();
        int rowCount = tableModel.getRowCount();
        Start.野外boss刷新.clear();
        for (int i = 0; i < rowCount; i++) {
            BossInMap bossInMap = new BossInMap((int) tableModel.getValueAt(i, 0),
                    (int) tableModel.getValueAt(i, 2),
                    (int) tableModel.getValueAt(i, 3),
                    (int) tableModel.getValueAt(i, 4),
                    tableModel.getValueAt(i, 5).toString(),
                    (int) tableModel.getValueAt(i, 6));
            bossInMap.setBosshp(tableModel.getValueAt(i, 1).toString());
            Start.野外boss刷新.add(bossInMap);
        }
        特殊控制台.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[] {
                    "\u91ce\u5916BOSS", "\u8840\u91cf(\u4ebf)", "\u5730\u56fe\u4ee3\u7801", "X\u5750\u6807", "Y\u5750\u6807", "\u8bf4\u660e", "\u5237\u65b0\u95f4\u9694/\u5206"
                }
            ));
            scrollPane1.setViewportView(table1);
        }
        add(scrollPane1);
        scrollPane1.setBounds(15, 10, 1245, 575);

        //---- button1 ----
        button1.setText("\u65b0\u589e");
        button1.addActionListener(e -> 新增(e));
        add(button1);
        button1.setBounds(145, 615, 175, 50);

        //---- button2 ----
        button2.setText("\u5220\u9664");
        button2.addActionListener(e -> 删除(e));
        add(button2);
        button2.setBounds(487, 615, 175, 50);

        //---- button3 ----
        button3.setText("\u4fdd\u5b58\u914d\u7f6e");
        button3.addActionListener(e -> 保存配置(e));
        add(button3);
        button3.setBounds(829, 615, 175, 50);

        {
            // 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 (BossInMap bossInMap : Start.野外boss刷新) {
            Object[] rowData = {bossInMap.getMobid(),bossInMap.getBosshp(),bossInMap.getMap(),bossInMap.getX(),bossInMap.getY(),bossInMap.getMsg(),bossInMap.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
}
