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

namespace PVDEMCS.Services.Impl
{
    /// <summary>
    /// PLC控制器 服务层 实现
    /// </summary>
    internal class DeviceService : IDeviceService
    {
        private readonly IDeviceRepository _deviceRepository;

        public DeviceService(IDeviceRepository deviceRepository)
        {
            this._deviceRepository = deviceRepository;

        }
        #region PLC控制器

        /// <summary>
        /// 获取PLC控制器分页列表
        /// </summary>
        /// <param name="deviceCode">控制器编号</param>
        /// <param name="deviceName">控制器名称</param>
        /// <param name="activated">是否启用</param>
        /// <param name="page">当前页</param>
        /// <param name="size">页大小</param>
        /// <returns></returns>
        public Result<PagedList<DeviceInfo>> GetDevicePageList(string deviceCode, string deviceName, bool? activated, int page, int size)
        {
            var result = _deviceRepository.GetDevicePageList(deviceCode, deviceName, activated, page, size);

            return result;
        }

        /// <summary>
        /// 获取PLC控制器列表
        /// </summary>
        /// <param name="deviceCode">控制器编号</param>
        /// <param name="deviceName">控制器名称</param>
        /// <param name="activated">是否启用</param>
        /// <returns></returns>
        public Result<List<DeviceInfo>> GetDeviceInfoList(string deviceCode, string deviceName, bool? activated = false)
        {
            var result = _deviceRepository.GetDeviceInfoList(deviceCode, deviceName, activated);

            return result;
        }

        /// <summary>
        /// 获取PLC控制器明细
        /// </summary>
        /// <param name="id">主键Id</param>
        /// <returns></returns>
        public Result<DeviceInfo> GetDeviceDetail(string id)
        {
            var result = new Result<DeviceInfo>();

            var value = _deviceRepository.GetDeviceDetail(id);
            if (!value.IsSuccess)
            {
                result.Message = value.Message;
                return result;
            }
            result.Content = ModelTools.PubClone<DeviceInfoEntity, DeviceInfo>.Trans(value.Content);

            return result;
        }

        /// <summary>
        /// 添加PLC控制器
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public Result<string> AddDevice(DeviceInfo info)
        {
            var entity = ModelTools.PubClone<DeviceInfo, DeviceInfoEntity>.Trans(info);
            var result = _deviceRepository.AddDevice(entity);
            return result;
        }

        /// <summary>
        /// 更新PLC控制器
        /// </summary>
        /// <param name="info"></param>
        public Result UpdateDevice(DeviceInfo info)
        {
            var entity = ModelTools.PubClone<DeviceInfo, DeviceInfoEntity>.Trans(info);
            var result = _deviceRepository.UpdateDevice(entity);
            return result;
        }

        /// <summary>
        /// 删除PLC控制器
        /// </summary>
        /// <param name="id"></param>
        public Result DeleteDevice(string id)
        {
            var result = _deviceRepository.DeleteDevice(id);
            return result;
        }

        #endregion

        #region PLC点位

        /// <summary>
        /// 获取PLC控制器点位分页列表
        /// </summary>
        /// <param name="deviceId">控制器Id</param>
        /// <param name="deviceCode">控制器编号</param>
        /// <param name="deviceName">控制器名称</param>
        /// <param name="equipmentName">设备名称</param>
        /// <param name="equipmentCode">设备编号</param>
        /// <param name="equipmentType">设备类型</param>
        /// <param name="pointCode">点位编号</param>
        /// <param name="pointName">点位列表</param>
        /// <param name="activated">是否启用</param>
        /// <param name="page">当前页</param>
        /// <param name="size">页大小</param>
        /// <returns></returns>
        public Result<PagedList<DevicePoint>> GetDevicePointPageList(string deviceId, string deviceName, string deviceCode, string equipmentName, string equipmentCode, string equipmentType, string pointCode, string pointName, bool? activated, int page, int size)
        {
            var result = _deviceRepository.GetDevicePointPageList(deviceId, deviceName, deviceCode, equipmentName, equipmentCode, equipmentType, pointCode, pointName, activated, page, size);

            return result;
        }

        /// <summary>
        /// 获取PLC控制器点位列表
        /// </summary>
        /// <param name="deviceId">控制器Id</param>
        /// <param name="deviceCode">控制器编号</param>
        /// <param name="deviceName">控制器名称</param>
        /// <param name="equipmentName">设备名称</param>
        /// <param name="equipmentCode">设备编号</param>
        /// <param name="equipmentType">设备类型</param>
        /// <param name="pointCode">点位编号</param>
        /// <param name="pointName">点位列表</param>
        /// <param name="activated">是否启用</param>
        /// <returns></returns>
        public Result<List<DevicePoint>> GetDevicePointList(string deviceId, string deviceName, string deviceCode, string equipmentName, string equipmentCode, string equipmentType, string pointCode, string pointName, bool? activated)
        {
            var result = _deviceRepository.GetDevicePointList(deviceId, deviceName, deviceCode, equipmentName, equipmentCode, equipmentType, pointCode, pointName, activated);

            return result;
        }

        /// <summary>
        /// 获取PLC控制器点位明细
        /// </summary>
        /// <param name="id">Id</param>
        /// <returns></returns>
        public Result<DevicePoint> GetDevicePointDetail(string id)
        {
            var result = new Result<DevicePoint>();

            var value = _deviceRepository.GetDevicePointDetail(id);
            if (!value.IsSuccess)
            {
                result.Message = value.Message;
                return result;
            }
            result.Content = ModelTools.PubClone<DevicePointEntity, DevicePoint>.Trans(value.Content);

            return result;
        }

        /// <summary>
        /// 添加PLC控制器点位
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public Result<string> AddDevicePoint(DevicePoint info)
        {
            var entity = ModelTools.PubClone<DevicePoint, DevicePointEntity>.Trans(info);
            var result = _deviceRepository.AddDevicePoint(entity);
            return result;
        }

        /// <summary>
        /// 更新PLC控制器点位
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public Result UpdateDevicePoint(DevicePoint info)
        {
            var entity = ModelTools.PubClone<DevicePoint, DevicePointEntity>.Trans(info);
            var result = _deviceRepository.UpdateDevicePoint(entity);
            return result;
        }

        /// <summary>
        /// 删除PLC控制器点位
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Result DeleteDevicePoint(string id)
        {
            var result = _deviceRepository.DeleteDevicePoint(id);
            return result;
        }

        #endregion
    }
}
