using molilian.core;
using dodohold.core;
using Microsoft.AspNetCore.Mvc;
using Org.BouncyCastle.Ocsp;
using System.Text.Json;
using System.Runtime.InteropServices;
using System.Net;
using System.Security.Cryptography;
using Microsoft.AspNetCore.Authentication;
using TencentCloud.Ecm.V20190719.Models;
using COSXML.Network;
using System.Linq;
using static dodohold.core.ZTOExpress.CreateOrderArgs;
using MySqlX.XDevAPI;
using TencentCloud.Ame.V20190916.Models;
using YunhuiKit;

namespace molilian.api.Controllers
{
    /// <summary>
    /// 淘宝会场活动
    /// </summary>
    [ApiController]
    [Route("[controller]/[action]")]
    public class TkActivityController : ControllerBase
    {

        protected IHttpContextAccessor _accessor;
        public TkActivityController(IHttpContextAccessor accessor)
        {
            _accessor = accessor;
        }




        [HttpPost]
        public async Task<ActionResult> Parse([FromBody] JsonElement form, [FromQuery] string a = "", [FromQuery] int t = 0, [FromQuery] string sign = "")
        {
            //正文
            var page_id = form.Read("page_id", string.Empty);
            var item_id = form.Read("item_id", string.Empty);
            var strategy_id = form.Read("strategy_id", "1");
            var ip = form.Read("ip", string.Empty);
            var oaid = form.Read("oaid", string.Empty);

            //验证签名
            if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(sign))
            {
                return new APIResult(new { success = false, message = "验证错误" }, APIResultCodeEnum.Unauthorized);
            }

            DateTime time1 = t.Convert2Datetime();
            if (time1 < DateTime.Now.AddMinutes(-10))
            {
                return new APIResult(new { success = false, message = "验证错误2" }, APIResultCodeEnum.Unauthorized);
            }

            var account = await ApiAccountCore.GetOneAsync(a);
            if (account == null)
            {
                return new APIResult(new { success = false, message = "验证错误3" }, APIResultCodeEnum.Unauthorized);
            }
            string app_sign = $"{account.api_secret}@{t}".MD5();
            if (sign != app_sign)
            {
                return new APIResult(new { success = false, message = "验证错误4" }, APIResultCodeEnum.Unauthorized);
            }

            string prefix = page_id switch
            {
                "20150318020021983" or "20150318020010092" or "20150318020019405" => "rec",
                _ => "top",
            };

            return await TkActivityCore.ParseAsync(page_id, prefix, item_id, strategy_id, ip, oaid);
        }


        [HttpPost]
        public async Task<ActionResult> unsafeParse([FromBody] JsonElement form)
        {
            var page_id = form.Read("page_id", string.Empty);
            var item_id = form.Read("item_id", string.Empty);
            var ip = form.Read("ip", string.Empty);
            var oaid = form.Read("oaid", string.Empty);
            var strategy_id = form.Read("strategy_id", string.Empty);


            string prefix = page_id switch
            {
                "20150318020021983" or "20150318020010092" or "20150318020019405" => "rec",
                _ => "top",
            };

            return await TkActivityCore.ParseAsync(page_id, prefix, item_id, strategy_id, ip, oaid);
        }



    }
}