using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using SmartAquaViewer.Controls;
using SmartAquaViewer.DataAnalysis;
using SmartAquaViewer.Model;

namespace SmartAquaViewer.ViewModel
{
    public class CCTVViewModel : INotifyPropertyChanged
    {
        public ObservableCollection<CCTVInfo> CCTVInfoList { get; } = new ObservableCollection<CCTVInfo>();

        public ObservableCollection<FFPlayerViewModel> PlayerVMs { get; } = new();

        private int _columnCount;
        public int ColumnCount
        {
            get => _columnCount;
            set
            {
                if (_columnCount != value)
                {
                    _columnCount = value;
                    OnPropertyChanged();
                }
            }
        }

        private int _rowCount;
        public int RowCount
        {
            get => _rowCount;
            set
            {
                if (_rowCount != value)
                {
                    _rowCount = value;
                    OnPropertyChanged();
                }
            }
        }

        private bool _isOpenMode;
        public bool IsOpenMode
        {
            get => _isOpenMode;
            set
            {
                if (_isOpenMode != value)
                {
                    _isOpenMode = value;
                    OnPropertyChanged();

                    BtnVisibilityLeft = _isOpenMode ? Visibility.Collapsed : Visibility.Visible;
                    BtnVisibilityRight = _isOpenMode ? Visibility.Visible : Visibility.Collapsed;
                }
            }
        }

        private Visibility _btnVisibilityLeft;
        public Visibility BtnVisibilityLeft
        {
            get => _btnVisibilityLeft;
            set
            {
                if (_btnVisibilityLeft != value)
                {
                    _btnVisibilityLeft = value;
                    OnPropertyChanged();
                }
            }
        }

        private Visibility _btnVisibilityRight;
        public Visibility BtnVisibilityRight
        {
            get => _btnVisibilityRight;
            set
            {
                if (_btnVisibilityRight != value)
                {
                    _btnVisibilityRight = value;
                    OnPropertyChanged();
                }
            }
        }

        private Visibility _btnVisibilityPlay;
        public Visibility BtnVisibilityPlay
        {
            get => _btnVisibilityPlay;
            set
            {
                if (_btnVisibilityPlay != value)
                {
                    _btnVisibilityPlay = value;
                    OnPropertyChanged();
                }
            }
        }

        private Visibility _btnVisibilityStop;
        public Visibility BtnVisibilityStop
        {
            get => _btnVisibilityStop;
            set
            {
                if (_btnVisibilityStop != value)
                {
                    _btnVisibilityStop = value;
                    OnPropertyChanged();
                }
            }
        }

        public ICommand ChangeDrawerStatusCommand { get; }

        public ICommand PlayAllCCTVCommand { get; }

        public ICommand StopAllCCTVCommand { get; }



        public CCTVViewModel()
        {
            ColumnCount = 4; // Default value
            RowCount = 2;    // Default value

            IsOpenMode = false;
            BtnVisibilityRight = Visibility.Collapsed;
            BtnVisibilityStop = Visibility.Collapsed;

            CCTVInfoList = new ObservableCollection<CCTVInfo>()
            {
                new()
                {
                    DeviceId = "000001",
                    DeviceName = "CCTV 1",
                    RtspUrl = "rtsp://210.217.121.58:8554/CAM-211",
                    Status = CCTVStatus.Disconnected
                },
                new()
                {
                    DeviceId = "000002",
                    DeviceName = "CCTV 2",
                    RtspUrl = "rtsp://210.217.121.58:8554/CAM-212",
                    Status = CCTVStatus.Disconnected
                },
                new()
                {
                    DeviceId = "000003",
                    DeviceName = "CCTV 3",
                    RtspUrl = "rtsp://210.217.121.58:8554/CAM-01",
                    Status = CCTVStatus.Disconnected
                },
                new()
                {
                    DeviceId = "000004",
                    DeviceName = "CCTV 4",
                    RtspUrl = "rtsp://210.217.121.58:8554/CAM-02",
                    Status = CCTVStatus.Disconnected
                },
                new()
                {
                    DeviceId = "000005",
                    DeviceName = "CCTV 5",
                    RtspUrl = "rtsp://210.217.121.58:8554/CAM-03",
                    Status = CCTVStatus.Disconnected
                },
                new()
                {
                    DeviceId = "000006",
                    DeviceName = "CCTV 6",
                    RtspUrl = "rtsp://210.217.121.58:8554/CAM-04",
                    Status = CCTVStatus.Disconnected
                },
                new()
                {
                    DeviceId = "000007",
                    DeviceName = "CCTV 7",
                    RtspUrl = "rtsp://210.217.121.58:8554/CAM-05",
                    Status = CCTVStatus.Disconnected
                },
                new()
                {
                    DeviceId = "000008",
                    DeviceName = "CCTV 8",
                    RtspUrl = "rtsp://210.217.121.58:8554/CAM-06",
                    Status = CCTVStatus.Disconnected
                }
            };

            BuildPlayers(CCTVInfoList);

            ChangeDrawerStatusCommand = new RelayCommand(_ => IsOpenMode = !IsOpenMode);
            PlayAllCCTVCommand = new RelayCommand(PlayAllCCTV);
            StopAllCCTVCommand = new RelayCommand(StopAllCCTV);
        }

        private void PlayAllCCTV(object obj)
        {
            foreach (var ff in PlayerVMs)
                ff.StartMedia(ff.CCTVInfo.RtspUrl!);

            BtnVisibilityPlay = Visibility.Collapsed;
            BtnVisibilityStop = Visibility.Visible;
        }

        private void StopAllCCTV(object obj)
        {
            foreach (var ff in PlayerVMs)
                ff.ClosePlayer();

            BtnVisibilityPlay = Visibility.Visible;
            BtnVisibilityStop = Visibility.Collapsed;
        }

        public void BuildPlayers(IEnumerable<CCTVInfo> infos)
        {
            PlayerVMs.Clear();
            foreach (var info in infos)
                PlayerVMs.Add(new FFPlayerViewModel(info));
        }

        public event PropertyChangedEventHandler? PropertyChanged;
        private void OnPropertyChanged([CallerMemberName] string? name = null)
            => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}
