using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MadRunFabric.Common;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Senparc.Weixin.MP.AdvancedAPIs;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WechatApi.Controllers
{
    [ApiVersion("6.0")]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class TemplateMessageController : BaseController
    {
        private readonly string AppId;
        private readonly string AppSecret;
        private readonly string TemplateId;
        private readonly string MessageUrl;
        public TemplateMessageController(IConfiguration configuration)
        {
            AppId = configuration["WechatStatic:AppId"];
            AppSecret = configuration["WechatStatic:AppSecret"];
            TemplateId= configuration["WechatStatic:TemplateId"];
            MessageUrl = configuration["WechatStatic:MessageUrl"];
        }

        /// <summary>
        /// 发送模板消息【需要安全验证】
        /// </summary>
        /// <returns></returns>
        [HttpGet("testsend")]
        public async Task<IActionResult> TestSendAsync(string openid)
        {
            //var url = "http://www.baidu.com";
            //var first = "你好，设备故障了！";
            //var result = await TemplateApi.SendTemplateMessageAsync(
            //AppId,
            //"ogNym1eye-hVDHjF_laRuxgbli1g",
            //"-8mPKpKQp12wVgmi3767sIb73W0VqM2Oe8dkUUNFako",
            //url,
            //   new
            //   {
            //       first = new { value = first, color = "#173177" },
            //       performance = new { value = "测试地点", color = "#173177" },
            //       time = new { value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), color = "#173177" },
            //       remark = new { value = string.IsNullOrWhiteSpace("") ? "请尽快处理！" : "", color = "#173177" },
            //   });
            //return Success("OK", result);

            if (string.IsNullOrEmpty(openid)) { openid = "ogNym1TbRgKYcwJrin0qo2W_N2AM"; }
            var url = "http://www.baidu.com";
            var result = await TemplateApi.SendTemplateMessageAsync(AppId, openid, TemplateId, url,
               new
               {
                   first = new { value = "测试发送模板消息！", color = "#173177" },
                   keyword1 = new { value = DateTime.Now.ToString("yyyyMMddHHmmssfff"), color = "#173177" },
                   keyword2 = new { value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), color = "#173177" },
                   remark = new { value = "测试消息内容！", color = "#173177" }
               });
            return Success("OK", result);
        }

        /// <summary>
        /// 发送模板消息【需要安全验证】
        /// </summary>
        /// <returns></returns>
        [HttpPost("sendtemplatemessage")]
        public async Task<IActionResult> SendTemplateMessageAsync(string openid,  string url, string jsondata, string templateid)
        {
            if (string.IsNullOrEmpty(templateid))
            {
                templateid = TemplateId;
            }
            if (MessageUrl.IndexOf("?") != -1)
            {
                url = MessageUrl + "&" + url;
            }
            else
            {
                url = MessageUrl + "?" + url;
            }

            var result = await TemplateApi.SendTemplateMessageAsync(AppId, openid, templateid, url, jsondata.ToJObject());
            return Success("OK", result);
        }


        /// <summary>
        /// 获取已添加至帐号下所有模板列表
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> GetListAsync()
        {
            var result = await TemplateApi.GetPrivateTemplateAsync(AppId);
            return Success("ok", result);
        }

        /// <summary>
        /// 获取所有模板列表
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> AddTemplatesync(string templateno)
        {
            var result = await TemplateApi.AddtemplateAsync(AppId, templateno);
            return Success("ok", result);
        }

        /// <summary>
        /// 删除模板
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> DelTemplatesync(string templateid)
        {
            var result = await TemplateApi.DelPrivateTemplateAsync(AppId, templateid);
            return Success("ok", result);
        }
    }
}
