using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http;
using System.Collections.Concurrent;
using Newtonsoft.Json.Linq;
using System.Threading;
using System.Net;
using System.Net.Http.Headers;
using System.IO;
using MisDataSaveDate;
using WebApi;

namespace MisDataSaveDate
{
    /// <summary>
    /// AGV接口控制器
    /// </summary>
    public class WebController : ApiController
    {
        readonly string headUrl = "/";

        /// <summary>
        /// AGV请求进入接口（接收方）
        /// 接收AGV的进入请求并存储到数据库
        /// </summary>
        /// <param name="jobj">AGV请求数据</param>
        /// <returns>处理结果</returns>
        [HttpPost]
        [Route("web/WEB_Request")]
        public HttpResponseMessage WEB_Request([FromBody] JObject jobj)
        {
            return new HttpResponseMessage
            {
                Content = new StringContent(WEB_BusinessLogic.WEB_Request(headUrl + System.Reflection.MethodBase.GetCurrentMethod().Name, jobj), Encoding.GetEncoding("UTF-8"), "application/json")
            };
        }

        /// <summary>
        /// AGV请求进入接口（接收方）
        /// 接收AGV的进入请求并存储到数据库
        /// </summary>
        /// <param name="jobj">AGV请求数据</param>
        /// <returns>处理结果</returns>
        [HttpPost]
        [Route("web/WEB_SpotCheckResultUpload")]
        public HttpResponseMessage WEB_SpotCheckResultUpload([FromBody] SpotCheckResult request)
        {
            var result = new MsgResHeader<object>
            {
                code = 200,
                message = "",
                Data = null
            };

            try
            {
                if (request == null)
                {
                    throw new Exception("请求体不能为空");
                }

                var response = CALL_Inerface_DataHandle.CALL_Inerface_SpotCheckResultUpload(request);
                int.TryParse(response["code"]?.ToString() ?? "500", out int remoteCode);
                result.code = remoteCode;
                result.message = response["message"]?.ToString() ?? "";
                result.Data = response["data"];
            }
            catch (Exception ex)
            {
                result.code = 500;
                result.message = ex.Message;
            }

            return new HttpResponseMessage
            {
                Content = new StringContent(JsonConvert.SerializeObject(result), Encoding.GetEncoding("UTF-8"), "application/json")
            };
        }

        /// <summary>
        /// 磨合转序接口
        /// 调用AGV转序接口，成功后执行转序存储过程
        /// </summary>
        /// <param name="jobj">转序请求数据</param>
        /// <returns>处理结果</returns>
        [HttpPost]
        [Route("web/WEB_RunningInTransfer")]
        public HttpResponseMessage WEB_RunningInTransfer([FromBody] JObject jobj)
        {
            return new HttpResponseMessage
            {
                Content = new StringContent(WEB_BusinessLogic.WEB_RunningInTransfer(headUrl + System.Reflection.MethodBase.GetCurrentMethod().Name, jobj), Encoding.GetEncoding("UTF-8"), "application/json")
            };
        }

        /// <summary>
        /// 临工AGV网页按钮调度接口。
        /// </summary>
        /// <param name="jobj">调度请求数据</param>
        /// <returns>处理结果</returns>
        [HttpPost]
        [Route("web/LGAGV_Dispatch")]
        public HttpResponseMessage LGAGV_Dispatch([FromBody] JObject jobj)
        {
            return new HttpResponseMessage
            {
                Content = new StringContent(LGWEB_BusinessLogic.WEB_DispatchAGV(headUrl + System.Reflection.MethodBase.GetCurrentMethod().Name, jobj), Encoding.GetEncoding("UTF-8"), "application/json")
            };
        }

        /// <summary>
        /// 临工AGV网页按钮放行接口。
        /// </summary>
        /// <param name="jobj">放行请求数据</param>
        /// <returns>处理结果</returns>
        [HttpPost]
        [Route("web/LGAGV_AllowLeave")]
        public HttpResponseMessage LGAGV_AllowLeave([FromBody] JObject jobj)
        {
            return new HttpResponseMessage
            {
                Content = new StringContent(LGWEB_BusinessLogic.WEB_AllowLeave(headUrl + System.Reflection.MethodBase.GetCurrentMethod().Name, jobj), Encoding.GetEncoding("UTF-8"), "application/json")
            };
        }

        /// <summary>
        /// 批量读取PLC点位值接口
        /// 前端传递TagTypeCodeID数组和工位号，返回对应点位的值
        /// </summary>
        /// <param name="jobj">请求数据，包含OpName和TagTypeCodeIDs数组</param>
        /// <returns>返回对应TagTypeCodeID的值列表</returns>
        [HttpPost]
        [Route("web/WEB_ReadPLCValues")]
        public HttpResponseMessage WEB_ReadPLCValues([FromBody] JObject jobj)
        {
            return new HttpResponseMessage
            {
                Content = new StringContent(WEB_BusinessLogic.WEB_ReadPLCValues(headUrl + System.Reflection.MethodBase.GetCurrentMethod().Name, jobj), Encoding.GetEncoding("UTF-8"), "application/json")
            };
        }

        /// <summary>
        /// 写入PLC接口
        /// 前端传递TagTypeCodeID、工位号和值，写入到PLC
        /// </summary>
        /// <param name="jobj">请求数据，包含TagTypeCodeID、OpName和TagValue</param>
        /// <returns>写入结果</returns>
        [HttpPost]
        [Route("web/WEB_WritePLC")]
        public HttpResponseMessage WEB_WritePLC([FromBody] JObject jobj)
        {
            return new HttpResponseMessage
            {
                Content = new StringContent(WEB_BusinessLogic.WEB_WritePLC(headUrl + System.Reflection.MethodBase.GetCurrentMethod().Name, jobj), Encoding.GetEncoding("UTF-8"), "application/json")
            };
        }
    }
}
