using AngleSharp.Dom;
using Masuit.Tools;
using Masuit.Tools.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using PVDEMCS.Common;
using PVDEMCS.Common.Constant;
using PVDEMCS.Devices;
using PVDEMCS.Devices.Impl;
using PVDEMCS.Services;
using PVDEMCS.Services.Models;
using SharpCompress.Common;
using System;

namespace PVDEMCS.Controllers
{
    /// <summary>
    /// PLC控制器
    /// </summary>
    [ApiController]
    [Route("[controller]/[action]")]
    public class DeviceController : Controller
    {
        private IDeviceService _deviceService;
        private IDeviceRun _deviceRun;

        public DeviceController(IDeviceService deviceService, IDeviceRun deviceRun)
        {
            _deviceService = deviceService;
            _deviceRun = deviceRun;
        }
        #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>
        [HttpGet]
        public Result<PagedList<DeviceInfo>> GetDevicePageList(string deviceCode, string deviceName, bool? activated, int page, int size = 20)
        {
            var result = _deviceService.GetDevicePageList(deviceCode, deviceName, activated, page, size);
            if (result.IsSuccess)
            {
                GetDeviceIsConnected(result.Content.Data);
            }
            return result;
        }


        /// <summary>
        /// 获取PLC控制器列表
        /// </summary>
        /// <param name="deviceCode">控制器编号</param>
        /// <param name="deviceName">控制器名称</param>
        /// <param name="activated">是否启用</param>
        /// /// <returns></returns>
        [HttpGet]
        public Result<List<DeviceInfo>> GetDeviceInfoList(string deviceCode, string deviceName, bool? activated = null)
        {
            var result = _deviceService.GetDeviceInfoList(deviceCode, deviceName, activated);
            if (result.IsSuccess)
            {
                GetDeviceIsConnected(result.Content);
            }
            return result;
        }

        [NonAction]
        private void GetDeviceIsConnected(List<DeviceInfo> list)
        {
            if (!list.IsNullOrEmpty() && !_deviceRun.GetDeviceIsConnected.IsNullOrEmpty())
            {
                foreach (var item in list)
                {
                    if (_deviceRun.GetDeviceIsConnected.Keys.Contains(item.DeviceCode)) 
                    {
                        item.isConnected = _deviceRun.GetDeviceIsConnected[item.DeviceCode];
                    }
                }
            }
        }

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

        /// <summary>
        /// 添加PLC控制器
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public Result<string> AddDevice(DeviceInfo entity)
        {
            var result = _deviceService.AddDevice(entity);
            _deviceRun.Run();
            return result;
        }

        /// <summary>
        /// 更新PLC控制器
        /// </summary>
        /// <param name="entity"></param>
        [HttpPost]
        public Result UpdateDevice(DeviceInfo entity)
        {
            var result = _deviceService.UpdateDevice(entity);
            _deviceRun.Run();
            return result;
        }

        /// <summary>
        /// 删除PLC控制器
        /// </summary>
        /// <param name="id"></param>
        [HttpPost]
        public Result DeleteDevice(string id)
        {
            var result = _deviceService.DeleteDevice(id);
            _deviceRun.Run();
            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="page">当前页</param>
        /// <param name="size">页大小</param>
        /// <returns></returns>
        [HttpGet]
        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 = 20)
        {
            var result = _deviceService.GetDevicePointPageList(deviceId, deviceName, deviceCode, equipmentName, equipmentCode, equipmentType, pointCode, pointName, activated, page, size);
            if (result.IsSuccess)
            {
                GetPointValue(result.Content.Data);
            }
            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>
        /// <returns></returns>
        [HttpGet]
        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 = _deviceService.GetDevicePointList(deviceId, deviceName, deviceCode, equipmentName, equipmentCode, equipmentType, pointCode, pointName, activated);
            GetPointValue(result.Content);
            return result;
        }

        [NonAction]
        private void GetPointValue(List<DevicePoint> list)
        {
            var points = _deviceRun.GetDevicePoints;
            if (!list.IsNullOrEmpty() && !points.IsNullOrEmpty())
            {
                foreach (var item in list)
                {
                    item.ObjectValue = points.Where(f => f.PointCode == item.PointCode).Select(f => f.ObjectValue).FirstOrDefault();
                }
            }
        }

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

        /// <summary>
        /// 添加PLC控制器点位
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public Result<string> AddDevicePoint(DevicePoint entity)
        {
            var result = _deviceService.AddDevicePoint(entity);
            _deviceRun.Run();
            return result;
        }

        /// <summary>
        /// 更新PLC控制器点位
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public Result UpdateDevicePoint(DevicePoint entity)
        {
            var result = _deviceService.UpdateDevicePoint(entity);
            _deviceRun.Run();
            return result;
        }

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

        #endregion


    }
}
