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 TencentCloud.Lighthouse.V20200324.Models;
using TencentCloud.Ame.V20190916.Models;
using TencentCloud.Omics.V20221128.Models;
using System.Threading.Channels;

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

        [HttpPost]
        public async Task<ActionResult> ChangeSuspend([FromForm] JsonElement body)
        {
            int accountId = body.Read<int>("accountId");
            string endpoint = body.Read<string>("endpoint");
            bool release = body.Read<bool>("release");
            int durationSeconds = body.Read<int>("durationSeconds");

            if (release)
            {
                TkEndpointManager.ReleaseSuspend(accountId, endpoint, notifyOtherNodes: false);
            }
            else
            {
                if (durationSeconds > 0)
                {
                    await TkEndpointManager.SuspendForDurationAsync(accountId, endpoint, TimeSpan.FromSeconds(durationSeconds), TkEndpointManager.SuspendReason.Active, notifyOtherNodes: false);
                }
                else
                {
                    await TkEndpointManager.SuspendAsync(accountId, endpoint, TkEndpointManager.SuspendReason.Active, notifyOtherNodes: false);
                }
            }

            return new APIResult(new
            {
                success = true,
                action = release ? "ReleaseSuspend" : "Suspend",
            });
        }


        [HttpPost]
        public async Task<ActionResult> GetStatus([FromForm] JsonElement body)
        {
            var ids = body.PathReadArray<int>("accountId[]");
            var result = await TkEndpointManager.GetStatus(ids);

            return new APIResult(new
            {
                success = true,
                data = result
            });
        }


    }
}
