/*
 * Created by JFormDesigner on Mon Mar 03 20:47:31 CST 2025
 */

package gui.UI_LenheartUI;

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

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

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

    private void 删除按钮(ActionEvent e) {
        int selectedRow = ItemTable.getSelectedRow();
        if (selectedRow != -1) {
            DefaultTableModel tableModel = (DefaultTableModel) ItemTable.getModel();
            tableModel.removeRow(selectedRow);
            if(tableModel.getRowCount() > selectedRow)ItemTable.setRowSelectionInterval(selectedRow, selectedRow);
        }
    }

    private void 保存配置(ActionEvent e) {
        DefaultTableModel tableModel = (DefaultTableModel) ItemTable.getModel();
        int rowCount = tableModel.getRowCount();
        int columnCount = tableModel.getColumnCount();

        Map<String, List<Integer>> ConfigItemJson = new HashMap<>();
        for (int i = 0; i < rowCount; i++) {
            String Key = tableModel.getValueAt(i, 2).toString();
            Integer A = (int) tableModel.getValueAt(i, 0);
            Integer B = (int) tableModel.getValueAt(i, 3);
            Integer C = (int) tableModel.getValueAt(i, 1);

            List<Integer> Data = new ArrayList<>();
            Data.add(B);
            Data.add(A);
            Data.add(C);
            ConfigItemJson.put(Key,Data);
        }

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

    private void 新增按钮(ActionEvent e) {
        DefaultTableModel tableModel = (DefaultTableModel) ItemTable.getModel();
        Object[] rowData = {0, 0, 0,0};
        tableModel.addRow(rowData);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        scrollPane1 = new JScrollPane();
        ItemTable = new JTable();
        删除按钮 = new JButton();
        保存配置 = new JButton();
        新增按钮 = new JButton();

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

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

            //---- ItemTable ----
            ItemTable.setModel(new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                    "\u7269\u54c1\u5151\u6362\u4ee3\u7801", "\u7269\u54c1\u5151\u6362\u6570\u91cf", "\u6240\u9700\u7269\u54c1\u4ee3\u7801", "\u6240\u9700\u7269\u54c1\u6570\u91cf"
                }
            ) {
                Class<?>[] columnTypes = new Class<?>[] {
                    Integer.class, Integer.class, Integer.class, Integer.class
                };
                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return columnTypes[columnIndex];
                }
            });
            ItemTable.setAutoCreateRowSorter(true);
            ItemTable.setBorder(null);
            scrollPane1.setViewportView(ItemTable);
        }
        add(scrollPane1);
        scrollPane1.setBounds(18, 10, 447, 660);

        //---- 删除按钮 ----
        删除按钮.setText("\u5220\u9664");
        删除按钮.addActionListener(e -> 删除按钮(e));
        add(删除按钮);
        删除按钮.setBounds(485, 35, 115, 删除按钮.getPreferredSize().height);

        //---- 保存配置 ----
        保存配置.setText("\u4fdd\u5b58\u914d\u7f6e");
        保存配置.setActionCommand("\u4fdd\u5b58\u914d\u7f6e");
        保存配置.addActionListener(e -> 保存配置(e));
        add(保存配置);
        保存配置.setBounds(485, 125, 115, 34);

        //---- 新增按钮 ----
        新增按钮.setText("\u65b0\u589e");
        新增按钮.setActionCommand("\u65b0\u589e\u6309\u94ae");
        新增按钮.addActionListener(e -> 新增按钮(e));
        add(新增按钮);
        新增按钮.setBounds(485, 80, 115, 34);

        setPreferredSize(new Dimension(1640, 695));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on

/*        ItemTable.addColumn(new TableColumn(0));*/
        Start.ConfigItemJson.forEach((k, v) -> {
            Integer NeedItemId = Integer.valueOf(k);
            Integer ItemId = v.get(1);
            Integer NeedCount = v.get(0);
            Integer GiveCount = v.get(2);

            DefaultTableModel tableModel = (DefaultTableModel) ItemTable.getModel();
            Object[] rowData = {ItemId, GiveCount ,NeedItemId, NeedCount};
            tableModel.addRow(rowData);
        });



    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JScrollPane scrollPane1;
    private JTable ItemTable;
    private JButton 删除按钮;
    private JButton 保存配置;
    private JButton 新增按钮;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
