using SWRIS.Dtos;
using SWRIS.Enums;
using SWRIS.Extensions;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;

namespace SWRIS.Models.ViewModel
{
    public class RecordViewModel : INotifyPropertyChanged
    {
        private GetRecordsInputDto searchInput = new GetRecordsInputDto();
        private ObservableCollection<RecordDto> records = new ObservableCollection<RecordDto>();
        private bool isAllSelected = false;
        public List<KeyAndValueDto> RiskLevels { get; set; } = new List<KeyAndValueDto>() { new KeyAndValueDto { Key = null, Value = "全部" } };
        public List<KeyAndValueDto> Ropes { get; set; } = new List<KeyAndValueDto>() { new KeyAndValueDto { Key = null, Value = "全部" } };
        public GetRecordsInputDto SearchInput
        {
            get => searchInput;
            set { searchInput = value; OnPropertyChanged("SearchInput"); }
        }

        public ObservableCollection<RecordDto> Records
        {
            get => records;
            set
            {
                records = value;
                OnPropertyChanged("Records");
            }
        }
       
        public RecordViewModel()
        {
            RiskLevels.AddRange(TypeExtension.ToKeyAndDescriptionList(typeof(RiskLevel)).OrderBy(c => c.Key));
            Ropes.AddRange(App.Config.Equipments.Select(c => new KeyAndValueDto
            {
                Key = c.RopeNumber,
                Value = c.RopeName
            }));
        }
        public bool IsAllSelected
        {
            get => isAllSelected;
            set
            {
                if (isAllSelected != value)
                {
                    isAllSelected = value;
                    OnPropertyChanged("IsAllSelected");

                    // 当全选复选框状态改变时，更新所有项的选择状态
                    if (Records != null)
                    {
                        foreach (var item in Records)
                        {
                            item.IsSelected = value;
                        }
                    }
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected internal virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
