using MainShell.Common.VisionTemple.View;
using MainShell.Hardware;
using Stylet;
using System.ComponentModel;
using System.Windows;

namespace MainShell.Common.VisionTemple.ViewModel
{
    public class VisionTempleWindowViewModel : Screen
    {
        private readonly VisionTempleViewModel _visionTemple;
        private VisionTempleWindow _window;
        private bool _isShuttingDown;
        private readonly HardwareManager _hardwareManager;

        public VisionTempleWindowViewModel(HardwareManager hardwareManager)
        {
            DisplayName = "视觉模板制作";
            _visionTemple = new VisionTempleViewModel();
            _visionTemple.Service = new MwFramework.Controls.PatternTemplate.ViewModel.MatchTemplateService();
            _visionTemple.Service.CameraModel.IsShowFoldDialog = false;
            _hardwareManager = hardwareManager;
        }

        private string _templatePath;
        /// <summary>
        /// 模版路径
        /// </summary>
        public string TemplatePath
        {
            get { return _templatePath; }
            set
            {
                _templatePath = value;
                _visionTemple.Service.CameraModel.TemplatePath = value;
            }
        }

        public VisionTempleViewModel VisionTemple
        {
            get
            {
                return _visionTemple;
            }
        }

        public void SetCamera(CameraType cameraType)
        {
            switch (cameraType)
            {
                case CameraType.MapCamera:
                    _visionTemple.Service.Cameras = _hardwareManager.CameraAxisManager.BottomCameraAxisDevices;
                    break;
                case CameraType.TopPositionCamera:
                    _visionTemple.Service.Cameras = _hardwareManager.CameraAxisManager.TopCameraAxisDevices;
                    break;
                case CameraType.TopWideCamera:
                    _visionTemple.Service.Cameras = _hardwareManager.CameraAxisManager.WideCameraAxisDevices;
                    break;
                default:
                    break;
            }
            if(_visionTemple.Service.Cameras!=null&& _visionTemple.Service.Cameras.Count>0)
            {
                _visionTemple.Service.SelectItem = _visionTemple.Service.Cameras[0];
            }
        }   

        public void ShowWindow()
        {
            if (_window == null)
            {
                _window = new VisionTempleWindow();
                _window.DataContext = this;
                _window.Closing += OnWindowClosing;
            }

            Window owner = Application.Current != null ? Application.Current.MainWindow : null;
            if (owner != null && _window.Owner == null)
            {
                _window.Owner = owner;
                owner.Closing += OnOwnerClosing;
            }

            if (_window.IsVisible)
            {
                if (_window.WindowState == WindowState.Minimized)
                {
                    _window.WindowState = WindowState.Normal;
                }

                _window.Activate();
                return;
            }

            _window.Show();
            _window.Activate();
        }

        private void OnWindowClosing(object sender, CancelEventArgs e)
        {
            if (!_isShuttingDown)
            {
                e.Cancel = true;
                _window.Hide();
            }
        }

        private void OnOwnerClosing(object sender, CancelEventArgs e)
        {
            _isShuttingDown = true;
        }
    }
}
