using Masuit.Tools.Models;
using PVDEMCS.Common.DI;
using PVDEMCS.Common;
using PVDEMCS.Services.Models;
using PVDEMCS.Services.Repositories;
using Microsoft.AspNetCore.Mvc.RazorPages;
using PVDEMCS.Common.Constant;
using PVDEMCS.Common.Tools;
using PVDEMCS.Services.Repositories.Entities;
using PVDEMCS.Services.Repositories.Impl;
using PVDEMCS.Devices;
using Masuit.Tools;

namespace PVDEMCS.Services.Impl
{
    /// <summary>
    /// 设备管理 服务层 接口
    /// </summary>
    internal class EquipmentService : IEquipmentService
    {
        private readonly IEquipmentRepository _equipmentRepository;
        private readonly IDeviceRun _deviceRun;

        public EquipmentService(IEquipmentRepository equipmentRepository, IDeviceRun deviceRun)
        {
            this._equipmentRepository = equipmentRepository;
            this._deviceRun = deviceRun;
        }

        /// <summary>
        /// 获取设备分页列表
        /// </summary>
        /// <param name="equipmentName">设备名称</param>
        /// <param name="equipmentCode">设备编号</param>
        /// <param name="equipmentType">设备类型</param>
        /// <param name="activated">是否启用</param>
        /// <param name="page">当前页</param>
        /// <param name="size">页大小</param>
        /// <returns></returns>
        public Result<PagedList<EquipmentInfo>> GetEquipmentPageList(string equipmentName, string equipmentCode, string equipmentType, bool? activated, int page, int size)
        {
            var result = this._equipmentRepository.GetEquipmentPageList(equipmentName, equipmentCode, equipmentType, activated, page, size);
            if (result.IsSuccess)
            {
                GetEquipmentState(result.Content.Data);
            }
            return result;
        }

        /// <summary>
        /// 获取设备列表
        /// </summary>
        /// <param name="equipmentName">设备名称</param>
        /// <param name="equipmentCode">设备编号</param>
        /// <param name="activated">是否启用</param>
        public Result<List<EquipmentInfo>> GetEquipmentList(string equipmentName, string equipmentCode, string equipmentType, bool? activated)
        {
            var result = this._equipmentRepository.GetEquipmentList(equipmentName, equipmentCode, equipmentType, activated);
            if (result.IsSuccess)
            {
                GetEquipmentState(result.Content);
            }
            return result;
        }

        /// <summary>
        /// 获取设备状态
        /// </summary>
        /// <param name="equipmentInfos"></param>
        private void GetEquipmentState(List<EquipmentInfo> equipmentInfos)
        {
            var points = _deviceRun.GetDevicePoints;
            if (!equipmentInfos.IsNullOrEmpty() && !points.IsNullOrEmpty())
            {
                foreach (var equipmentInfo in equipmentInfos)
                {
                    var list = points.Where(f => f.EquipmentId == equipmentInfo.Id).ToList();
                    if (list.Count >= 2)
                    {
                        var startStop = list.Where(f => f.ActionType == ActionType.StartStop).FirstOrDefault();
                        var fault = list.Where(f => f.ActionType == ActionType.Fault).FirstOrDefault();
                        if (!startStop.IsNullOrEmpty() &&
                             !fault.IsNullOrEmpty() &&
                             !startStop.ObjectValue.IsNullOrEmpty() &&
                             !fault.ObjectValue.IsNullOrEmpty())
                        {
                            var state = EquipmentState.Stop;
                            //如果运行状态比警告状态高，这里先判断运行状态，如果运行状态为false，在判断警告状态
                            if (startStop.GetValue<bool>())
                            {
                                state = EquipmentState.Run;
                            }
                            else if (fault.GetValue<bool>())
                            {
                                state = EquipmentState.Alarm;
                            }
                            equipmentInfo.State = state;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 获取设备明显
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Result<EquipmentInfo> GetEquipmentDetail(string id)
        {
            var result = new Result<EquipmentInfo>();

            var value = _equipmentRepository.GetEquipmentDetail(id);
            if (!value.IsSuccess)
            {
                result.Message = value.Message;
                return result;
            }
            result.Content = ModelTools.PubClone<EquipmentInfoEntity, EquipmentInfo>.Trans(value.Content);

            return result;
        }

        /// <summary>
        /// 添加设备信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public Result AddEquipment(EquipmentInfo info)
        {
            var entity = ModelTools.PubClone<EquipmentInfo, EquipmentInfoEntity>.Trans(info);
            var result = _equipmentRepository.AddEquipment(entity);
            return result;
        }

        /// <summary>
        /// 更新设备信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public Result UpdateEquipment(EquipmentInfo info)
        {
            var entity = ModelTools.PubClone<EquipmentInfo, EquipmentInfoEntity>.Trans(info);
            var result = _equipmentRepository.UpdateEquipment(entity);
            return result;
        }

        /// <summary>
        /// 删除设备信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Result DeleteEquipment(string id)
        {
            var result = _equipmentRepository.DeleteEquipment(id);
            return result;
        }
    }
}
