using MwFramework.Controls.UIControl;
using MwFramework.ManagerService;
using Stylet;
using System.ComponentModel.Composition;

namespace MainShell.ParaSetting.Model
{
    [Export(typeof(IParameter))]
    public class DeviceFoundationSetting : ParameterBase
    {
        public DeviceFoundationSetting()
        {
            SaveSettingItem = new DeviceSaveSettingItem();
            CameraSettingItem = new DeviceCameraSettingItem();
        }

        public DeviceSaveSettingItem SaveSettingItem { get; set; }

        public DeviceCameraSettingItem CameraSettingItem { get; set; }

        public override void Copy(IParameter source)
        {
            DeviceFoundationSetting setting = source as DeviceFoundationSetting;
            if (setting == null)
            {
                return;
            }

            if (SaveSettingItem == null)
            {
                SaveSettingItem = new DeviceSaveSettingItem();
            }

            if (CameraSettingItem == null)
            {
                CameraSettingItem = new DeviceCameraSettingItem();
            }

            SaveSettingItem.CopyFrom(setting.SaveSettingItem);
            CameraSettingItem.CopyFrom(setting.CameraSettingItem);
        }
    }

    public class DeviceSaveSettingItem : PropertyChangedBase, IParameterItem
    {
        private FileSaveSettingItem _fileSaveSetting = new FileSaveSettingItem();

        public FileSaveSettingItem FileSaveSetting
        {
            get
            {
                return _fileSaveSetting;
            }
            set
            {
                SetAndNotify(ref _fileSaveSetting, value);
            }
        }

        private ImageSaveSettingItem _imageSaveSetting = new ImageSaveSettingItem();

        public ImageSaveSettingItem ImageSaveSetting
        {
            get
            {
                return _imageSaveSetting;
            }
            set
            {
                SetAndNotify(ref _imageSaveSetting, value);
            }
        }

        public void CopyFrom(DeviceSaveSettingItem source)
        {
            if (source == null)
            {
                return;
            }

            if (FileSaveSetting == null)
            {
                FileSaveSetting = new FileSaveSettingItem();
            }

            if (ImageSaveSetting == null)
            {
                ImageSaveSetting = new ImageSaveSettingItem();
            }

            FileSaveSetting.CopyFrom(source.FileSaveSetting);
            ImageSaveSetting.CopyFrom(source.ImageSaveSetting);
        }

        public IParameterItem Clone()
        {
            DeviceSaveSettingItem item = new DeviceSaveSettingItem();
            item.CopyFrom(this);
            return item;
        }
    }

    public class FileSaveSettingItem : PropertyChangedBase, IParameterItem
    {
        private bool _isSaveEnabled = true;

        public bool IsSaveEnabled
        {
            get
            {
                return _isSaveEnabled;
            }
            set
            {
                SetAndNotify(ref _isSaveEnabled, value);
            }
        }

        private string _saveFolder = @"D:\RecordData\File\";

        public string SaveFolder
        {
            get
            {
                return _saveFolder;
            }
            set
            {
                SetAndNotify(ref _saveFolder, value);
            }
        }

        private bool _isCreateDateFolder = true;

        public bool IsCreateDateFolder
        {
            get
            {
                return _isCreateDateFolder;
            }
            set
            {
                SetAndNotify(ref _isCreateDateFolder, value);
            }
        }

        public void CopyFrom(FileSaveSettingItem source)
        {
            if (source == null)
            {
                return;
            }

            IsSaveEnabled = source.IsSaveEnabled;
            SaveFolder = source.SaveFolder;
            IsCreateDateFolder = source.IsCreateDateFolder;
        }

        public IParameterItem Clone()
        {
            FileSaveSettingItem item = new FileSaveSettingItem();
            item.CopyFrom(this);
            return item;
        }
    }

    public class ImageSaveSettingItem : PropertyChangedBase, IParameterItem
    {
        private bool _isSaveEnabled = true;

        public bool IsSaveEnabled
        {
            get
            {
                return _isSaveEnabled;
            }
            set
            {
                SetAndNotify(ref _isSaveEnabled, value);
            }
        }

        private string _saveFolder = @"D:\RecordData\Image\";

        public string SaveFolder
        {
            get
            {
                return _saveFolder;
            }
            set
            {
                SetAndNotify(ref _saveFolder, value);
            }
        }

        private bool _isCreateDateFolder = true;

        public bool IsCreateDateFolder
        {
            get
            {
                return _isCreateDateFolder;
            }
            set
            {
                SetAndNotify(ref _isCreateDateFolder, value);
            }
        }

        private bool _isSaveUpCameraImage = true;

        public bool IsSaveUpCameraImage
        {
            get
            {
                return _isSaveUpCameraImage;
            }
            set
            {
                SetAndNotify(ref _isSaveUpCameraImage, value);
            }
        }

        private bool _isSaveDownCameraImage = true;

        public bool IsSaveDownCameraImage
        {
            get
            {
                return _isSaveDownCameraImage;
            }
            set
            {
                SetAndNotify(ref _isSaveDownCameraImage, value);
            }
        }

        private bool _isSaveMapCameraImage = true;

        public bool IsSaveMapCameraImage
        {
            get
            {
                return _isSaveMapCameraImage;
            }
            set
            {
                SetAndNotify(ref _isSaveMapCameraImage, value);
            }
        }

        public void CopyFrom(ImageSaveSettingItem source)
        {
            if (source == null)
            {
                return;
            }

            IsSaveEnabled = source.IsSaveEnabled;
            SaveFolder = source.SaveFolder;
            IsCreateDateFolder = source.IsCreateDateFolder;
            IsSaveUpCameraImage = source.IsSaveUpCameraImage;
            IsSaveDownCameraImage = source.IsSaveDownCameraImage;
            IsSaveMapCameraImage = source.IsSaveMapCameraImage;
        }

        public IParameterItem Clone()
        {
            ImageSaveSettingItem item = new ImageSaveSettingItem();
            item.CopyFrom(this);
            return item;
        }
    }

    public class DeviceCameraSettingItem : PropertyChangedBase, IParameterItem
    {
        private CameraFovSettingItem _upCamera = new CameraFovSettingItem("上相机");

        public CameraFovSettingItem UpCamera
        {
            get
            {
                return _upCamera;
            }
            set
            {
                SetAndNotify(ref _upCamera, value);
            }
        }

        private CameraFovSettingItem _downCamera = new CameraFovSettingItem("下相机");

        public CameraFovSettingItem DownCamera
        {
            get
            {
                return _downCamera;
            }
            set
            {
                SetAndNotify(ref _downCamera, value);
            }
        }

        private CameraFovSettingItem _mapCamera = new CameraFovSettingItem("Map相机");

        public CameraFovSettingItem MapCamera
        {
            get
            {
                return _mapCamera;
            }
            set
            {
                SetAndNotify(ref _mapCamera, value);
            }
        }

        public void EnsureDefaultCameras()
        {
            if (UpCamera == null)
            {
                UpCamera = new CameraFovSettingItem("上相机");
            }
            else if (string.IsNullOrWhiteSpace(UpCamera.CameraName))
            {
                UpCamera.CameraName = "上相机";
            }

            if (DownCamera == null)
            {
                DownCamera = new CameraFovSettingItem("下相机");
            }
            else if (string.IsNullOrWhiteSpace(DownCamera.CameraName))
            {
                DownCamera.CameraName = "下相机";
            }

            if (MapCamera == null)
            {
                MapCamera = new CameraFovSettingItem("Map相机");
            }
            else if (string.IsNullOrWhiteSpace(MapCamera.CameraName))
            {
                MapCamera.CameraName = "Map相机";
            }
        }

        public void CopyFrom(DeviceCameraSettingItem source)
        {
            if (source == null)
            {
                return;
            }

            EnsureDefaultCameras();
            source.EnsureDefaultCameras();

            UpCamera.CopyFrom(source.UpCamera);
            DownCamera.CopyFrom(source.DownCamera);
            MapCamera.CopyFrom(source.MapCamera);
        }

        public IParameterItem Clone()
        {
            DeviceCameraSettingItem item = new DeviceCameraSettingItem();
            item.CopyFrom(this);
            return item;
        }
    }

    public class CameraFovSettingItem : PropertyChangedBase, IParameterItem
    {
        public CameraFovSettingItem()
        {
        }

        public CameraFovSettingItem(string cameraName)
        {
            _cameraName = cameraName;
            _fovX = 1.0;
            _fovY = 1.0;
        }

        private string _cameraName;

        public string CameraName
        {
            get
            {
                return _cameraName;
            }
            set
            {
                SetAndNotify(ref _cameraName, value);
            }
        }

        private double _fovX;

        public double FovX
        {
            get
            {
                return _fovX;
            }
            set
            {
                SetAndNotify(ref _fovX, value);
            }
        }

        private double _fovY;

        public double FovY
        {
            get
            {
                return _fovY;
            }
            set
            {
                SetAndNotify(ref _fovY, value);
            }
        }

        public void CopyFrom(CameraFovSettingItem source)
        {
            if (source == null)
            {
                return;
            }

            CameraName = source.CameraName;
            FovX = source.FovX;
            FovY = source.FovY;
        }

        public IParameterItem Clone()
        {
            CameraFovSettingItem item = new CameraFovSettingItem();
            item.CopyFrom(this);
            return item;
        }
    }
}
