using Flurl.Http;
using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using OpenAuth.App;
using OpenAuth.App.Config;
using OpenAuth.App.Interface;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.App.SSO;
using Org.BouncyCastle.Ocsp;


namespace OpenAuth.WebApi.Controllers
{
    /// <summary>
    /// 无人机执法
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    partial class DroneCaseinfoController
    {
        IConfiguration _configuration;
        DroneCaseinfoApp _app;
        WFProcessApp _wfProcessApp;
        public DroneCaseinfoController(
            IConfiguration configuration,
            DroneCaseinfoApp app,
            IAuth auth,
            ILogger<DroneCaseinfoController> logger,
            WFProcessApp wFProcessApp)
        {
            _configuration = configuration;
            _app = app;
            _wfProcessApp = wFProcessApp;
        }
        ///// <summary>
        ///// 同步航飞库数据
        ///// </summary>
        ///// <param name="req"></param>
        ///// <returns></returns>
        //[AllowAnonymous]
        //[HttpPost]
        //public async Task<Response<string>> AsynchDroneCaseDataOld([FromBody] AddOrUpdateAsyncDroneCaseDataReq req)
        //{
        //    Response<string> response = new Response<string>();
        //    try
        //    {
        //        response.Result = _app.AsynchDroneCaseDataNew(req);
        //        if (response.Result == "同步失败")
        //        {
        //            return response;
        //        }
        //        var loginResult = await "http://localhost:1120/api/Check/Login"
        //            .PostJsonAsync(new
        //            {
        //                account = "System",
        //                passWord = "e10adc3949ba59abbe56e057f20f883e"

        //            }).ReceiveJson<Response<LoginResult>>();

        //        var token = loginResult.Result.Token;

        //        var draftResult = await "http://localhost:1120/api/WFProcess/SaveDraftWithUser"
        //            .WithHeader("X-Token", token)
        //            .PostJsonAsync(new
        //            {
        //                processId = req.model.Id,
        //                schemeCode = "754545",
        //                userId = req.model.createusername,
        //                instanceInfo = "{\"schemeId\":\"853793b9-6db7-47a8-b125-faaef20862f7\",\"pkey\":\"Id\",\"pkeyValue\":\"" + req.model.Id + "\"}",
        //                isChild = 0,
        //                title = "后端发起流程" + req.model.case_description + DateTime.Now.ToString(),
        //            }).ReceiveJson<Response<bool>>();



        //        var creteResult = await "http://localhost:1120/api/WFProcess/CreateWithUser"
        //            .WithHeader("X-Token", token)
        //            .PostJsonAsync(new
        //            {
        //                processId = req.model.Id,
        //                userId = req.model.createusername,
        //                isChild = 0,
        //                title = "后端创建" + req.model.case_description + DateTime.Now.ToString(),
        //                instanceInfo = "{\"schemeId\":\"853793b9-6db7-47a8-b125-faaef20862f7\",\"pkey\":\"Id\",\"pkeyValue\":\"" + req.model.Id + "\"}",
        //            }).ReceiveJson<Response<bool>>();


        //        //var aaa = await _wfProcessApp.SaveDraft(
        //        //      req.model.Id,
        //        //      "9",
        //        //      "-1",
        //        //      "{\"schemeId\":\"3a286ae0-7236-487e-ab93-1ebc03038b4c\",\"pkey\":\"Id\",\"pkeyValue\":\"" + req.model.Id + "\"}",
        //        //      null,
        //        //      null,
        //        //      null,
        //        //      0,
        //        //      "后端发起流程" + DateTime.Now.ToString()
        //        //      );

        //        //var bbb = await _wfProcessApp.Create(req.model.Id, null, "-1", null, "{\"schemeId\":\"3a286ae0-7236-487e-ab93-1ebc03038b4c\",\"pkey\":\"Id\",\"pkeyValue\":\"" + req.model.Id + "\"}", "后端发起流程" + DateTime.Now.ToString());
        //    }
        //    catch (Exception ex)
        //    {
        //        response.Code = 500;
        //        response.Message = ex.InnerException?.Message ?? ex.Message;
        //    }
        //    return response;
        //}
        /// <summary>
        /// 案件上报-流程
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        public async Task<Response<string>> AsynchDroneCaseData([FromBody] AddOrUpdateAsyncDroneCaseDataReq req)
        {
            Response<string> response = new Response<string>();
            try
            {
                var caseResult = await _app.AsynchDroneCaseDataNew(req);
                if (caseResult == "同步失败")
                {
                    response.Code = 500;
                    response.Result = caseResult;
                    return response;
                }
            }
            catch (Exception ex)
            {
                response.Result = "流程发起失败";
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 新案件上报-流程
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        public async Task<Response<string>> AsynchDroneCaseDataLast([FromBody] AddOrUpdateAsyncDroneCaseDataReq req)
        {
            Response<string> response = new Response<string>();
            try
            {
                var caseResult = await _app.AsynchDroneCaseDataLast(req);
                response.Message = caseResult;
                response.Result = caseResult;
                if (caseResult == "同步失败")
                {
                    response.Code = 500;
                    response.Result = caseResult;
                    return response;
                }
            }
            catch (Exception ex)
            {
                response.Result = "流程发起失败";
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 案件上报-流程 非粮化
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        [NonAction]
        public async Task<Response<string>> AsynchDroneCaseDataFLH([FromBody] AddOrUpdateAsyncDroneCaseDataReq req)
        {
            Response<string> response = new Response<string>();
            try
            {
                var caseResult = _app.AsynchDroneCaseDataFLH(req);
                if (caseResult == "同步失败")
                {
                    response.Code = 500;
                    response.Result = caseResult;
                    return response;
                }
                var scode = _configuration.GetSection("AppSetting:DroneCaseInfoNonFoodSchemeCode").Value;
                var schemeId = "a1d40efb-c530-47cc-94b3-100cafc4c4d8";
                var flag = await _wfProcessApp.SaveDraftWithUser(req.model.Id, scode, req.model.createusername, "{\"schemeId\":\"" + schemeId + "\",\"pkey\":\"Id\",\"pkeyValue\":\"" + req.model.Id + "\"}", null, null, null, 0, (req.model.case_description));
                if (flag.Result)
                {
                    var createFlag = await _wfProcessApp.CreateWithUser(req.model.Id, null, req.model.createusername, null, "{\"schemeId\":\"" + schemeId + "\",\"pkey\":\"Id\",\"pkeyValue\":\"" + req.model.Id + "\"}", (req.model.case_description));
                    if (createFlag.Result)
                    {
                        response.Result = "流程发起成功";
                    }
                    else
                    {
                        response.Code = 500;
                        response.Result = createFlag.Message;
                    }
                }
            }
            catch (Exception ex)
            {
                response.Result = "流程发起失败";
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 发起未正常发起流程的案件
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<string>> AsynchAddProcessCaseData()
        {
            Response<string> response = new Response<string>();
            try
            {
                var caseResult = _app.LoadNoProcess().Result;

                foreach (var item in caseResult)
                {
                    var scode = _configuration.GetSection("AppSetting:SchemeCode").Value;
                    var schemeId = _configuration.GetSection("AppSetting:SchemeId").Value;
                    var flag = await _wfProcessApp.SaveDraftWithUser(item.Id, scode, item.createusername, "{\"schemeId\":\"" + schemeId + "\",\"pkey\":\"Id\",\"pkeyValue\":\"" + item.Id + "\"}", null, null, null, 0, (item.case_description));
                    if (flag.Result)
                    {
                        var createFlag = await _wfProcessApp.CreateWithUser(item.Id, null, item.createusername, null, "{\"schemeId\":\"" + schemeId + "\",\"pkey\":\"Id\",\"pkeyValue\":\"" + item.Id + "\"}", (item.case_description));
                        if (createFlag.Result)
                        {
                            response.Result = "流程发起成功";
                        }
                        else
                        {
                            response.Code = 500;
                            response.Result = createFlag.Message;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                response.Result = "流程发起失败";
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }


        /// <summary>
        /// 计算点与shp之间的距离
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<bool>> GetPointShpDistance(string lng, string lat, string caseid)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                return await _app.GetPointShpDistance(lng, lat, caseid);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        #region  案件关闭  同步航飞库用
        /// <summary>
        /// 关闭案件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<Response<bool>> CloseDroneCaseInfos([FromBody] ResubmitCloseReq req)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = await _app.CloseDroneCaseInfos(req.no, req.subject);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        #endregion
    }
}
