# coding:utf-8
import os
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
import configparser
from core.main_ui import MainWindow
import config as sdConstants
from core.qmt import qmtv

class ConfigWindow:
    def __init__(self, root):
        self.root = root
        self.root.title("系统配置")
        self.root.geometry("500x250")
        self.root.resizable(False, False)
        
        # 居中显示
        self.root.withdraw()  # 先隐藏窗口
        self.root.update_idletasks()
        x = (self.root.winfo_screenwidth() // 2) - (500 // 2)
        y = (self.root.winfo_screenheight() // 2) - (250 // 2)
        self.root.geometry(f"500x250+{x}+{y}")
        self.root.deiconify()  # 再显示窗口
        
        self.miniQMTPath = tk.StringVar()
        self.account_no = tk.StringVar()
        
        self.create_widgets()
    
    def create_widgets(self):
        # 创建主框架
        main_frame = ttk.Frame(self.root, padding="20")
        main_frame.pack(fill=tk.BOTH, expand=True)
        
        # miniQMT路径配置
        path_frame = ttk.Frame(main_frame)
        path_frame.pack(fill=tk.X, pady=5)
        
        path_label = ttk.Label(path_frame, text="miniQMT路径:")
        path_label.pack(side=tk.LEFT)
        
        path_entry = ttk.Entry(path_frame, textvariable=self.miniQMTPath, width=40)
        path_entry.pack(side=tk.LEFT, padx=(10, 5), fill=tk.X, expand=True)
        
        browse_btn = ttk.Button(path_frame, text="浏览", command=self.browse_folder)
        browse_btn.pack(side=tk.LEFT)
        
        # 资金账号配置
        account_frame = ttk.Frame(main_frame)
        account_frame.pack(fill=tk.X, pady=5)
        
        account_label = ttk.Label(account_frame, text="资金账号:")
        account_label.pack(side=tk.LEFT)
        
        account_entry = ttk.Entry(account_frame, textvariable=self.account_no, width=40)
        account_entry.pack(side=tk.LEFT, padx=(10, 0))
        
        # 说明文本
        info_label = ttk.Label(
            main_frame, 
            text="请配置miniQMT的userdata_mini路径和资金账号\n路径示例: D:/Programs/DTQMT/userdata_mini",
            foreground="gray"
        )
        info_label.pack(pady=10)
        
        # 按钮框架
        button_frame = ttk.Frame(main_frame)
        button_frame.pack(fill=tk.X, pady=10)
        
        save_btn = ttk.Button(button_frame, text="保存配置", command=self.save_config)
        save_btn.pack(side=tk.RIGHT)
        
        cancel_btn = ttk.Button(button_frame, text="取消", command=self.root.destroy)
        cancel_btn.pack(side=tk.RIGHT, padx=(0, 10))
    
    def browse_folder(self):
        folder_selected = filedialog.askdirectory()
        if folder_selected:
            self.miniQMTPath.set(folder_selected)
    
    def save_config(self):
        mini_qmt_path = self.miniQMTPath.get().strip()
        account_number = self.account_no.get().strip()
        
        # 检查miniQMT路径
        if not mini_qmt_path:
            messagebox.showerror("错误", "请选择miniQMT路径")
            return
            
        if not os.path.exists(mini_qmt_path):
            messagebox.showerror("错误", "miniQMT路径不存在")
            return
        
        # 检查账号
        if not account_number:
            messagebox.showerror("错误", "请输入资金账号")
            return
        
        # 保存配置
        config = configparser.ConfigParser()
        config['config'] = {
            'miniQMTPath': mini_qmt_path.replace('\\', '/'),
            'account_no': account_number,
            'log_level': 'INFO'
        }
        
        config_path = sdConstants.get_config_path()
        try:
            with open(config_path, 'w') as configfile:
                config.write(configfile)
            messagebox.showinfo("成功", "配置已保存")
            self.root.destroy()
        except Exception as e:
            messagebox.showerror("错误", f"保存配置失败: {str(e)}")

def check_and_create_config():
    """检查配置文件，如果不存在则打开配置窗口"""
    root = tk.Tk()
    config_window = ConfigWindow(root)
    root.mainloop()

def initialize_system():
    """初始化系统"""
    
    try:
        while True:
            # 初始化配置
            if sdConstants.exist_config() and sdConstants.initConfig():
                # 初始化qmtv
                qmtv.init_qmtv()
                connected = qmtv.connect()
                if connected:
                    # 连接成功，启动主窗口
                    window = MainWindow()
                    window.run()
                    break
                else:
                    option = messagebox.askokcancel("连接失败", "QMT连接失败，请检查")
                    if option:
                        check_and_create_config()
                    else:
                        break
            else:
                option = messagebox.askokcancel("错误", "请检查配置")
                if option:
                        check_and_create_config()
                else:
                    break
    except Exception as e:
        messagebox.showerror("错误", f"系统初始化失败: {str(e)}")

if __name__ == '__main__':
    initialize_system()