using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using FFmpeg.AutoGen;
using Newtonsoft.Json;
using SmartAquaViewer.Controls;
using SmartAquaViewer.DataAnalysis;
using SmartAquaViewer.Model;

namespace SmartAquaViewer.ViewModel
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public string AppTitle { get; set; } = string.Empty;

        private object? _selectedViewModel;
        public object? SelectedViewModel
        {
            get => _selectedViewModel;
            set
            {
                if (_selectedViewModel != value)
                {
                    _selectedViewModel = value;
                    OnPropertyChanged(nameof(SelectedViewModel));
                }
            }
        }

        public ICommand SwapViewCommand { get; }

        public MainViewModel()
        {
            AppTitle = "스마트양식 수처리시스템 운용 및 성능예측 소프트웨어 개발";

            SwapViewCommand = new RelayCommand(SwapView);

            SelectedViewModel = new MonitoringViewModel(); // Default view

            ffmpeg.RootPath = $"{Environment.CurrentDirectory}\\ffmpeg";

            //더미데이터 생성 및 파일로 저장
            //for (int i = 1; i <= 10; i++)
            //{
            //    DateTime date = new(2025, 8, i);
            //    var dataList = WaterQualityVO.GetSampleData(date, date, 24);
            //    var jsonStr = JsonConvert.SerializeObject(dataList, Formatting.Indented);

            //    string fileName = date.ToString("yyyy-MM-dd") + ".json";
            //    File.WriteAllText(fileName, jsonStr);
            //}
        }

        private void SwapView(object obj)
        {
            var tag = (string)obj;

            switch (tag)
            {
                case "monitoring":
                    SelectedViewModel = new MonitoringViewModel();
                    break;
                case "energy":
                    SelectedViewModel = new EnergyViewModel();
                    break;
                case "greenHouseGas":
                    SelectedViewModel = new GreenHouseGasViewModel();
                    break;
                case "cctv":
                    SelectedViewModel = new CCTVViewModel();
                    break;
                default:
                    throw new ArgumentException("Unknown view tag", nameof(obj));                
            }
        }

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