using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using WebApi.Models;
using WebApi.Helpers;
using System.Reflection;
using System.Net.Http;
using System.Threading.Tasks;
using MesServerWork;


namespace WebApi
{
    public class ServerController
    {
        /// <summary>
        /// AGV到位,离开
        /// </summary>
        /// <param name="url"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public static async Task<string> AGV_To_MES_AgvPass(string url, string json,string position)
        {
            var result = new msgResHeader();
            result.code = "500";
            result.message = "";
            string opName = "OPA10";

            string errorMessage = "";
            int AID = -1;
            try
            {
                // 显示一下接口日志
                LogHelper.ShowMsg("Receive【" + url + "】" + json);
                AID = LogHelper.SaveWebApiReqLogo(url, json);

                // 解析订单内容，存储数据库，根据实际业务来写
                AGV_To_MES_AgvPass_Req personnelBaseData = JsonConvert.DeserializeObject<AGV_To_MES_AgvPass_Req>(json);

                // 验证必填字段
                if (!ValidationHelper.ValidateRequired(personnelBaseData, out errorMessage))
                {
                    LogHelper.ShowMsg($"****************{errorMessage}");
                    result.message = errorMessage;
                    return JsonConvert.SerializeObject(result);
                }
                // 判断是进入还是离开
                if(personnelBaseData.agvStatus == "enter")
                {
                    bool isIn = false;
                    bool isReturn = false;

                    // 根据位置号判断点位
                    switch (position)
                    {
                        case "H1":
                            MIS_BASE.WritePLC(200, opName, true); // 写入请求进入
                            isIn = Convert.ToBoolean(MIS_BASE.ReadPLC(208, opName).ToString() == "1"); // 读取允许进入
                            break;
                        case "H2":
                            MIS_BASE.WritePLC(203, opName, true); // 写入请求进入
                            isIn = Convert.ToBoolean(MIS_BASE.ReadPLC(213, opName).ToString() == "1"); // 读取允许进入
                            break;
                        default:
                            LogHelper.ShowMsg($"****************未识别的station{position}");
                            result.message = $"未识别的Station{position}";
                            return JsonConvert.SerializeObject(result);
                    }
                    // 判断是否允许进入 返回200 否则 返回 500不允许
                    if (isIn)
                    {
                        result.code = "200";
                        LogHelper.ShowMsg($"AGV状态更新: 工位={position}, 状态=允许进入");
                    }
                }
                if (personnelBaseData.agvStatus == "leave")
                {
                    // 根据位置号判断点位
                    switch (position)
                    {
                        case "H1":
                            MIS_BASE.WritePLC(201, opName, true); // 写入AGV已离开
                            break;
                        case "H2":
                            MIS_BASE.WritePLC(204, opName, true); // 写入AGV已离开
                            break;
                        default:
                            LogHelper.ShowMsg($"****************未识别的station{position}");
                            result.message = $"未识别的Station{position}";
                            return JsonConvert.SerializeObject(result);
                    }
                    // 判断是否允许进入 返回200 否则 返回 500不允许
                    result.code = "200";
                    LogHelper.ShowMsg($"AGV状态更新: 工位={position}, 状态=已离开");
                }

                LogHelper.SaveWebApiResLogo(AID, JsonConvert.SerializeObject(result));
            }
            catch (Exception err)
            {
                result.code = "500";
                result.message = err.Message;
                Console.WriteLine($"错误: {MethodBase.GetCurrentMethod().Name} - {err.Message}");
            }

            return JsonConvert.SerializeObject(result);
        }
    }
}
