using BPASmartClient.CustomResource.UserControls;
using BPASmartClient.CustomResource.UserControls.MessageShow;
using BPASmartClient.Helper;
using BPASmartClient.IoT;
using DataVAPI.Tool.IOT;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace BPASmartClient.ViewModel
{
    public class LogOrAlarmIOTViewModel : ObservableObject
    {

        #region 单一变量
        private volatile static LogOrAlarmIOTViewModel _Instance;
        public static LogOrAlarmIOTViewModel GetInstance() => _Instance ?? (_Instance = new LogOrAlarmIOTViewModel());
        private LogOrAlarmIOTViewModel()
        {
            Init();
        }
        #endregion
        #region 变量
        public string api = "https://bpa.black-pa.com:21527/datav/api/Log/QueryLogFile?DeviceName=";
        public string DataVApiAddress { set; get; }
        private ObservableCollection<FileModel> _LogModels; public ObservableCollection<FileModel> LogDataFile
        {
            get
            {
                return _LogModels;
            }
            set
            {
                if (_LogModels == value)
                    return;
                _LogModels = value;
                OnPropertyChanged("LogDataFile");
            }
        }
        #endregion
        #region Command
        public RelayCommand QueryCommand { get; set; }
        #endregion

        #region 函数
        public void Init()
        {
            DataVApiAddress = System.Configuration.ConfigurationManager.AppSettings["DataVServiceUri"].ToString();

            LogDataFile = new ObservableCollection<FileModel>();
            //查询
            QueryCommand = new RelayCommand(() =>
           {
               Res();
           });
        }

        public void Res()
        {
            if (DataVClient.GetInstance().DeviceDataV != null && DataVClient.GetInstance().DeviceDataV.deviceTable != null)
            {
                try
                {
                    LogDataFile.Clear();
                    api = $"{DataVApiAddress}/api/Log/QueryLogFile?DeviceName={DataVClient.GetInstance().DeviceDataV.deviceTable.devicename}"; ;
                    string json = HttpRequestHelper.HttpGetRequest(api, 1000);
                    JsonMsg<List<FileModel>> jsonMsg = Tools.JsonToObjectTools<JsonMsg<List<FileModel>>>(json);
                    jsonMsg.obj?.data?.ForEach(file =>
                    {
                        LogDataFile.Add(file);
                    });
                    NoticeDemoViewModel.OpenMsg(EnumPromptType.Info, MainViewModel.GetInstance().window, "提示", $"查询成功,文件数{LogDataFile.Count}!");
                }
                catch (Exception ex)
                {
                    NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, MainViewModel.GetInstance().window, "提示", $"查询失败!");
                }

            }
        }
        #endregion
    }

    public class FileModel : ObservableObject
    {
        public string downloadUrl { get; set; }
        public string fileId { get; set; }
        public string name { get; set; }
        public string size { get; set; }
        public string utcCreatedOn { get; set; }
        private Brush _foreground;
        public Brush foreground
        {
            get
            {
                return _foreground;
            }
            set
            {
                if (_foreground == value)
                    return;
                _foreground = value;
                OnPropertyChanged("foreground");
            }
        }
        public FileModel()
        {
            foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#21bb2e"));
        }
    }
}
