import tkinter as tk
from tkinter import ttk
from core.logger import LogLevel, LogData, PrintLog
from core.sfgrid.sfgrid_ui import TradeTargetUI

# 检测运行环境，决定使用真实或模拟 QMT
def get_qmt_module():
    try:
        # 尝试导入真实 QMT，如果失败则使用模拟
        from core.qmt import qmtv
        return qmtv
    except ImportError:
        from core.qmt_dummy import qmtv
        return qmtv

qmtv = get_qmt_module()

from core.eventbus import EventPrintLog
from core.eventbus import event_bus as eBus


class MainWindow:
    def __init__(self, configLogLevel:str):
        self.root = tk.Tk()
        self.root.title("神之一手 - 交易系统")
        self.root.geometry("1400x700")

        self.logLevel = LogLevel[configLogLevel]
        PrintLog(LogLevel.DEBUG, f"系统启动成功 {self.logLevel.name}")
        # 存储各个Frame的引用
        self.strategy_frames = {}
        # 日志面板可见性标志
        self.log_visible = False
        self.create_ui()

        eBus.subscribe(EventPrintLog, self.on_log_event)
    
    
    def create_ui(self):
        """创建UI界面"""
        # 主容器
        main_container = ttk.Frame(self.root)
        main_container.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)

        # 中间主体区域
        content_area = ttk.Frame(main_container)
        content_area.pack(fill=tk.BOTH, expand=True)

        # 右侧内容区域容器
        self.content_container = ttk.Frame(content_area)
        self.content_container.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

        # 创建策略Frame
        strategy_names = ["网格"]
        self.create_strategy_frames(strategy_names)

        # 创建全局日志面板（默认隐藏）
        self.create_global_log_panel(main_container)

        # 默认显示第一个策略
        self.show_strategy_frame(0)
    
    def create_global_log_panel(self, parent):
        """创建全局日志面板"""
        # 日志区域(默认隐藏)
        self.log_frame = ttk.LabelFrame(parent, text="操作日志", padding=10)
        # 默认不显示,通过工具栏按钮控制
        
        # 创建日志表格
        columns = ("timestamp", "level", "message")
        
        self.log_table = ttk.Treeview(self.log_frame, columns=columns, show='headings', height=8)
        
        log_column_configs = {
            "timestamp": ("时间", 100),
            "level": ("级别", 50),
            "message": ("消息", 1150)  # 调整宽度适应全局布局
        }
        
        for col in columns:
            title, width = log_column_configs[col]
            self.log_table.heading(col, text=title)
            self.log_table.column(col, width=width, anchor=tk.W)
        
        # 添加初始日志
        from datetime import datetime
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        self.log_table.insert('', tk.END, values=(timestamp, "INFO", "系统启动成功"))
        
        # 滚动条
        scrollbar = ttk.Scrollbar(self.log_frame, orient=tk.VERTICAL, command=self.log_table.yview)
        self.log_table.configure(yscrollcommand=scrollbar.set)
        
        self.log_table.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
        scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

    def on_log_event(self, event:LogData):
        if self.logLevel.value <= event.level.value:
            self.add_log(event.level, event.message)

    
    def add_log(self, level:LogLevel, message):
        """添加日志记录 - 全局方法"""
        from datetime import datetime
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        self.log_table.insert('', 0, values=(timestamp, level.name, message))
    
    def clear_logs(self):
        """清空日志记录"""
        # 删除所有日志项
        for item in self.log_table.get_children():
            self.log_table.delete(item)
    
    def create_strategy_frames(self, strategy_names):
        """创建各个策略的Frame"""
        frame = TradeTargetUI(self.content_container)
        self.strategy_frames[0] = frame

    def show_strategy_frame(self, index):
        """显示策略Frame"""
        if index in self.strategy_frames:
            self.strategy_frames[index].pack(fill=tk.BOTH, expand=True)

    def toggle_log_panel(self):
        """切换日志面板的显示/隐藏"""
        if self.log_visible:
            self.log_frame.pack_forget()
            self.log_visible = False
        else:
            self.log_frame.pack(side=tk.BOTTOM, fill=tk.X, pady=(5, 0))
            self.log_visible = True
    
    def on_exit(self):
        """退出程序"""
        from tkinter import messagebox
        result = messagebox.askyesno("确认退出", "确定要退出系统吗？")
        if result:
            self.root.destroy()

    def run(self):
        """运行程序"""
        self.root.mainloop()