/*
 * Created by JFormDesigner on Wed Mar 12 14:50:47 CST 2025
 */

package gui.UI_LenheartUI;

import java.util.*;
import gui.Start;
import gui.tw.BlConfig;
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 DropControl extends JPanel {
    public DropControl() {
        initComponents();
    }


    private void 保存配置(ActionEvent e) {
        // TODO add your code here

        Start.blkg = this.toggleButton1.isSelected() ? 1 : 0;
        Start.bldj = this.toggleButton2.isSelected() ? 1 : 0;
        Start.bskg = this.toggleButton3.isSelected() ? 1 : 0;

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

            Integer ItemId = (int) tableModel.getValueAt(i, 0);
            Integer GoldRate = (int) tableModel.getValueAt(i, 1);
            Integer ItemRate = (int) tableModel.getValueAt(i, 2);
            String Time =  tableModel.getValueAt(i, 3) + "-" + tableModel.getValueAt(i, 4);
            Integer IsStack = (int) tableModel.getValueAt(i, 5);

            BlConfig buffer = new BlConfig();
            buffer.setItem_id(ItemId);
            buffer.setGold_rate(GoldRate);
            buffer.setItem_rate(ItemRate);
            buffer.setTime_range(Time);
            buffer.setStackable(IsStack);
            Buf.add(buffer);
        }
        Start.BLlist = Buf;
        Start.bsConfig.setItem_id(Integer.valueOf(this.textField1.getText()));
        Start.bsConfig.setRate(Integer.valueOf(this.textField2.getText()));
        特殊控制台.setTwConfig();
    }

    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 initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        toggleButton1 = new JToggleButton();
        toggleButton2 = new JToggleButton();
        label1 = new JLabel();
        label2 = new JLabel();
        button19 = new JButton();
        button20 = new JButton();
        button21 = new JButton();
        textField1 = new JTextField();
        textField2 = new JTextField();
        toggleButton3 = new JToggleButton();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();

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

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

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                    "\u9053\u5177\u4ee3\u7801", "\u91d1\u5e01\u7206\u7387", "\u7269\u54c1\u7206\u7387", "\u5f00\u59cb\u65f6\u95f4", "\u7ed3\u675f\u65f6\u95f4", "\u5355\u7269\u54c1\u6570\u91cf\u5806\u53e0"
                }
            ) {
                Class<?>[] columnTypes = new Class<?>[] {
                    Integer.class, Integer.class, Integer.class, String.class, String.class, Integer.class
                };
                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return columnTypes[columnIndex];
                }
            });
            scrollPane1.setViewportView(table1);
        }
        add(scrollPane1);
        scrollPane1.setBounds(18, 10, 1130, 550);

        //---- toggleButton1 ----
        toggleButton1.setIcon(new ImageIcon(getClass().getResource("/image/OFF3.png")));
        toggleButton1.setSelectedIcon(new ImageIcon(getClass().getResource("/image/ON3.png")));
        toggleButton1.setFocusPainted(false);
        toggleButton1.setContentAreaFilled(false);
        add(toggleButton1);
        toggleButton1.setBounds(15, 565, 100, 35);

        //---- toggleButton2 ----
        toggleButton2.setIcon(new ImageIcon(getClass().getResource("/image/OFF3.png")));
        toggleButton2.setSelectedIcon(new ImageIcon(getClass().getResource("/image/ON3.png")));
        toggleButton2.setFocusPainted(false);
        toggleButton2.setContentAreaFilled(false);
        add(toggleButton2);
        toggleButton2.setBounds(15, 605, 100, 35);

        //---- label1 ----
        label1.setText("\u591a\u500d\u7206\u7387\u5f00\u5173");
        add(label1);
        label1.setBounds(130, 570, 185, 30);

        //---- label2 ----
        label2.setText("\u591a\u7269\u54c1\u7206\u7387\u662f\u5426\u53e0\u52a0");
        add(label2);
        label2.setBounds(130, 605, 185, 30);

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

        //---- button20 ----
        button20.setText("\u65b0\u589e\u914d\u7f6e");
        button20.addActionListener(e -> 新增配置(e));
        add(button20);
        button20.setBounds(825, 570, 160, 35);

        //---- button21 ----
        button21.setText("\u5220\u9664\u914d\u7f6e");
        button21.addActionListener(e -> 删除配置(e));
        add(button21);
        button21.setBounds(990, 570, 160, 35);
        add(textField1);
        textField1.setBounds(1220, 22, 150, textField1.getPreferredSize().height);
        add(textField2);
        textField2.setBounds(1220, 57, 150, textField2.getPreferredSize().height);

        //---- 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);
        add(toggleButton3);
        toggleButton3.setBounds(1150, 95, 100, 35);

        //---- label3 ----
        label3.setText("\u97ad\u5c38\u5f00\u5173");
        add(label3);
        label3.setBounds(1255, 100, 185, 30);

        //---- label4 ----
        label4.setText("\u9053\u5177\u4ee3\u7801");
        add(label4);
        label4.setBounds(1165, 20, 55, 30);

        //---- label5 ----
        label5.setText("\u97ad\u5c38\u51e0\u7387");
        add(label5);
        label5.setBounds(1165, 55, 88, 30);

        {
            // 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.toggleButton1.setSelected(Start.blkg == 1 ? true : false);
        this.toggleButton2.setSelected(Start.bldj == 1 ? true : false);
        this.toggleButton3.setSelected(Start.bskg == 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.BLlist.forEach((obj) -> {
            Integer ItemId = obj.getItem_id();
            Integer GoldRate = obj.getGold_rate();
            Integer ItemRate = obj.getItem_rate();
            String Time = obj.getTime_range();
            Integer IsStack = obj.getStackableNum();

            String[] split = Time.split("-");
            Object[] rowData = {ItemId, GoldRate ,ItemRate,split[0],split[1], IsStack};
            tableModel.addRow(rowData);
        });

        int itemId = Start.bsConfig.getItem_id();
        int rate = Start.bsConfig.getRate();
        this.textField1.setText(String.valueOf(itemId));
        this.textField2.setText(String.valueOf(rate));
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JScrollPane scrollPane1;
    private JTable table1;
    private JToggleButton toggleButton1;
    private JToggleButton toggleButton2;
    private JLabel label1;
    private JLabel label2;
    private JButton button19;
    private JButton button20;
    private JButton button21;
    private JTextField textField1;
    private JTextField textField2;
    private JToggleButton toggleButton3;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
