#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""系统设置对话框模块"""

import os
import tkinter as tk
from tkinter import messagebox, filedialog, ttk

from app.config.settings import ConfigManager

from app.ui.user_settings import load_user_settings, save_user_settings
from app.ui.ui_widgets import center_window
from app.core.utils.dialog_utils import show_cloud_sync_dialog


def show_config_dialog(root, cfg: ConfigManager):

    settings = load_user_settings()
    dlg = tk.Toplevel(root)
    dlg.title("系统设置")
    dlg.geometry("700x460")
    center_window(dlg)

    content = ttk.Frame(dlg)
    content.pack(fill=tk.BOTH, expand=True, padx=12, pady=12)
    content.columnconfigure(0, weight=1)

    # ── 辅助函数 ──
    def _add_pair(parent, row, col, label_text, widget, label_width=None):
        """在 parent 的 (row, col*2) 放 label, (row, col*2+1) 放 widget"""
        lbl = ttk.Label(parent, text=label_text)
        if label_width:
            lbl.configure(width=label_width)
        lbl.grid(row=row, column=col * 2, sticky='w', padx=(6, 2), pady=3)
        widget.grid(row=row, column=col * 2 + 1, sticky='ew', padx=(2, 6), pady=3)

    def _make_dir_widget(parent, var, label):
        f = ttk.Frame(parent)
        e = ttk.Entry(f, textvariable=var)
        e.pack(side=tk.LEFT, fill=tk.X, expand=True)

        def _select_dir():
            d = filedialog.askdirectory(title=f"选择{label}")
            if d:
                try:
                    var.set(os.path.relpath(d, os.getcwd()))
                except Exception:
                    var.set(d)

        ttk.Button(f, text="选择", command=_select_dir).pack(side=tk.LEFT, padx=4)
        return f

    # ── 当前值 ──
    log_level_val = tk.StringVar(value=settings.get('log_level', 'INFO'))
    max_workers_val = tk.StringVar(value=str(settings.get('concurrency_max_workers', cfg.getint('Performance', 'max_workers', 4))))
    batch_size_val = tk.StringVar(value=str(settings.get('concurrency_batch_size', cfg.getint('Performance', 'batch_size', 5))))
    template_path_val = tk.StringVar(value=settings.get('template_path', os.path.join(cfg.get('Paths', 'template_folder', 'templates'), cfg.get('Templates', 'purchase_order', '银豹-采购单模板.xls'))))
    input_dir_val = tk.StringVar(value=settings.get('input_folder', cfg.get('Paths', 'input_folder', 'data/input')))
    output_dir_val = tk.StringVar(value=settings.get('output_folder', cfg.get('Paths', 'output_folder', 'data/output')))
    result_dir_val = tk.StringVar(value=settings.get('result_folder', 'data/result'))
    api_key_val = tk.StringVar(value=settings.get('api_key', cfg.get('API', 'api_key', '')))
    secret_key_val = tk.StringVar(value=settings.get('secret_key', cfg.get('API', 'secret_key', '')))
    timeout_val = tk.StringVar(value=str(settings.get('timeout', cfg.getint('API', 'timeout', 30))))
    max_retries_val = tk.StringVar(value=str(settings.get('max_retries', cfg.getint('API', 'max_retries', 3))))
    retry_delay_val = tk.StringVar(value=str(settings.get('retry_delay', cfg.getint('API', 'retry_delay', 2))))
    api_url_val = tk.StringVar(value=settings.get('api_url', cfg.get('API', 'api_url', '')))
    gitea_url_val = tk.StringVar(value=cfg.get('Gitea', 'base_url', fallback='https://gitea.94kan.cn'))
    gitea_owner_val = tk.StringVar(value=cfg.get('Gitea', 'owner', fallback='houhuan'))
    gitea_repo_val = tk.StringVar(value=cfg.get('Gitea', 'repo', fallback='yixuan-sync-data'))
    gitea_token_val = tk.StringVar(value=cfg.get('Gitea', 'token', fallback=''))

    # ═══════════════════════════════════════════════════
    #  区块 1: 基本设置
    # ═══════════════════════════════════════════════════
    f1 = ttk.LabelFrame(content, text=" 基本设置 ", padding=(8, 4))
    f1.pack(fill=tk.X, pady=(0, 6))
    for c in range(4):
        f1.columnconfigure(c, weight=1 if c % 2 == 1 else 0)

    lvl = ttk.Combobox(f1, textvariable=log_level_val, values=['DEBUG', 'INFO', 'WARNING', 'ERROR'], state='readonly', width=12)
    _add_pair(f1, 0, 0, "日志级别", lvl)
    _add_pair(f1, 0, 1, "最大并发", ttk.Entry(f1, textvariable=max_workers_val, width=6))
    _add_pair(f1, 1, 0, "批次大小", ttk.Entry(f1, textvariable=batch_size_val, width=6))

    # 模板路径（带选择按钮，占右列）
    tpl_frame = ttk.Frame(f1)
    tpl_entry = ttk.Entry(tpl_frame, textvariable=template_path_val)
    tpl_entry.pack(side=tk.LEFT, fill=tk.X, expand=True)

    def _select_template():
        p = filedialog.askopenfilename(title="选择模板文件", filetypes=[("Excel模板", "*.xls *.xlsx"), ("所有文件", "*.*")])
        if p:
            try:
                template_path_val.set(os.path.relpath(p, os.getcwd()))
            except Exception:
                template_path_val.set(p)

    ttk.Button(tpl_frame, text="选择", command=_select_template).pack(side=tk.LEFT, padx=4)
    _add_pair(f1, 1, 1, "采购模板", tpl_frame)

    _add_pair(f1, 2, 0, "输入目录", _make_dir_widget(f1, input_dir_val, "输入目录"))
    _add_pair(f1, 2, 1, "输出目录", _make_dir_widget(f1, output_dir_val, "输出目录"))
    _add_pair(f1, 3, 0, "结果目录", _make_dir_widget(f1, result_dir_val, "结果目录"))

    # ═══════════════════════════════════════════════════
    #  区块 2: API 设置
    # ═══════════════════════════════════════════════════
    f2 = ttk.LabelFrame(content, text=" API 设置 ", padding=(8, 4))
    f2.pack(fill=tk.X, pady=(0, 6))
    for c in range(4):
        f2.columnconfigure(c, weight=1 if c % 2 == 1 else 0)

    _add_pair(f2, 0, 0, "API Key", ttk.Entry(f2, textvariable=api_key_val))
    secret_entry = ttk.Entry(f2, textvariable=secret_key_val, show='*')
    _add_pair(f2, 0, 1, "Secret Key", secret_entry)
    _add_pair(f2, 1, 0, "Timeout", ttk.Entry(f2, textvariable=timeout_val, width=6))
    _add_pair(f2, 1, 1, "Max Retries", ttk.Entry(f2, textvariable=max_retries_val, width=6))
    _add_pair(f2, 2, 0, "Retry Delay", ttk.Entry(f2, textvariable=retry_delay_val, width=6))
    _add_pair(f2, 2, 1, "API URL", ttk.Entry(f2, textvariable=api_url_val))

    # ═══════════════════════════════════════════════════
    #  区块 3: 云端同步 (Gitea)
    # ═══════════════════════════════════════════════════
    f3 = ttk.LabelFrame(content, text=" 云端同步 (Gitea) ", padding=(8, 4))
    f3.pack(fill=tk.X, pady=(0, 8))
    for c in range(4):
        f3.columnconfigure(c, weight=1 if c % 2 == 1 else 0)

    _add_pair(f3, 0, 0, "Gitea 地址", ttk.Entry(f3, textvariable=gitea_url_val))
    _add_pair(f3, 0, 1, "仓库所有者", ttk.Entry(f3, textvariable=gitea_owner_val))
    _add_pair(f3, 1, 0, "仓库名称", ttk.Entry(f3, textvariable=gitea_repo_val))
    _add_pair(f3, 1, 1, "Access Token", ttk.Entry(f3, textvariable=gitea_token_val, show='*'))

    # ═══════════════════════════════════════════════════
    #  按钮区
    # ═══════════════════════════════════════════════════
    btns = ttk.Frame(content)
    btns.pack(fill=tk.X, pady=(4, 0))

    def save_settings():
        try:
            s = load_user_settings()
            s['log_level'] = log_level_val.get()
            s['concurrency_max_workers'] = int(max_workers_val.get() or '4')
            s['concurrency_batch_size'] = int(batch_size_val.get() or '5')
            tp = template_path_val.get()
            inp = input_dir_val.get()
            outp = output_dir_val.get()
            resp = result_dir_val.get()
            try:
                if tp:
                    tp = os.path.relpath(tp, os.getcwd()) if os.path.isabs(tp) else tp
                if inp:
                    inp = os.path.relpath(inp, os.getcwd()) if os.path.isabs(inp) else inp
                if outp:
                    outp = os.path.relpath(outp, os.getcwd()) if os.path.isabs(outp) else outp
                if resp:
                    resp = os.path.relpath(resp, os.getcwd()) if os.path.isabs(resp) else resp
            except Exception:
                pass
            s['template_path'] = tp
            s['input_folder'] = inp
            s['output_folder'] = outp
            s['result_folder'] = resp
            save_user_settings(s)
            try:
                from app.core.utils.log_utils import set_log_level
                set_log_level(s['log_level'])
            except Exception:
                pass
            try:
                tpl_path = s['template_path']
                tpl_dir = os.path.dirname(tpl_path)
                tpl_name = os.path.basename(tpl_path)
                cfg.update('Paths', 'template_folder', tpl_dir)
                cfg.update('Templates', 'purchase_order', tpl_name)
                try:
                    cfg.update('Paths', 'template_file', os.path.join(tpl_dir, tpl_name))
                except Exception:
                    pass
                cfg.update('Paths', 'input_folder', s['input_folder'])
                cfg.update('Paths', 'output_folder', s['output_folder'])
                cfg.update('Performance', 'max_workers', s['concurrency_max_workers'])
                cfg.update('Performance', 'batch_size', s['concurrency_batch_size'])
                cfg.update('API', 'api_key', api_key_val.get())
                cfg.update('API', 'secret_key', secret_key_val.get())
                cfg.update('API', 'timeout', timeout_val.get())
                cfg.update('API', 'max_retries', max_retries_val.get())
                cfg.update('API', 'retry_delay', retry_delay_val.get())
                cfg.update('API', 'api_url', api_url_val.get())
                cfg.update('Gitea', 'base_url', gitea_url_val.get())
                cfg.update('Gitea', 'owner', gitea_owner_val.get())
                cfg.update('Gitea', 'repo', gitea_repo_val.get())
                cfg.update('Gitea', 'token', gitea_token_val.get())
                cfg.save_config()
            except Exception:
                pass
            messagebox.showinfo("设置已保存", "系统设置已更新并保存")
            dlg.destroy()
        except Exception as e:
            messagebox.showerror("保存失败", str(e))

    ttk.Button(btns, text="云端同步", command=lambda: show_cloud_sync_dialog(dlg)).pack(side=tk.LEFT)
    ttk.Button(btns, text="取消", command=dlg.destroy).pack(side=tk.RIGHT)
    ttk.Button(btns, text="保存", command=save_settings).pack(side=tk.RIGHT, padx=6)
