using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

namespace NavisworksTransport.UI.WPF.Views
{
    /// <summary>
    /// 路径选择对话框
    /// </summary>
    public partial class PathSelectionDialog : Window
    {
        private ObservableCollection<PathRouteDisplayItem> _allPaths;
        private ObservableCollection<PathRouteDisplayItem> _filteredPaths;

        public PathSelectionDialog()
        {
            InitializeComponent();
            
            _allPaths = new ObservableCollection<PathRouteDisplayItem>();
            _filteredPaths = new ObservableCollection<PathRouteDisplayItem>();
            dgPaths.ItemsSource = _filteredPaths;
            
            LoadPaths();
        }

        /// <summary>
        /// 加载路径列表
        /// </summary>
        private void LoadPaths()
        {
            try
            {
                var database = PathPlanningManager.Instance?.GetPathDatabase();
                if (database == null)
                {
                    MessageBox.Show("数据库未初始化", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                var routes = database.GetAllPathRoutes();
                
                _allPaths.Clear();
                foreach (var route in routes)
                {
                    _allPaths.Add(new PathRouteDisplayItem(route));
                }
                
                _filteredPaths.Clear();
                foreach (var item in _allPaths)
                {
                    _filteredPaths.Add(item);
                }
                
                UpdateSelectedCount();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载路径失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        /// <summary>
        /// 获取选中的路径
        /// </summary>
        public List<PathRoute> GetSelectedPaths()
        {
            return _allPaths.Where(p => p.IsSelected).Select(p => p.Route).ToList();
        }

        /// <summary>
        /// 搜索框文本变化
        /// </summary>
        private void txtSearch_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            var searchText = txtSearch.Text.ToLower();
            
            _filteredPaths.Clear();
            foreach (var item in _allPaths)
            {
                if (string.IsNullOrEmpty(searchText) || 
                    item.Name.ToLower().Contains(searchText) ||
                    item.PathTypeDisplay.ToLower().Contains(searchText))
                {
                    _filteredPaths.Add(item);
                }
            }
        }

        /// <summary>
        /// 全选
        /// </summary>
        private void btnSelectAll_Click(object sender, RoutedEventArgs e)
        {
            var allSelected = _filteredPaths.All(p => p.IsSelected);
            
            foreach (var item in _filteredPaths)
            {
                item.IsSelected = !allSelected;
            }
            
            UpdateSelectedCount();
        }

        /// <summary>
        /// 更新选中数量显示
        /// </summary>
        private void UpdateSelectedCount()
        {
            txtSelectedCount.Text = $"已选择: {_allPaths.Count(p => p.IsSelected)} 条路径";
        }

        /// <summary>
        /// 确定
        /// </summary>
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            var selectedPaths = GetSelectedPaths();
            if (selectedPaths.Count == 0)
            {
                MessageBox.Show("请至少选择一条路径", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            DialogResult = true;
            Close();
        }

        /// <summary>
        /// 取消
        /// </summary>
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }
    }

    /// <summary>
    /// 路径显示项
    /// </summary>
    public class PathRouteDisplayItem
    {
        public PathRoute Route { get; }
        public bool IsSelected { get; set; }
        
        public string Name => Route.Name;
        public string PathTypeDisplay => GetPathTypeDisplay(Route.PathType);
        public string TotalLengthDisplay => $"{Route.TotalLength:F2} 米";
        public int PointCount => Route.Points?.Count ?? 0;
        public string CreatedTimeDisplay => Route.CreatedTime.ToString("yyyy-MM-dd HH:mm");

        public PathRouteDisplayItem(PathRoute route)
        {
            Route = route;
            IsSelected = false;
        }

        private string GetPathTypeDisplay(PathType pathType)
        {
            switch (pathType)
            {
                case PathType.Ground:
                    return "地面";
                case PathType.Rail:
                    return "轨道";
                case PathType.Hoisting:
                    return "吊装";
                default:
                    return "未知";
            }
        }
    }
}