using BPA.SingleDevice.Interface;
using BPA.SingleDevice.Json;
using BPA.SingleDevice.Services;

namespace BPA.SingleDevice.ViewModel
{
    public class DebugViewModel : NotifyBase
    {
        private readonly IProcessControl process;
        private readonly ILogService logService;

        public DebugViewModel(IProcessControl process, ILogService logService)
        {
            this.process = process;
            this.logService = logService;
            StartMoveInchCommand = new(() =>
            {
                if (process.CurrentRecipes.Count != 0)
                {
                    Message.ErrorGlobal("当前有配方正在执行，不可对流水线进行寸动控制。");
                    return;
                }
                var isSuccess = process.Conveyer?.SetInchParam(IsReverse ? 1 : 0, InchSpeed);
                if (isSuccess is not null && isSuccess == true)
                {
                    process.Conveyer?.StartInchMove();
                    Message.ShowGlobal("设置流水线寸动开始成功。");
                    logService.LogUserInfo("设置流水线寸动开始。");
                }
                else
                {
                    Message.ShowGlobal("设置流水线寸动开始失败，写入寸动参数失败了。");
                }
            });
            StopMoveInchCommand = new(() =>
            {
                process.Conveyer.StopInchMove();
                Message.ShowGlobal("设置流水线寸动停止成功。");
                logService.LogUserInfo("设置流水线寸动停止。");
            });

            WriteParamsCommand = new(() =>
            {
                if (process.CurrentRecipes.Count > 0)
                {
                    Message.ErrorGlobal("当前有配方正在执行，请稍后重试。");
                    return;
                }
                //数据验证。
                if (ValidateData())
                {
                    var isSuccess = process?.Conveyer.SetMoveParam(MoveSpeed, AccTime, MoveLength);
                    if (isSuccess is not null && isSuccess == true)
                    {
                        Json<ConnectConfig>.Data.MoveLength = MoveLength;
                        Json<ConnectConfig>.Data.MoveSpeed = MoveSpeed;
                        Json<ConnectConfig>.Data.MoveDelay = MoveDelay;
                        Json<ConnectConfig>.Save();
                        logService.LogUserInfo("写入流水线参数并保存到文件。");
                        Message.SuccessGlobal("写入流水线参数并保存到文件成功。");
                    }
                    else
                    {
                        Message.ErrorGlobal("写入流水线参数失败，请检查后重试。");
                    }
                }
                else
                {
                    Message.ErrorGlobal("写入错误，数据验证失败。");
                }
            });
        }

        private bool ValidateData()
        {
            return true;
        }

        public BPARelayCommand<bool> SwitchSystemModeCommand { get; set; }

        /// <summary>开始寸动。</summary>
        public BPARelayCommand StartMoveInchCommand { get; set; }

        /// <summary>开始寸动。</summary>
        public BPARelayCommand StopMoveInchCommand { get; set; }

        /// <summary>保存设置。</summary>
        public BPARelayCommand SaveCommand { get; set; }

        /// <summary>向流水线写入移动参数。</summary>
        public BPARelayCommand WriteParamsCommand { get; set; }

        /// <summary>寸动是否反转。</summary>
        public bool IsReverse { get; set; }

        public uint InchSpeed { get; set; } = 1000;

        /// <summary>流水线移动速度。</summary>
        public uint MoveSpeed { get; set; } = Json<ConnectConfig>.Data.MoveSpeed;

        /// <summary>加速时间</summary>
        public uint AccTime { get; set; }

        /// <summary>移动一次的距离。</summary>
        public uint MoveLength { get; set; } = Json<ConnectConfig>.Data.MoveLength;
        /// <summary>
        /// 传送带移动延迟（配料完成后延迟一定时间再移动）
        /// </summary>
        public uint MoveDelay { get; set; } = Json<ConnectConfig>.Data.MoveDelay;

        /// <summary>开机自启</summary>
        public bool AutoStart
        { get { return SystemHelper.GetInstance.IsAutoStart(); } set { SystemHelper.GetInstance.AutoStart(value); OnPropertyChanged(); } }
    }
}