import tkinter as tk
from tkinter import ttk, messagebox

from config import DEFAULT_DEVICE_IP, DEFAULT_DEVICE_PORT
from pages.device_info import DeviceInfoPage
from pages.global_params import GlobalParamsPage
from pages.port_params import PortParamsPage
from pages.upgrade import UpgradePage
from pages.status_page import StatusPage
from pages.waveform_page import WaveformPage
from tcp_client import TCPClient


class DeviceTool:
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("设备配置工具")
        self.root.geometry("800x600")

        self.tcp_client = TCPClient(DEFAULT_DEVICE_IP, DEFAULT_DEVICE_PORT)
        self.is_connected = False  # 添加连接状态标志
        self.create_widgets()

    def create_widgets(self):
        # 连接控制栏
        conn_frame = ttk.Frame(self.root)
        conn_frame.pack(fill=tk.X, padx=10, pady=5)

        ttk.Label(conn_frame, text="IP:").pack(side=tk.LEFT)
        self.ip_var = tk.StringVar(value=DEFAULT_DEVICE_IP)
        ttk.Entry(conn_frame, textvariable=self.ip_var, width=15).pack(side=tk.LEFT, padx=5)

        ttk.Label(conn_frame, text="端口:").pack(side=tk.LEFT)
        self.port_var = tk.IntVar(value=DEFAULT_DEVICE_PORT)
        ttk.Entry(conn_frame, textvariable=self.port_var, width=8).pack(side=tk.LEFT, padx=5)

        # 使用单个按钮代替连接和断开按钮
        self.conn_button = ttk.Button(conn_frame, text="连接", command=self.toggle_connection)
        self.conn_button.pack(side=tk.LEFT, padx=5)

        # 状态显示
        self.status_label = ttk.Label(conn_frame, text="状态: 未连接", foreground="red")
        self.status_label.pack(side=tk.LEFT, padx=10)

        # 选项卡
        self.notebook = ttk.Notebook(self.root)
        self.notebook.pack(fill=tk.BOTH, expand=True, padx=10, pady=5)

        # 添加页面
        self.device_info_page = DeviceInfoPage(self.notebook, self.tcp_client)
        self.notebook.add(self.device_info_page, text="设备信息")

        self.global_params_page = GlobalParamsPage(self.notebook, self.tcp_client)
        self.notebook.add(self.global_params_page, text="全局配置")

        self.port_params_page = PortParamsPage(self.notebook, self.tcp_client)
        self.notebook.add(self.port_params_page, text="通道配置")

        self.upgrade = UpgradePage(self.notebook, self.tcp_client)
        self.notebook.add(self.upgrade, text="固件升级")

        self.status_page = StatusPage(self.notebook, self.tcp_client)
        self.notebook.add(self.status_page, text="实时状态")

        self.waveform_page = WaveformPage(self.notebook, self.tcp_client)
        self.notebook.add(self.waveform_page, text="实时波形")

        # 可以继续添加其他页面...

    def toggle_connection(self):
        """切换连接状态"""
        if not self.is_connected:
            self.connect()
        else:
            self.disconnect()

    def connect(self):
        # 获取当前输入框中的 IP 和端口
        current_ip = self.ip_var.get()
        current_port = self.port_var.get()

        # 更新 TCPClient 的连接参数
        self.tcp_client.host = current_ip
        self.tcp_client.port = current_port

        if self.tcp_client.connect():
            self.is_connected = True
            self.conn_button.config(text="断开")
            self.status_label.config(text="状态: 已连接", foreground="green")
            messagebox.showinfo("成功", "连接成功")
        else:
            messagebox.showerror("错误", "连接失败")

    def disconnect(self):
        self.tcp_client.disconnect()
        self.is_connected = False
        self.conn_button.config(text="连接")
        self.status_label.config(text="状态: 未连接", foreground="red")
        messagebox.showinfo("成功", "已断开连接")

    def run(self):
        self.root.mainloop()


if __name__ == "__main__":
    app = DeviceTool()
    app.run()