using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Media3D;
using Newtonsoft.Json;
using SmartAquaViewer.Classes;
using SmartAquaViewer.DataAnalysis;
using static MaterialDesignThemes.Wpf.Theme.ToolBar;

namespace SmartAquaViewer.Model
{
    public class Datas : INotifyPropertyChanged
    {
        public static Datas Instance { get; } = new Datas();

        public ObservableCollection<WaterQualityVO> WaterQualityList { get; set; }
        public ReadOnlyObservableCollection<WaterQualityVO> WaterQualityView { get; }
        public ObservableCollection<CCTVInfo> CctvInfoList { get; set; }

        private Datas()
        {
            WaterQualityList = new ObservableCollection<WaterQualityVO>();
            WaterQualityView = new ReadOnlyObservableCollection<WaterQualityVO>(WaterQualityList);
            CctvInfoList = new ObservableCollection<CCTVInfo>();
        }

        public ObservableCollection<WaterQualityVO> GetWaterQualityVO()
        {
            return WaterQualityList;
        }

        public void SetWaterQualityVO(List<WaterQualityVO> sampleData)
        {
            WaterQualityList.Clear();
            if (sampleData == null) return;
            foreach (var data in sampleData)            
                WaterQualityList.Add(data);

            OnPropertyChanged(nameof(WaterQualityList));
        }

        public void GetCCTVInfoListFromJson()
        {
            string cctvListPath = Utils.Instance.GetDataFileContentPath(Constants.DataFiles.CCTV_LIST);
            if (!File.Exists(cctvListPath))
            {
                CreateAndSetMockUpCCTVInfoList(cctvListPath);
                return;
            }

            string jsonString = File.ReadAllText(cctvListPath);
            var cctvInfoList = JsonConvert.DeserializeObject<List<CCTVInfo>>(jsonString) ?? new List<CCTVInfo>();

            CctvInfoList = new ObservableCollection<CCTVInfo>(cctvInfoList);
        }

        public void CreateAndSetMockUpCCTVInfoList(string filePath)
        {
            CctvInfoList.Clear();

            CctvInfoList = new ObservableCollection<CCTVInfo>()
            {
                new()
                {
                    DeviceId = "000001",
                    DeviceName = "CCTV 1",
                    RtspUrl = "rtsp://210.217.121.58:8554/CAM-07",
                    Status = CCTVStatus.Disconnected
                },
                new()
                {
                    DeviceId = "000002",
                    DeviceName = "CCTV 2",
                    RtspUrl = "rtsp://210.217.121.58:8554/CAM-08",
                    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
                }
            };

            string updatedJson = JsonConvert.SerializeObject(CctvInfoList, Formatting.Indented);
            File.WriteAllText(filePath, updatedJson);
        }

        public ObservableCollection<CCTVInfo> GetCCTVInfoList()
        {
            return CctvInfoList;
        }

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