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;

namespace molilian.api.Controllers
{
    [ApiController]
    [Route("api/[action]")]
    public class ApiController : ControllerBase
    {
        protected IHttpContextAccessor _accessor;
        TaokeOpenCore core;
        public ApiController(IHttpContextAccessor accessor)
        {
            _accessor = accessor;
            core = new TaokeOpenCore();
        }



        [HttpPost]
        public async Task<ActionResult> PromotionQuery([FromBody] JsonElement form, [FromQuery] string a = "", [FromQuery] int t = 0, [FromQuery] string sign = "")
        {
            var img = form.Read("img", string.Empty);
            var url = form.Read("url", string.Empty);
            var ip = form.Read("ip", string.Empty);
            var oaid = form.Read("oaid", string.Empty);
            var recommand = form.Read("recommand", true);

            //验证签名
            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 = ApiAccountCore.GetOne(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);
            }

            if (!string.IsNullOrEmpty(url))
            {
                var bytes = new WebClientUtility().Request(url).ResponseBody;
                img = Convert.ToBase64String(bytes);
            }
            return await core.PromotionQueryAsync(img, oaid, ip, recommand);
        }


        [HttpPost]
        public async Task<ActionResult> unsafePromotionQuery([FromBody] JsonElement form)
        {
            var img = form.Read("img", string.Empty);
            var url = form.Read("url", string.Empty);
            var ip = form.Read("ip", string.Empty);
            var oaid = form.Read("oaid", string.Empty);
            var recommand = form.Read("recommand", true);
            var debug = form.Read("debug", false);

            if (!string.IsNullOrEmpty(url))
            {
                var bytes = new WebClientUtility().Request(url).ResponseBody;
                img = Convert.ToBase64String(bytes);
            }

            if (string.IsNullOrEmpty(oaid))
            {
                Random rand = new Random();
                oaid = $"{rand.Next(100000, 999999)}-{rand.Next(100000, 999999)}-{rand.Next(100000, 999999)}";
            }

            return await core.PromotionQueryAsync(img, oaid, ip, recommand, debug);
        }


    }
}