using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYY.Api.Finance.dd;
using XYY.Core.Standard.Mvc;
using XYY.Model.Standard.Finance;
using XYY.Service.Standard.Finance;

namespace XYY.Api.Finance.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DingtalkController : ApiControllerBase
    {
        IQuotationApproveService _quotationApproveService;
        ICustomerAccountService _customerAccountService;
        IDingTalk_ReimbursementService _ReimbursementService;
        IDingTalk_PaymentService _PaymentService;
        public DingtalkController(IQuotationApproveService quotationApproveService, ICustomerAccountService customerAccountService, IDingTalk_ReimbursementService reimbursementService, IDingTalk_PaymentService paymentService)
        {
            _quotationApproveService = quotationApproveService;
            _customerAccountService = customerAccountService;
            _ReimbursementService = reimbursementService;
            _PaymentService = paymentService;
        }

        public class body
        {
            public string encrypt { get; set; }
        }

        [HttpPost]
        public async Task<IActionResult> ddxx([FromQuery] string msg_signature, [FromQuery] string timestamp, [FromQuery] string nonce, [FromBody] body body)
        {
            //接收encrypt参数
            DingTalkEncryptor dingTalkEncryptor = new DingTalkEncryptor("ivx5zgdkKbXR", "2gny8U4MFc8Ad7gXASzIfRt1T6WPZVKUoR9wdPooa5E", "dingxucfxjsjiuzhlcg8");
            string encryptMsg = body.encrypt.Replace("{\"encrypt\":\"", "").Replace("\"}", "");

            string decryptMsg = dingTalkEncryptor.getDecryptMsg(msg_signature, timestamp, nonce, encryptMsg);
            // 3. 反序列化回调事件json数据
            var eventJson = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(decryptMsg);
            string eventType = eventJson.EventType;
            string processCode = eventJson.processCode;

            if ("bpms_instance_change".Equals(eventType))
            {
                if (DingTalkProcessCode.CustomerQuotaProcessCode.Equals(processCode))
                {
                    //审批开始事件
                    string processInstanceId = eventJson.processInstanceId;//审批实例id
                    if (eventJson.type == "start")
                    {
                        //2185da4e-9ada-4bfd-8a24-ad0bdb987c63
                        string message = await _customerAccountService.DingTalkStartProcessEvent(processInstanceId);
                        if (message != "成功")
                            throw new Exception(message);
                    }
                    else if (eventJson.type == "finish")
                    {
                        if (eventJson.result == "agree")
                            await _customerAccountService.ApplySubmit(processInstanceId);
                        else if (eventJson.result == "refuse")
                            await _customerAccountService.Refuse(processInstanceId);
                    }
                    else if (eventJson.type == "terminate")
                    {
                        await _customerAccountService.Cancel(processInstanceId);
                    }
                }
                else if (DingTalkProcessCode.ReimbursementCode.Equals(processCode))
                {
                    //审批开始事件
                    string processInstanceId = eventJson.processInstanceId;//审批实例id
                    if (eventJson.type == "start")
                    {
                        string message = await _ReimbursementService.DingTalkStartProcessEvent(processInstanceId);
                        if (message != "成功")
                            throw new Exception(message);
                    }
                    else if (eventJson.type == "finish")
                    {
                        if (eventJson.result == "agree")
                            await _ReimbursementService.ApplySubmit(processInstanceId);
                        else if (eventJson.result == "refuse")
                            await _ReimbursementService.Refuse(processInstanceId);
                    }
                    else if (eventJson.type == "terminate")
                    {
                        await _ReimbursementService.Cancel(processInstanceId);
                    }
                }
                else if (DingTalkProcessCode.PaymentCode.Equals(processCode))
                {
                    //审批开始事件
                    string processInstanceId = eventJson.processInstanceId;//审批实例id
                    if (eventJson.type == "start")
                    {
                        string message = await _PaymentService.DingTalkStartProcessEvent(processInstanceId);
                        if (message != "成功")
                            throw new Exception(message);
                    }
                    else if (eventJson.type == "finish")
                    {
                        if (eventJson.result == "agree")
                            await _PaymentService.ApplySubmit(processInstanceId);
                        else if (eventJson.result == "refuse")
                            await _PaymentService.Refuse(processInstanceId);
                    }
                    else if (eventJson.type == "terminate")
                    {
                        await _PaymentService.Cancel(processInstanceId);
                    }
                }
                else if (DingTalkProcessCode.ChannelDiscountCode.Equals(processCode))
                {
                    //审批开始事件
                    string processInstanceId = eventJson.processInstanceId;//审批实例id
                     if (eventJson.type == "finish")
                    {
                        if (eventJson.result == "agree")
                            await _quotationApproveService.HandleQuotationDiscountApproved(processInstanceId);
                        else if (eventJson.result == "refuse")
                            await _quotationApproveService.HandleQuotationReject(processInstanceId);
                    }
                }
                else
                {
                    string processInstanceId = eventJson.processInstanceId;//审批实例id
                    if (eventJson.type == "finish")
                    {
                        if (eventJson.result == "agree")
                            await _quotationApproveService.HandleQuotationApproved(processInstanceId);
                        else if (eventJson.result == "refuse")
                            await _quotationApproveService.HandleQuotationReject(processInstanceId);
                    }

                }
            }
            //else if ("bpms_task_change".Equals(eventType))
            //{

            //    if (DingTalkProcessCode.QuotationApproveProcessCode.Equals(processCode))
            //    {
            //        //审批完成事件
            //        string processInstanceId = eventJson.processInstanceId;//审批实例id
            //        if (eventJson.result == "agree")
            //        {
            //            //审批同意了
            //            var processResult = await _quotationApproveService.HandleQuotationApproved(processInstanceId);
            //            //写入回传消息


            //        }
            //        else if (eventJson.result == "refuse")
            //        {
            //            DingDingProcessResult processResult = await _quotationApproveService.HandleQuotationReject(processInstanceId);
            //        }
            //    }
            //}
            return new JsonResult(dingTalkEncryptor.getEncryptedMap("success"));
        }


        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> ddxx_test(string processInstanceId)
        {
            await _quotationApproveService.HandleQuotationApproved(processInstanceId);
            return new JsonResult("success");
        }
    }
}
