using MainShell.Common;
using MainShell.EventArgsFolder;
using MainShell.Hardware;
using MainShell.Manual.Model;
using MainShell.Recipe.Models;
using Stylet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MainShell.Manual.ViewModel
{
    public class ChipStraighteningViewModel : OperateViewModelBase,IHandle<ChipAngleEventArgs>
    {
        private double _chipAngle;
        public double ChipAngle
        {
            get { return _chipAngle; }
            set { SetAndNotify(ref _chipAngle, value); }
        }

        private readonly HardwareManager _hardwareManager;
        private readonly IEventAggregator _eventAggregator;
        private readonly RecipeManager _recipeManager;
        public ChipStraighteningViewModel(HardwareManager hardwareManager, IEventAggregator eventAggregator, RecipeManager recipeManager) 
        {
            _hardwareManager = hardwareManager ?? throw new ArgumentNullException(nameof(hardwareManager));
            _recipeManager = recipeManager ?? throw new ArgumentNullException(nameof(recipeManager));
            _eventAggregator = eventAggregator ?? throw new ArgumentNullException(nameof(eventAggregator));
            eventAggregator.Subscribe(this);
            _cameraAxisViewModel = IoC.Get<Common.Display.ViewModel.CameraAxisViewModel>();
            _cameraAxisViewModel.CameraAxisDevices.HardwareDeviceList = _hardwareManager.CameraAxisManager.TopCameraAxisDevices;
        }

        // 手动启动流程
        public async Task StartProcess()
        {
            var context = new MW.WorkFlow.WorkflowContext();
            context[WorkflowContextKeys.EventAggregator] = _eventAggregator;
            context[WorkflowContextKeys.RecipeManager] = _recipeManager;

            await RunManualActivityAsync(new MainShell.Process.ChipStraighteningActivity("ChipStraightening"), context);
        }

        public override async Task StopProcess()
        {
            await base.StopProcess();
        }


        public void Handle(ChipAngleEventArgs message)
        {
            Stylet.Execute.OnUIThread(() =>
            {
                ChipAngle = message.Angle;
            });
        }
    }
}
