using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CallCenterApi.IRepositories;
using MadRunFabric.Common;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace CallCenterApi.Controllers
{
    /// <summary>
    /// 外呼任务控制器 存储获取外呼任务号码及相关信息
    /// </summary>
    [ApiVersion("6.0")]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class AuthorizeController : BaseController
    {
        private readonly ILogger<AuthorizeController> _logger;
        private readonly ISys_SystemConfigRepository _sys_systemconfigRepository;

        public AuthorizeController(ILogger<AuthorizeController> logger, ISys_SystemConfigRepository sys_systemconfigRepository)
        {
            _logger = logger;
            _sys_systemconfigRepository = sys_systemconfigRepository;
        }

        public IActionResult Index()
        {
            return View();
        }

        //授权
        [HttpPost("auth")]
        public async Task<IActionResult> AuthAsync(string tdata,int kind=0)//0时授权，1时查询授权，2查序号
        {
            //taskid = "5b63c7e3672619914f9d9338";
            #region 获取路径

            var dModel = await _sys_systemconfigRepository.GetSingle(s => s.paramcode.Equals("ACDAutoCall") & s.isdelete == false, null);
            string acdip = dModel.paramvalue;

            #endregion

            int type = kind +1;
            //if (kind == 1)
            //{ type = 2; }
            //else if (kind == 2)
            //{ type = 3; }

            string strURL = "http://" + acdip + "/v1/acd/mgr";
            var m = new
            {
                type,
                data = tdata
            };
            //实体序列化和反序列化
            string json1 = JsonConvert.SerializeObject(m);
            string strHTML = MadRunFabric.Common.HttpMethods.HttpPost(strURL, json1);
            JObject jo1 = (JObject)JsonConvert.DeserializeObject(strHTML);
            if (jo1 != null)
            {
                return Success("请求成功", jo1);
            }
            return Error("请求失败");
        }
    }
}