using MainShell.Common;
using MainShell.Log;
using MainShell.Models;
using MainShell.ParaSetting.Model;
using MaxwellFramework.Core.Interfaces;
using MwFramework.ManagerService;
using Stylet;
using System;
using System.Windows;

namespace MainShell.ParaSetting.ViewModel
{
    public class CameraParameterViewModel : BaseScreen
    {
        private readonly IParamList _paramList;
        private DeviceFoundationSetting _deviceFoundationSetting;

        public CameraParameterViewModel(IParameterManager parameterManager)
        {
            _paramList = parameterManager as IParamList;
            if (_paramList == null)
            {
                throw new ArgumentNullException(nameof(parameterManager));
            }
        }

        private DeviceCameraSettingItem _cameraSettingItem;

        public DeviceCameraSettingItem CameraSettingItem
        {
            get
            {
                return _cameraSettingItem;
            }
            set
            {
                SetAndNotify(ref _cameraSettingItem, value);
            }
        }

        private BindableCollection<CameraFovSettingItem> _cameraItems;

        public BindableCollection<CameraFovSettingItem> CameraItems
        {
            get
            {
                return _cameraItems;
            }
            set
            {
                SetAndNotify(ref _cameraItems, value);
            }
        }

        protected override void OnViewLoaded()
        {
            base.OnViewLoaded();
            _deviceFoundationSetting = _paramList.GetParameter<DeviceFoundationSetting>();
            RefreshState();
        }

        public void BtnSave()
        {
            try
            {
                _deviceFoundationSetting.Write();
                MwMessageBox.Show("保存完成", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (Exception ex)
            {
                LogManager.LogSysError(ex.ToString());
                MwMessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void RefreshState()
        {
            if (_deviceFoundationSetting.CameraSettingItem == null)
            {
                _deviceFoundationSetting.CameraSettingItem = new DeviceCameraSettingItem();
            }

            CameraSettingItem = _deviceFoundationSetting.CameraSettingItem;
            CameraSettingItem.EnsureDefaultCameras();

            BindableCollection<CameraFovSettingItem> cameraItems = new BindableCollection<CameraFovSettingItem>();
            cameraItems.Add(CameraSettingItem.UpCamera);
            cameraItems.Add(CameraSettingItem.DownCamera);
            cameraItems.Add(CameraSettingItem.MapCamera);
            CameraItems = cameraItems;
        }
    }
}
