/*
 * Created by JFormDesigner on Tue Apr 15 18:46:50 CST 2025
 */

package gui.UI_LenheartUI;

import java.awt.event.*;

import client.MapleCharacter;
import gui.Start;
import gui.tw.DiaoLuoTiShi;
import gui.特殊控制台;
import handling.channel.ChannelServer;
import handling.world.World;
import tools.MaplePacketCreator;

import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.table.*;

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

    private void 新增(ActionEvent e) {
        // TODO add your code here
        int Id = Integer.valueOf(textField1.getText());
        DiaoLuoTiShi diaoLuoTiShi = new DiaoLuoTiShi(0, Id);
        Start.diaoLuoTiShis.put(Id,diaoLuoTiShi);
        ReData();
//        Integer.valueOf(textField1.getText())
    }

    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
        Start.diaoLuoGongGao = textField3.getText();
        特殊控制台.setTwConfig();
    }

    private void 发起公告(final int type) {
        for (final ChannelServer cserv1 : ChannelServer.getAllInstances()) {
            for (final MapleCharacter mch : cserv1.getPlayerStorage().getAllCharacters()) {
                switch (type) {
                    case 0: {
                        World.Broadcast.broadcastMessage(MaplePacketCreator.serverMessage(this.textField2.getText()));
                        continue;
                    }
                    case 1: {
                        World.Broadcast.broadcastMessage(MaplePacketCreator.serverNotice(1, this.textField2.getText()));
                        continue;
                    }
                    case 2: {
                        mch.startMapEffect(this.textField2.getText(), 5120015);
                        continue;
                    }
                    case 3: {
                        World.Broadcast.broadcastMessage(MaplePacketCreator.serverNotice(6, this.textField2.getText()));
                        continue;
                    }
                    case 4: {
                        World.Broadcast.broadcastMessage(MaplePacketCreator.serverMessage(this.textField2.getText()));
                        World.Broadcast.broadcastMessage(MaplePacketCreator.serverNotice(1, this.textField2.getText()));
                        World.Broadcast.broadcastMessage(MaplePacketCreator.serverNotice(6, this.textField2.getText()));
                        mch.startMapEffect(this.textField2.getText(), 5120015);
                        continue;
                    }
                }
            }
        }
    }

    private void 顶端滚动公告(ActionEvent e) {
        // TODO add your code here
        发起公告(0);
    }

    private void 中心弹窗公告(ActionEvent e) {
        // TODO add your code here
        发起公告(1);
    }

    private void 中心气泡公告(ActionEvent e) {
        // TODO add your code here
        发起公告(2);
    }

    private void 聊天蓝色公告(ActionEvent e) {
        // TODO add your code here
        发起公告(3);
    }

    private void 发放全部公告(ActionEvent e) {
        // TODO add your code here
        发起公告(4);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        button1 = new JButton();
        textField1 = new JTextField();
        label1 = new JLabel();
        button2 = new JButton();
        button3 = new JButton();
        textField2 = new JTextField();
        label2 = new JLabel();
        button4 = new JButton();
        button5 = new JButton();
        button6 = new JButton();
        button7 = new JButton();
        button8 = new JButton();
        textField3 = new JTextField();
        label3 = new JLabel();

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

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

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null},
                    {null, null, null},
                },
                new String[] {
                    "\u5e8f\u53f7", "\u9053\u5177\u540d", "\u9053\u5177ID"
                }
            ));
            scrollPane1.setViewportView(table1);
        }
        add(scrollPane1);
        scrollPane1.setBounds(new Rectangle(new Point(25, 20), scrollPane1.getPreferredSize()));

        //---- button1 ----
        button1.setText("\u65b0\u589e");
        button1.addActionListener(e -> 新增(e));
        add(button1);
        button1.setBounds(225, 465, 95, 35);
        add(textField1);
        textField1.setBounds(115, 465, 110, 35);

        //---- label1 ----
        label1.setText("\u9053\u5177ID");
        add(label1);
        label1.setBounds(45, 465, 110, 35);

        //---- button2 ----
        button2.setText("\u5220\u9664");
        button2.addActionListener(e -> 删除(e));
        add(button2);
        button2.setBounds(350, 465, 95, 35);

        //---- button3 ----
        button3.setText("\u4fdd\u5b58\u914d\u7f6e");
        button3.addActionListener(e -> 保存配置(e));
        add(button3);
        button3.setBounds(335, 505, 130, 55);
        add(textField2);
        textField2.setBounds(505, 55, 575, 160);

        //---- label2 ----
        label2.setText("\u5168\u670d\u516c\u544a");
        add(label2);
        label2.setBounds(515, 20, 120, 30);

        //---- button4 ----
        button4.setText("\u9876\u7aef\u6eda\u52a8");
        button4.addActionListener(e -> 顶端滚动公告(e));
        add(button4);
        button4.setBounds(505, 225, 85, button4.getPreferredSize().height);

        //---- button5 ----
        button5.setText("\u4e2d\u5fc3\u5f39\u7a97");
        button5.addActionListener(e -> 中心弹窗公告(e));
        add(button5);
        button5.setBounds(630, 225, 85, 34);

        //---- button6 ----
        button6.setText("\u4e2d\u5fc3\u6c14\u6ce1");
        button6.addActionListener(e -> 中心气泡公告(e));
        add(button6);
        button6.setBounds(750, 225, 85, 34);

        //---- button7 ----
        button7.setText("\u804a\u5929\u84dd\u8272");
        button7.addActionListener(e -> 聊天蓝色公告(e));
        add(button7);
        button7.setBounds(870, 225, 85, 34);

        //---- button8 ----
        button8.setText("\u53d1\u653e\u5168\u90e8");
        button8.addActionListener(e -> 发放全部公告(e));
        add(button8);
        button8.setBounds(990, 225, 85, 34);
        add(textField3);
        textField3.setBounds(95, 510, 240, 70);

        //---- label3 ----
        label3.setText("\u64ad\u62a5\u5185\u5bb9");
        add(label3);
        label3.setBounds(new Rectangle(new Point(45, 530), label3.getPreferredSize()));

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


        ReData();
        textField3.setText(Start.diaoLuoGongGao);
    }

    private void ReData(){
        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);
            }
        }

        int xh = 1;
        for (DiaoLuoTiShi value : Start.diaoLuoTiShis.values()) {
            Object[] rowData = {xh,value.getName(),value.getItemId()};
            tableModel.addRow(rowData);
            xh++;
        }

    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JScrollPane scrollPane1;
    private JTable table1;
    private JButton button1;
    private JTextField textField1;
    private JLabel label1;
    private JButton button2;
    private JButton button3;
    private JTextField textField2;
    private JLabel label2;
    private JButton button4;
    private JButton button5;
    private JButton button6;
    private JButton button7;
    private JButton button8;
    private JTextField textField3;
    private JLabel label3;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
