/*
 * Created by JFormDesigner on Fri Apr 04 16:38:34 CST 2025
 */

package gui.UI_LenheartUI;

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

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

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



    private void 新增按钮1(ActionEvent e) {
        // TODO add your code here
            DefaultTableModel tableModel = (DefaultTableModel)this.table1.getModel();
            Object[] rowData = {tableModel.getRowCount()+1, 0 ,0};
            tableModel.addRow(rowData);
    }

    private void 删除按钮1(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 新增按钮2(ActionEvent e) {
        DefaultTableModel tableModel = (DefaultTableModel)this.table2.getModel();
        Object[] rowData = {tableModel.getRowCount()+1, 0 ,0,0,0,0,0,0};
        tableModel.addRow(rowData);
    }

    private void 删除按钮2(ActionEvent e) {
        // TODO add your code here
        int selectedRow = table2.getSelectedRow();
        if (selectedRow != -1) {
            DefaultTableModel tableModel = (DefaultTableModel) table2.getModel();
            tableModel.removeRow(selectedRow);
            if(tableModel.getRowCount() > selectedRow)table2.setRowSelectionInterval(selectedRow, selectedRow);
        }
    }

    private void 新增按钮3(ActionEvent e) {
        DefaultTableModel tableModel = (DefaultTableModel)this.table3.getModel();
        Object[] rowData = {tableModel.getRowCount()+1, 0 ,0,0};
        tableModel.addRow(rowData);
    }

    private void 删除按钮3(ActionEvent e) {
        // TODO add your code here
        int selectedRow = table3.getSelectedRow();
        if (selectedRow != -1) {
            DefaultTableModel tableModel = (DefaultTableModel) table3.getModel();
            tableModel.removeRow(selectedRow);
            if(tableModel.getRowCount() > selectedRow)table3.setRowSelectionInterval(selectedRow, selectedRow);
        }
    }


    private  void 重载配置操作(){
        List<DiaoYuMap> mapIds = Start.diaoyuConfig.getMapIds();
        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);
            }
        }

        mapIds.forEach((obj) -> {
            Integer Id = obj.getId();
            String NameFront = obj.getName();
            Integer mapid = obj.getMapId();

            Object[] rowData = {Id, NameFront ,mapid};
            tableModel.addRow(rowData);
        });


        List<DiaoyuItem> reward = Start.diaoyuConfig.getAll();
        DefaultTableModel tableMode2 = (DefaultTableModel)this.table2.getModel();
        tableMode2.setRowCount(0);
        // 遍历表格的所有列，将字符串类型的列设置为右对齐
        for (int i = 0; i < table2.getColumnCount(); i++) {
            if (table2.getColumnClass(i) == String.class) {
                table2.getColumnModel().getColumn(i).setCellRenderer(rightRenderer);
            }
        }
        reward.forEach((obj) -> {
            Integer Id = obj.getId();
            String ItemName = obj.getItemName();
            Integer ItemId = obj.getItemId();
            Integer min = obj.getMinNum();
            Integer max = obj.getMaxNum();
            Integer rate = obj.getChance();
            Integer noti = obj.getNotice();
            Integer iskd = obj.getIsAdvanced();


            Object[] rowData = {Id, ItemName ,ItemId ,min,max,rate,noti,iskd};
            tableMode2.addRow(rowData);
        });


        List<BL> bls = Start.diaoyuConfig.getBls();
        DefaultTableModel tableMode3 = (DefaultTableModel)this.table3.getModel();
        tableMode3.setRowCount(0);
        // 遍历表格的所有列，将字符串类型的列设置为右对齐
        for (int i = 0; i < table3.getColumnCount(); i++) {
            if (table3.getColumnClass(i) == String.class) {
                table3.getColumnModel().getColumn(i).setCellRenderer(rightRenderer);
            }
        }
        bls.forEach((obj) -> {
            Integer Id = obj.getId();
            String Name = obj.getName();
            Integer ItemId = obj.getItemId();
            Integer Rate = obj.getBl();

            Object[] rowData = {Id, Name ,ItemId ,Rate};
            tableMode3.addRow(rowData);
        });

        String notice = Start.diaoyuConfig.getNotice();
        textField1.setText(notice);

        textField2.setText(String.valueOf(Start.diaoyuConfig.getDyyg()));
        textField3.setText(String.valueOf(Start.diaoyuConfig.getDyyz()));
        textField4.setText(String.valueOf(Start.diaoyuConfig.getDyjg()));
        textField5.setText(String.valueOf(Start.diaoyuConfig.getPtye()));
        textField6.setText(String.valueOf(Start.diaoyuConfig.getGjye()));

        toggleButton1.setSelected(Start.diaoyuConfig.getDykg() == 1);
        toggleButton2.setSelected(Start.diaoyuConfig.getRyyz() == 1);
        toggleButton3.setSelected(Start.diaoyuConfig.getDytx() == 1);
        toggleButton4.setSelected(Start.diaoyuConfig.getKjbzdc() == 1);
        toggleButton5.setSelected(Start.diaoyuConfig.getBldj() == 1);
    }

    private void 重载配置(ActionEvent e) {
        重载配置操作();
    }

    private void button7PropertyChange(ActionEvent e) {
        // TODO add your code here
        // 保存 table1 的数据
        DefaultTableModel tableModel = (DefaultTableModel) this.table1.getModel();
        List<DiaoYuMap> mapIds = new ArrayList<>();
        for (int i = 0; i < tableModel.getRowCount(); i++) {
            Integer id = (Integer) tableModel.getValueAt(i, 0);
            String nameFront = "";
            Integer mapid = (Integer) tableModel.getValueAt(i, 2);
            DiaoYuMap diaoYuMap = new DiaoYuMap();
            diaoYuMap.setId(id);
            diaoYuMap.setName(nameFront);
            diaoYuMap.setMapId(mapid);
            mapIds.add(diaoYuMap);
        }
        Start.diaoyuConfig.setMapIds(mapIds);


        // 保存 table2 的数据
        DefaultTableModel tableMode2 = (DefaultTableModel) this.table2.getModel();
        List<DiaoyuItem> reward = new ArrayList<>();
        reward.clear();
        for (int i = 0; i < tableMode2.getRowCount(); i++) {
            Integer id = (Integer) tableMode2.getValueAt(i, 0);
            String itemName = (String) tableMode2.getValueAt(i, 1);
            Integer itemId = (Integer) tableMode2.getValueAt(i, 2);
            Integer min = (Integer) tableMode2.getValueAt(i, 3);
            Integer max = (Integer) tableMode2.getValueAt(i, 4);
            Integer rate = (Integer) tableMode2.getValueAt(i, 5);
            Integer noti = (Integer) tableMode2.getValueAt(i, 6);
            Integer iskd = (Integer) tableMode2.getValueAt(i, 7);
            DiaoyuItem diaoyuItem = new DiaoyuItem();
            diaoyuItem.setId(id);
            diaoyuItem.setItemName(itemName);
            diaoyuItem.setItemId(itemId);
            diaoyuItem.setMinNum(min);
            diaoyuItem.setMaxNum(max);
            diaoyuItem.setChance(rate);
            diaoyuItem.setNotice(noti);
            diaoyuItem.setIsAdvanced(iskd);
            reward.add(diaoyuItem);
        }
        Start.diaoyuConfig.setAll(reward);

        // 保存 table3 的数据
        DefaultTableModel tableMode3 = (DefaultTableModel) this.table3.getModel();
        List<BL> bls = new ArrayList<>();
        bls.clear();
        for (int i = 0; i < tableMode3.getRowCount(); i++) {
            Integer id = (Integer) tableMode3.getValueAt(i, 0);
            String name = (String) tableMode3.getValueAt(i, 1);
            Integer itemId = (Integer) tableMode3.getValueAt(i, 2);
            Integer rate = (Integer) tableMode3.getValueAt(i, 3);
            BL bl = new BL();
            bl.setId(id);
            bl.setName(name);
            bl.setItemId(itemId);
            bl.setBl(rate);
            bls.add(bl);
        }
        Start.diaoyuConfig.setBls(bls);

        // 保存文本框的数据
        Start.diaoyuConfig.setNotice(textField1.getText());
        Start.diaoyuConfig.setDyyg(Integer.parseInt(textField2.getText()));
        Start.diaoyuConfig.setDyyz(Integer.parseInt(textField3.getText()));
        Start.diaoyuConfig.setDyjg(Integer.parseInt(textField4.getText()));
        Start.diaoyuConfig.setPtye(Integer.parseInt(textField5.getText()));
        Start.diaoyuConfig.setGjye(Integer.parseInt(textField6.getText()));

        // 保存开关按钮的数据
        Start.diaoyuConfig.setDykg(toggleButton1.isSelected() ? 1 : 0);
        Start.diaoyuConfig.setRyyz(toggleButton2.isSelected() ? 1 : 0);
        Start.diaoyuConfig.setDytx(toggleButton3.isSelected() ? 1 : 0);
        Start.diaoyuConfig.setKjbzdc(toggleButton4.isSelected() ? 1 : 0);
        Start.diaoyuConfig.setBldj(toggleButton5.isSelected() ? 1 : 0);

        特殊控制台.setTwConfig();
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        scrollPane2 = new JScrollPane();
        table2 = new JTable();
        scrollPane3 = new JScrollPane();
        table3 = new JTable();
        button2 = new JButton();
        button1 = new JButton();
        button3 = new JButton();
        button4 = new JButton();
        textField1 = new JTextField();
        label1 = new JLabel();
        button5 = new JButton();
        button6 = new JButton();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        textField2 = new JTextField();
        textField3 = new JTextField();
        textField4 = new JTextField();
        textField5 = new JTextField();
        textField6 = new JTextField();
        toggleButton1 = new JToggleButton();
        label7 = new JLabel();
        label8 = new JLabel();
        label9 = new JLabel();
        label10 = new JLabel();
        label11 = new JLabel();
        toggleButton2 = new JToggleButton();
        toggleButton3 = new JToggleButton();
        toggleButton4 = new JToggleButton();
        toggleButton5 = new JToggleButton();
        button7 = new JButton();
        button8 = new JButton();

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

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

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                    "\u5e8f\u53f7", "\u5730\u56fe\u540d", "\u5730\u56feID"
                }
            ) {
                Class<?>[] columnTypes = new Class<?>[] {
                    Integer.class, String.class, Integer.class
                };
                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return columnTypes[columnIndex];
                }
            });
            {
                TableColumnModel cm = table1.getColumnModel();
                cm.getColumn(0).setMinWidth(10);
                cm.getColumn(0).setMaxWidth(35);
            }
            scrollPane1.setViewportView(table1);
        }
        add(scrollPane1);
        scrollPane1.setBounds(20, 10, 350, 625);

        //======== scrollPane2 ========
        {

            //---- table2 ----
            table2.setModel(new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                    "\u5e8f\u53f7", "\u9053\u5177\u540d", "\u9053\u5177ID", "\u6700\u5c0f\u6570\u91cf", "\u6700\u5927\u6570\u91cf", "\u51e0\u7387", "\u64ad\u62a5", "\u4ec5\u9ad8\u7ea7\u9c7c\u9975\u53ef\u9493"
                }
            ) {
                Class<?>[] columnTypes = new Class<?>[] {
                    Integer.class, Integer.class, Integer.class, Integer.class, Integer.class, Integer.class, Integer.class, Integer.class
                };
                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return columnTypes[columnIndex];
                }
            });
            {
                TableColumnModel cm = table2.getColumnModel();
                cm.getColumn(0).setMaxWidth(35);
                cm.getColumn(6).setMaxWidth(35);
            }
            scrollPane2.setViewportView(table2);
        }
        add(scrollPane2);
        scrollPane2.setBounds(380, 10, 685, 555);

        //======== scrollPane3 ========
        {

            //---- table3 ----
            table3.setModel(new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                    "\u5e8f\u53f7", "\u9053\u5177\u540d", "\u9053\u5177ID", "\u500d\u7387"
                }
            ) {
                Class<?>[] columnTypes = new Class<?>[] {
                    Integer.class, String.class, Integer.class, Integer.class
                };
                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return columnTypes[columnIndex];
                }
            });
            scrollPane3.setViewportView(table3);
        }
        add(scrollPane3);
        scrollPane3.setBounds(1075, 10, 425, 250);

        //---- button2 ----
        button2.setText("\u5220\u9664");
        button2.addActionListener(e -> 删除按钮1(e));
        add(button2);
        button2.setBounds(215, 645, 155, button2.getPreferredSize().height);

        //---- button1 ----
        button1.setText("\u65b0\u589e");
        button1.setActionCommand("\u65b0\u589e");
        button1.addActionListener(e -> 新增按钮1(e));
        add(button1);
        button1.setBounds(20, 645, 155, button1.getPreferredSize().height);

        //---- button3 ----
        button3.setText("\u65b0\u589e");
        button3.addActionListener(e -> 新增按钮2(e));
        add(button3);
        button3.setBounds(385, 645, 155, button3.getPreferredSize().height);

        //---- button4 ----
        button4.setText("\u5220\u9664");
        button4.addActionListener(e -> 删除按钮2(e));
        add(button4);
        button4.setBounds(615, 645, 155, button4.getPreferredSize().height);
        add(textField1);
        textField1.setBounds(385, 590, 385, 50);

        //---- label1 ----
        label1.setText("\u64ad\u62a5\u5185\u5bb9:");
        add(label1);
        label1.setBounds(new Rectangle(new Point(390, 570), label1.getPreferredSize()));

        //---- button5 ----
        button5.setText("\u65b0\u589e");
        button5.addActionListener(e -> 新增按钮3(e));
        add(button5);
        button5.setBounds(1115, 270, 155, button5.getPreferredSize().height);

        //---- button6 ----
        button6.setText("\u5220\u9664");
        button6.addActionListener(e -> 删除按钮3(e));
        add(button6);
        button6.setBounds(1295, 270, 155, button6.getPreferredSize().height);

        //---- label2 ----
        label2.setText("\u9493\u9c7c\u9c7c\u7aff");
        add(label2);
        label2.setBounds(1150, 310, 60, 25);

        //---- label3 ----
        label3.setText("\u9493\u9c7c\u6905\u5b50");
        add(label3);
        label3.setBounds(1150, 350, 60, 25);

        //---- label4 ----
        label4.setText("\u9493\u9c7c\u95f4\u9694");
        add(label4);
        label4.setBounds(1150, 390, 60, 25);

        //---- label5 ----
        label5.setText("\u666e\u901a\u9c7c\u9975");
        add(label5);
        label5.setBounds(1150, 430, 60, 25);

        //---- label6 ----
        label6.setText("\u9ad8\u7ea7\u9c7c\u9975");
        add(label6);
        label6.setBounds(1150, 470, 60, 25);
        add(textField2);
        textField2.setBounds(1210, 310, 205, textField2.getPreferredSize().height);
        add(textField3);
        textField3.setBounds(1210, 350, 205, textField3.getPreferredSize().height);
        add(textField4);
        textField4.setBounds(1210, 390, 205, textField4.getPreferredSize().height);
        add(textField5);
        textField5.setBounds(1210, 430, 205, textField5.getPreferredSize().height);
        add(textField6);
        textField6.setBounds(1210, 470, 205, textField6.getPreferredSize().height);

        //---- 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(1280, 495, 100, 35);

        //---- label7 ----
        label7.setText("\u5f00\u653e\u9493\u9c7c\u7cfb\u7edf\u5f00\u5173");
        add(label7);
        label7.setBounds(1160, 500, 110, 25);

        //---- label8 ----
        label8.setText("\u542f\u7528\u4efb\u610f\u6905\u5b50\u9493\u9c7c");
        add(label8);
        label8.setBounds(1160, 540, 110, 25);

        //---- label9 ----
        label9.setText("\u663e\u793a\u9493\u9c7c\u6210\u529f\u7279\u6548");
        add(label9);
        label9.setBounds(1160, 575, 110, 25);

        //---- label10 ----
        label10.setText("\u7a7a\u95f4\u4e0d\u8db3\u81ea\u52a8\u4e22\u51fa");
        add(label10);
        label10.setBounds(1160, 610, 110, 25);

        //---- label11 ----
        label11.setText("\u500d\u7387\u9053\u5177\u662f\u5426\u53e0\u52a0");
        add(label11);
        label11.setBounds(1160, 650, 110, 25);

        //---- 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(1280, 535, 100, 35);

        //---- 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(1280, 570, 100, 35);

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

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

        //---- button7 ----
        button7.setText("\u4fdd\u5b58\u914d\u7f6e");
        button7.addActionListener(e -> button7PropertyChange(e));
        add(button7);
        button7.setBounds(775, 585, 290, 45);

        //---- button8 ----
        button8.setText("\u91cd\u8f7d\u914d\u7f6e");
        button8.addActionListener(e -> 重载配置(e));
        add(button8);
        button8.setBounds(775, 635, 290, 45);

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

        重载配置操作();
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JScrollPane scrollPane1;
    private JTable table1;
    private JScrollPane scrollPane2;
    private JTable table2;
    private JScrollPane scrollPane3;
    private JTable table3;
    private JButton button2;
    private JButton button1;
    private JButton button3;
    private JButton button4;
    private JTextField textField1;
    private JLabel label1;
    private JButton button5;
    private JButton button6;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField4;
    private JTextField textField5;
    private JTextField textField6;
    private JToggleButton toggleButton1;
    private JLabel label7;
    private JLabel label8;
    private JLabel label9;
    private JLabel label10;
    private JLabel label11;
    private JToggleButton toggleButton2;
    private JToggleButton toggleButton3;
    private JToggleButton toggleButton4;
    private JToggleButton toggleButton5;
    private JButton button7;
    private JButton button8;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
