using Edu.Model.CacheModel;
using Edu.Model.ViewModel.User;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TencentCloud.Ccc.V20200210.Models;
using TencentCloud.Common;

namespace Edu.ThirdCore.TenCloud
{
    /// <summary>
    /// 腾讯呼叫中心帮助类
    /// </summary>
    public class TenCloudCCCHelper
    {
        private static readonly long SDKAPPID = 1400617921;

        /// <summary>
        /// 创建客服账号
        /// </summary>
        /// <param name="employee"></param>
        /// <returns></returns>
        public async Task<(bool result, string message)> SetUserToCCCUser(Employee_ViewModel employee)
        {
            CreateStaffRequest req = new CreateStaffRequest
            {
                SdkAppId = SDKAPPID
            };
            SeatUserInfo seatUserInfo1 = new SeatUserInfo
            {
                Name = employee.EmployeeName,
                Mail = employee.Email,
                Phone = employee.EmployeeTel,
                Nick = employee.EmployeeName.ToCharArray()[0] + "老师",
                UserId = employee.Id.ToString(),
                StaffNumber = employee.Id.ToString()
            };
            req.Staffs = new SeatUserInfo[] { seatUserInfo1 };
            var credential = TenCloudCredential._instance;
            CreateStaffResponse resp = credential.CreateStaffSync(req);
            if (resp.ErrorStaffList != null && resp.ErrorStaffList.Length > 0)
            {
                return (false, resp.ErrorStaffList[0].Message);
            }
            else
            {
                var groupId = QueryDescribeSkillGroup();
                if (groupId == null)
                {
                    return (false, "电话客服添加成功，但是关联技能组失败");
                }
                else
                {
                    SetTenUserStaffSkillGroupList(employee.Email, groupId);
                }
                return (true, "添加成功");
            }
        }

        public void SetTenUserStaffSkillGroupList(string mail, long?[] list)
        {
            var credential = TenCloudCredential._instance;
            BindStaffSkillGroupListRequest req = new BindStaffSkillGroupListRequest
            {
                SdkAppId = SDKAPPID,
                StaffEmail = mail,
                SkillGroupList = list
            };
            BindStaffSkillGroupListResponse resp = credential.BindStaffSkillGroupListSync(req);
        }

        /// <summary>
        /// 获取技能组数组
        /// </summary>
        /// <returns></returns>
        public long?[] QueryDescribeSkillGroup()
        {
            var credential = TenCloudCredential._instance;
            DescribeSkillGroupInfoListRequest req = new DescribeSkillGroupInfoListRequest
            {
                SdkAppId = SDKAPPID,
                PageSize = 1,
                PageNumber = 100
            };
            DescribeSkillGroupInfoListResponse resp = credential.DescribeSkillGroupInfoListSync(req);
            if (resp.SkillGroupList != null && resp.SkillGroupList.Length > 0)
            {
                return resp.SkillGroupList.Select(x => x.SkillGroupId).ToArray();
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 删除坐席
        /// </summary>
        /// <param name="employee"></param>
        /// <returns></returns>
        public async Task<(bool result, string message)> DelUserToCCCUser(Employee_ViewModel employee)
        {
            var credential = TenCloudCredential._instance;
            DeleteStaffRequest req = new DeleteStaffRequest
            {
                SdkAppId = SDKAPPID,
                StaffList = new string[] { employee.Email }
            };
            DeleteStaffResponse resp = credential.DeleteStaffSync(req);
            return (resp.OnlineStaffList == null, "");
        }

        /// <summary>
        /// 获取电话客服的Token
        /// </summary>
        /// <returns></returns>
        public async Task<TenCccTokenItem> GetTenCccUserToken(string mail)
        {
            var credential = TenCloudCredential._instance;
            var tenCccTokenItem = new TenCccTokenItem();
            CreateSDKLoginTokenRequest req = new CreateSDKLoginTokenRequest
            {
                SdkAppId = SDKAPPID,
                SeatUserId = mail
            };
            CreateSDKLoginTokenResponse resp = credential.CreateSDKLoginTokenSync(req);
            tenCccTokenItem.SdkURL = resp.SdkURL;
            tenCccTokenItem.Token = resp.Token;
            return tenCccTokenItem;
        }
    }
}
