#if !CUSTOMER_BUILD
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Ounibo.Catalog.Core;

namespace Ounibo.Catalog.App
{
    /// <summary>
    /// 维护入口 (M1): 数据包信息 + 系列清单 + 加载/替换 + 资源完整性校验。
    /// M2 将扩展 Builder 编辑功能。
    /// </summary>
    public class AdminWindow : Window
    {
        TextBlock _info;
        DataGrid _seriesGrid;
        ListBox _report;
        MainWindow _owner;

        public AdminWindow(MainWindow owner)
        {
            _owner = owner;
            Title = "维护入口 - 数据包管理 / Admin - Package";
            Width = 640; Height = 520; MinWidth = 520; MinHeight = 400;
            WindowStartupLocation = WindowStartupLocation.CenterOwner;
            Owner = owner;

            var panel = new DockPanel { Margin = new Thickness(12) };
            _info = new TextBlock();
            DockPanel.SetDock(_info, Dock.Top);
            panel.Children.Add(_info);

            var btns = new StackPanel { Orientation = Orientation.Horizontal, Margin = new Thickness(0, 8, 0, 8) };
            var btnLoad = new Button { Content = "加载新数据包 / Load Package", Width = 170, Margin = new Thickness(0, 0, 8, 0) };
            btnLoad.Click += (s, e) => LoadPackage();
            var btnCheck = new Button { Content = "校验 / Validate", Width = 120, Margin = new Thickness(0, 0, 8, 0) };
            btnCheck.Click += (s, e) => Validate();
            var btnBuilder = new Button { Content = "目录制作器 / Builder", Width = 150, Margin = new Thickness(0, 0, 8, 0) };
            btnBuilder.Click += (s, e) => { var w = new BuilderWindow(_owner); w.ShowDialog(); };
            var btnClose = new Button { Content = "关闭 / Close", Width = 90 };
            btnClose.Click += (s, e) => Close();
            btns.Children.Add(btnLoad); btns.Children.Add(btnCheck); btns.Children.Add(btnBuilder); btns.Children.Add(btnClose);
            DockPanel.SetDock(btns, Dock.Top);
            panel.Children.Add(btns);

            _seriesGrid = new DataGrid { IsReadOnly = true, AutoGenerateColumns = false, Height = 220, Margin = new Thickness(0, 4, 0, 4) };
            _seriesGrid.Columns.Add(new DataGridTextColumn { Header = "系列代码", Binding = new System.Windows.Data.Binding("Code"), Width = 80 });
            _seriesGrid.Columns.Add(new DataGridTextColumn { Header = "中文名", Binding = new System.Windows.Data.Binding("Zh"), Width = 210 });
            _seriesGrid.Columns.Add(new DataGridTextColumn { Header = "变体数", Binding = new System.Windows.Data.Binding("Variants"), Width = 70 });
            _seriesGrid.Columns.Add(new DataGridTextColumn { Header = "参数", Binding = new System.Windows.Data.Binding("Params"), Width = 70 });
            _seriesGrid.Columns.Add(new DataGridTextColumn { Header = "规则", Binding = new System.Windows.Data.Binding("Rules"), Width = 60 });
            _seriesGrid.Columns.Add(new DataGridTextColumn { Header = "附件", Binding = new System.Windows.Data.Binding("Atts"), Width = 60 });
            panel.Children.Add(_seriesGrid);

            _report = new ListBox();
            panel.Children.Add(_report);

            Content = panel;
            RefreshInfo();
        }

        void RefreshInfo()
        {
            var p = _owner.Package;
            if (p == null)
            {
                _info.Text = "未加载数据包";
                _seriesGrid.ItemsSource = null;
                return;
            }
            var c = p.Catalog;
            _info.Text = string.Format(
                "数据包: {0}   文件: {1}\n目录版本: {2} | 格式版本: {3}   语言: {4}\n系列数: {5} | 变体数: {6}   (与主窗口当前加载一致)",
                c.catalogName, System.IO.Path.GetFileName(p.Path), c.catalogVersion, c.schemaVersion,
                string.Join(",", c.langs ?? new List<string>()),
                c.series.Count, c.series.Sum(s => s.variants == null ? 0 : s.variants.Count));
            var dt = new DataTable();
            dt.Columns.Add("Code"); dt.Columns.Add("Zh"); dt.Columns.Add("Variants", typeof(int));
            dt.Columns.Add("Params", typeof(int)); dt.Columns.Add("Rules", typeof(int)); dt.Columns.Add("Atts", typeof(int));
            foreach (var s in c.series)
                dt.Rows.Add(s.code, s.nameZh, s.variants == null ? 0 : s.variants.Count,
                    s.parameters == null ? 0 : s.parameters.Count, s.rules == null ? 0 : s.rules.Count,
                    s.attachments == null ? 0 : s.attachments.Count);
            _seriesGrid.ItemsSource = dt.DefaultView;
        }

        void Validate()
        {
            var p = _owner.Package;
            _report.Items.Clear();
            if (p == null) { _report.Items.Add("未加载数据包"); return; }
            var issues = PackageValidator.Validate(p);
            foreach (var i in issues)
                _report.Items.Add("[" + i.Severity + "] " + (string.IsNullOrEmpty(i.Target) ? "" : i.Target + " ") + i.Message);
            _report.Items.Add(string.Format("校验完成: {0} 个错误, {1} 个警告, 共 {2} 个变体",
                issues.Count(x => x.Severity == "ERROR"), issues.Count(x => x.Severity == "WARNING"),
                p.Catalog.series.Sum(s => s.variants == null ? 0 : s.variants.Count)));
        }

        void LoadPackage()
        {
            var dlg = new Microsoft.Win32.OpenFileDialog
            {
                Filter = "目录数据包 (*.opc)|*.opc",
                Title = "选择 .opc 数据包"
            };
            if (dlg.ShowDialog() == true)
            {
                if (_owner.ReplacePackage(dlg.FileName))
                {
                    MessageBox.Show("数据包已替换", "维护入口", MessageBoxButton.OK, MessageBoxImage.Information);
                    RefreshInfo();
                    _report.Items.Clear();
                }
            }
        }
    }
}
#endif
