using Microsoft.Extensions.Logging;
using SlnMesnac.Common;
using SlnMesnac.Model.AirportApiEntity;
using SlnMesnac.Model.domain;
using SlnMesnac.Repository.service.@base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace SlnMesnac.Repository.service.Impl
{
    public class ManipulatorStateServiceImpl : BaseServiceImpl<ManipulatorState>, IManipulatorStateService
    {
        private ILogger<ManipulatorStateServiceImpl> _logger;


        public ManipulatorStateServiceImpl(Repository<ManipulatorState> rep, ILogger<ManipulatorStateServiceImpl> logger) : base(rep)
        {
            _logger = logger;
        }

        public List<ManipulatorState> GetAllManipulatorState()
        {
            var list = new List<ManipulatorState>();
            try
            {
                list = base._rep.GetList();
            }
            catch (Exception ex)
            {
                _logger.LogError($"所有机械臂状态信息获取异常:{ex.Message}");
            }
            return list;
        }

        public List<ManipulatorState> GetFreeManipulatorState()
        {
            var list = new List<ManipulatorState>();
            try
            {
                list = base._rep.GetList()
                    .Where(x => x.ManipulatorNo != "" && (x.TaskNo == null || x.TaskNo == "") && x.ManipulatorWorkState == "任务空闲")
                    .ToList();
            }
            catch (Exception ex)
            {
                _logger.LogError($"空闲机械臂状态信息获取异常:{ex.Message}");
            }
            return list;
        }

        public async Task<bool> UpdateAsync(ManipulatorState record)
        {
            bool result = await _rep.UpdateAsync(record);
            return result;
        }

        public async Task<bool> UpdateOrAddByResponse(ManipulatorStateResponseEntity response)
        {
            ManipulatorState record = new ManipulatorState()
            {
                ManipulatorNo = response.ManipulatorNo,
                ManipulatorAlarmState = response.ManipulatorAlarmState,
                ManipulatorWorkState = response.ManipulatorWorkState,
                TaskNo = response.TaskNo,
                RefreshTime = DateTime.Now,
            };
            bool result = false;
            try
            {
                var list = GetAllManipulatorState();
                if (list.Where(x => x.ManipulatorNo == response.ManipulatorNo).Count() == 0)
                {
                    result = _rep.Insert(record);
                }
                else
                {
                    record.ID = list.Where(x => x.ManipulatorNo == response.ManipulatorNo).FirstOrDefault().ID;
                    result = await _rep.UpdateAsync(record);
                }
            }
            catch(Exception ex)
            {
                _logger.LogError($"机械臂状态信息更新异常:{ex.Message}");
            }
            return result;
        }
    }
}
