using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using XYY.Api.WarningNotice.WarningNoticeService;
using XYY.Common.Standard;
using XYY.Data.Standard.WX;
using XYY.Service.Standard.DingDing;

namespace XYY.Api.WarningNotice.Controllers
{
    [Route("[controller]/[action]")]
    [ApiController]
    public class WarningNoticeController : ControllerBase
    {
        IWXBindRepository wXBindRepository;
        IDingDingService _dingDingService;
        IWebHook wxWebHook;
        public WarningNoticeController(IDingDingService dingDingService, IWebHook wxWebHook, IWXBindRepository wXBindRepository)
        {
            _dingDingService = dingDingService;
            this.wxWebHook = wxWebHook;
            this.wXBindRepository = wXBindRepository;
        }

        /// <summary>
        /// 以文本方式通知
        /// </summary>
        /// <param name="target">通知内容</param>
        /// <returns></returns>
        public async Task<IActionResult> NoticeText(TextMessageNoticeTarget target)
        {
            var result = await Task.Run<IActionResult>(() =>
            {
                if (target.ServiceType == "钉钉")
                {
                    DingTalkWarningNoticeService dingTalkWarningNoticeService = new DingTalkWarningNoticeService(_dingDingService);
                    dingTalkWarningNoticeService.NoticeText(target);
                }
                else if (target.ServiceType == "微信")
                {
                    QYWXWebHook webHook = new QYWXWebHook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5eb4675a-773a-40a0-9bad-745501122580");
                    wxWebHook.SendMarkdown(target.TargetName, target.Content, target.TargetName, 2 * 60);
                }
                else
                {
                    return new OkObjectResult(new { IsSuccess = false, ErrMsg = "未实现该服务" });
                }
                return new OkObjectResult(new { IsSuccess = true, ErrMsg = "" });
            });
            return result;
        }

        public async Task<IActionResult> NoticeTextToUser(TextMessageNoticeTarget target)
        {
            var result = await Task.Run<IActionResult>(() =>
            {
                if (target.ServiceType == "微信")
                {
                    QYWXWebHook webHook = new QYWXWebHook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5eb4675a-773a-40a0-9bad-745501122580");
                    webHook.SendText(target.TargetName, target.Content, target.UserId);

                }
                else
                {
                    return new OkObjectResult(new { IsSuccess = false, ErrMsg = "未实现该服务" });
                }

                return new OkObjectResult(new { IsSuccess = true, ErrMsg = "" });
            });
            return result;
        }


        public async Task<IActionResult> NoticeTextToXYYCustomer(TextMessageNoticeTarget target)
        {
            if (target.CustomerId == null || target.CustomerId.Count() == 0)
                throw new Exception("传入客户ID必须不为空");
            var user = await wXBindRepository.GetWXUserId(target.UserId.Select(x => int.Parse(x)).ToArray());
            string wxid = user.First().WXUserId;
            int[] customerIds = null;
            try
            {
                customerIds = target.CustomerId.Select(x => int.Parse(x)).ToArray();
            }
            catch (Exception ex)
            {
                throw new Exception("传入客户ID必须为不为空的纯数字数组");
            }

            var userInfo = await wXBindRepository.GetCustomerWXUserId(customerIds);
            var result = await Task.Run<IActionResult>(() =>
            {
                if (target.ServiceType == "微信")
                {
                    QYWXWebHook webHook = new QYWXWebHook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5eb4675a-773a-40a0-9bad-745501122580");
                    webHook.SendMarkdownToCustomer(target.TargetName, target.Content, userInfo.Select(x => x.WXUserId).ToArray(),wxid);
                }
                else
                {
                    return new OkObjectResult(new { IsSuccess = false, ErrMsg = "未实现该服务" });
                }

                return new OkObjectResult(new { IsSuccess = true, ErrMsg = "" });
            });
            return result;

        }

        public async Task<IActionResult> NoticeTextToXYYUser(TextMessageNoticeTarget target)
        {
            if (target.UserId == null || target.UserId.Count() == 0)
                throw new Exception("传入用户ID必须不为空");

            int[] userIds = null;
            try
            {
                userIds = target.UserId.Select(x => int.Parse(x)).ToArray();
            }
            catch (Exception ex)
            {
                throw new Exception("传入用户ID必须为不为空的纯数字数组");
            }

            var userInfo = await wXBindRepository.GetWXUserId(userIds);
            var result = await Task.Run<IActionResult>(() =>
            {
                if (target.ServiceType == "微信")
                {
                    QYWXWebHook webHook = new QYWXWebHook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5eb4675a-773a-40a0-9bad-745501122580");
                    webHook.SendMarkdown(target.TargetName, target.Content, userInfo.Select(x => x.WXUserId).ToArray());
                }
                else
                {
                    return new OkObjectResult(new { IsSuccess = false, ErrMsg = "未实现该服务" });
                }

                return new OkObjectResult(new { IsSuccess = true, ErrMsg = "" });
            });
            return result;
        }


        /// <summary>
        /// 以文本方式通知
        /// </summary>
        /// <param name="target">通知内容</param>
        /// <returns></returns>
        public async Task<IActionResult> NoticeCard(ActionCardNoticeTarget target)
        {
            if (target.ServiceType == "钉钉")
            {
                DingTalkWarningNoticeService dingTalkWarningNoticeService = new DingTalkWarningNoticeService(_dingDingService);
                await dingTalkWarningNoticeService.NoticeActionCard(target);
            }
            else
            {
                return new OkObjectResult(new { IsSuccess = false, ErrMsg = "未实现该服务" });
            }
            return new OkObjectResult(new { IsSuccess = true, ErrMsg = "" });
        }
    }
}
