using MeterVision.db;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MeterVision.Stand
{
    /// <summary>
    /// SelecteStationDlg.xaml 的交互逻辑
    /// </summary>
    public partial class SelecteStationDlg : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public ObservableCollection<TStation> StationList1 { get; set; }
        public ObservableCollection<TStation> StationList2 { get; set; }

        private ICollectionView _stationListView1;
        public ICollectionView StationListView1
        {
            get => _stationListView1;
            set
            {
                _stationListView1 = value;
                OnPropertyChanged(nameof(StationListView1));
            }
        }

        private ICollectionView _stationListView2;
        public ICollectionView StationListView2
        {
            get => _stationListView2;
            set
            {
                _stationListView2 = value;
                OnPropertyChanged(nameof(StationListView2));
            }
        }

        //public List<TStation> StationList1 { get; set; }
        //public List<TStation> StationList2 { get; set; }

        public int TotalStationCount1 { get; set; }
        public int TotalStationCount2 { get; set; }

        public TStation SelectedStationItem1 { get; set; }
        public TStation SelectedStationItem2 { get; set; }
        public string StationId { get; set; }

        private List<TStation> _allStationList;

        public SelecteStationDlg(List<TStation> stations)
        {
            InitializeComponent();

            StationId = string.Empty;

            //StationList1 = new List<TStation>();     //new ObservableCollection<TStation>();
            //StationList2 = new List<TStation>();     //new ObservableCollection<TStation>();
            StationList1 = new ObservableCollection<TStation>();
            StationList2 = new ObservableCollection<TStation>();
            foreach (var item in stations)
            {
                StationList1.Add(item);
            }
            //StationList1.AddRange(stations);

            _allStationList = new List<TStation>();
            _allStationList.AddRange(stations);

            //dgStation1.ItemsSource = StationList1;
            StationListView1 = CollectionViewSource.GetDefaultView(StationList1);
            StationListView1.SortDescriptions.Add(new SortDescription("StationId", ListSortDirection.Ascending));
            dgStation1.ItemsSource = StationListView1;

            //dgStation2.ItemsSource = StationList2;
            StationListView2 = CollectionViewSource.GetDefaultView(StationList2);
            StationListView2.SortDescriptions.Add(new SortDescription("StationId", ListSortDirection.Ascending));
            dgStation2.ItemsSource = StationListView2;
            
            //TotalStationCount1 = StationList1.Count;
            //TotalStationCount2 = StationList2.Count;

            this.DataContext = this;
        }

        private void BtnViewImage_Click(object sender, RoutedEventArgs e)
        {
            //GetMinSampleTimeDetail
            Button button = sender as Button;
            if (button == null) return;

            DataGridRow row = FindAncestor<DataGridRow>(button);
            if (row == null) return;

            //获取当前行绑定的数据项
            var selectedItem = row.Item as TStation;
            if (selectedItem == null) return;


            TStandDetail standDetail = DBStand.GetMinSampleTimeDetail(selectedItem.StandId, selectedItem.StationId);
            if (standDetail == null) return;

            if (!File.Exists(standDetail.SrcImage)) return;

            var dialog = new imgWindow(standDetail.SrcImage)
            {
                Owner = this,
                WindowStartupLocation = WindowStartupLocation.CenterOwner
            };
            dialog.Show();
        }

        private void BtnAddStation_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            if (button == null) return;

            DataGridRow row = FindAncestor<DataGridRow>(button);
            if (row == null) return;

            //获取当前行绑定的数据项
            var selectedItem = row.Item as TStation;

            StationList1.Remove(selectedItem);
            StationList2.Add(selectedItem);
            StationListView2.Refresh();     //刷新，以确保排序
        }

        private void BtnRemoveStation_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            if (button == null) return;

            DataGridRow row = FindAncestor<DataGridRow>(button);
            if (row == null) return;

            //获取当前行绑定的数据项
            var selectedItem = row.Item as TStation;
            StationList2.Remove(selectedItem);
            StationList1.Add(selectedItem);
            StationListView1.Refresh(); //刷新CollectionView,以确保排序
        }

        private void BtnOK_Click(object sender, RoutedEventArgs e)
        {
            if(StationList2.Count <= 0)
            {
                MessageBox.Show("请最少选择一个站点");
                return;
            }
            DialogResult = true;
            this.Close();
        }

        private void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void BtnQuery_Click(object sender, RoutedEventArgs e)
        {
            StationList1.Clear();
            string stationId = StationId.Trim();
            
            foreach(var item in _allStationList)
            {
                if (item.StationId.Contains(stationId))
                {
                    StationList1.Add(item);
                }
            }
            StationListView1.Refresh();
        }

        private static T FindAncestor<T>(DependencyObject dependencyObject) where T : DependencyObject
        {
            var parent = VisualTreeHelper.GetParent(dependencyObject);

            if (parent == null) return null;

            var parentT = parent as T;
            return parentT ?? FindAncestor<T>(parent);
        }


        //-------------------------------------------------------------------------------
    }
}
