using Microsoft.AspNetCore.Mvc;
using ServiceCenter;
using System.Net.NetworkInformation;
using System.Text;
using WCS.Core;
using WCS.Entity.Protocol.SRM;
using WCS.WorkEngineering.Systems;

namespace WCS.WorkEngineering.WebApi.Controllers
{
    /// <summary>
    /// WCS相关接口控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class WcsController : ControllerBase, IDeviceWriter
    {

        

        /// <summary>
        ///  设备信息写入接口
        /// </summary>
        /// <param name="deviceType">需要写入信息的设备类型</param>
        /// <param name="devCode">设备编号</param>
        /// <param name="protocol">设备协议类名</param>
        /// <param name="propName">写入字段名</param>
        /// <param name="value">值</param>
        [HttpPost]
        public void Write(DeviceTypeEnum deviceType, string devCode, string protocol, string propName, string value)
        {
            World.GetSystemInstance<DeviceWriteSystem>().Invoke(new DeviceWriteInfo
            {
                DeviceType = deviceType,
                Code = devCode,
                Protocol = protocol,
                Property = propName,
                Value = value
            });
        }

        /// <summary>
        ///  获取设备配置信息接口
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<Device> GetDeviceList()
        {
            return Device.All.ToList();
        }

        /// <summary>
        ///  获取设备信息
        /// </summary>
        /// <param name="name">设备名称</param>
        /// <returns></returns>
        [HttpGet]
        public object GetDeviceInfo(string name)
        {
            var obj = World.GetSystemInstance<GetDeviceSystem>().Invoke(name);

            return obj;
        }

        /// <summary>
        ///  堆垛机测试
        /// </summary>
        /// <param name="srmcod">堆垛机编号</param>
        /// <param name="typeEnum">任务类型</param>
        /// <param name="value1">起始行</param>
        /// <param name="value2">起始列</param>
        /// <param name="value3">起始层</param>
        /// <param name="value4">目标行</param>
        /// <param name="value5">目标列</param>
        /// <param name="value6">目标层</param>
        [HttpPost]
        public void SrmDeBug(string srmcod, SrmTaskTypeEnum typeEnum, short value1, short value2, short value3, short value4, short value5, short value6)
        {
            World.GetSystemInstance<SrmDebugSystem>().Invoke(new SrmDebugInfo
            {
                SrmCode = srmcod,
                srmTaskType = typeEnum,
                SLine = value1,
                SCol = value2,
                SLayer = value3,
                ELine = value4,
                ECol = value5,
                ELayer = value6
            });
        }

        #region 设备IP相关

        /// <summary>
        ///  获取设备Ip集合
        /// </summary>
        /// <returns>设备Ip集合</returns>
        [HttpGet]
        public List<string> GetDeviceIpList()
        {
            if (!ServiceHub.DeviceIPList.Any()) throw new Exception("未配置任何Ip");
            return ServiceHub.DeviceIPList;
        }

        /// <summary>
        ///  获取设备IP检测结果
        /// </summary>
        /// <returns>设备IP检测结果</returns>
        [HttpGet]
        public List<DeviceIpTestResults> DeviceIpTest()
        {
            if (!ServiceHub.DeviceIPList.Any()) throw new Exception("未配置任何Ip");
            List<DeviceIpTestResults> deviceIpTestResults = new List<DeviceIpTestResults>();
            ServiceHub.DeviceIPList.ForEach(ip =>
            {
                deviceIpTestResults.Add(new DeviceIpTestResults
                {
                    Ip = ip,
                    Result = PingIpOrDomainName(ip)
                });
            });

            return deviceIpTestResults;
        }

        /// <summary>
        /// 检查Ip是否正常联通
        /// </summary>
        /// <param name="strIpOrDName">输入参数,表示IP地址或域名</param>
        /// <returns></returns>
        public static bool PingIpOrDomainName(string strIpOrDName)
        {
            try

            {
                Ping objPingSender = new Ping();

                PingOptions objPinOptions = new PingOptions();

                objPinOptions.DontFragment = true;

                string data = "";

                byte[] buffer = Encoding.UTF8.GetBytes(data);

                int intTimeout = 120;

                PingReply objPinReply = objPingSender.Send(strIpOrDName, intTimeout, buffer, objPinOptions);

                string strInfo = objPinReply.Status.ToString();

                if (strInfo == "Success")

                {
                    return true;
                }
                else

                {
                    return false;
                }
            }
            catch (Exception)

            {
                return false;
            }
        }

        #endregion 设备IP相关
    }

    public interface IDeviceWriter
    {
        [HttpPost]
        void Write(DeviceTypeEnum deviceType, string devCode, string protocol, string propName, string value);
    }

    /// <summary>
    ///  设备Ip通讯检测结构
    /// </summary>
    public class DeviceIpTestResults
    {
        public string Ip { get; set; }

        public bool Result { get; set; }
    }
}
