using System.IO;
using System.Linq;
using System.Text;
using MadRunFabric.Common;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using NLog;
using Senparc.Weixin.MP.CommonAPIs;
using Senparc.Weixin.MP.Entities.Menu;
using WechatApi.Model;
using MadRunFabric.Model;
using Microsoft.Extensions.Logging;

// 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/Menu/[action]")]
    public class MenuController : BaseController
    {
        private readonly string Token;
        private readonly string AppId;
        private readonly string AppSecret;
        private readonly string EncodingAESKey;
        private readonly string MenuJson;
        private readonly ILogger<MenuController> _logger;

        public MenuController(ILogger<MenuController> logger, IConfiguration configuration)
        {
            _logger = logger;
            Token = configuration["WechatStatic:Token"];
            AppId = configuration["WechatStatic:AppId"];
            AppSecret = configuration["WechatStatic:AppSecret"];
            EncodingAESKey = configuration["WechatStatic:EncodingAESKey"];
            MenuJson= configuration["WechatStatic:MenuJson"];
        }


        /// <summary>
        /// 获取自定义菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMenus()
        {
            var result = CommonApi.GetMenu(AppId);
            return Success("", result);
        }

        /// <summary>
        /// 创建/更新自定义菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult CreateMenus(string btnStr = "")
        {
            if (string.IsNullOrEmpty(btnStr))
            {
                btnStr = MenuJson;
            }

            var buttons = btnStr.ToList<MenuButton>();

            ButtonGroup btnGroup = new ButtonGroup();

            foreach (var button in buttons)
            {
                BaseButton baseButton = null;
                SubButton subButton = null;
                SingleButton singleButton = null;
                if (button.sub_button.Count > 0)
                {
                    subButton = new SubButton { name = button.Name };

                    foreach (var sub_button in button.sub_button)
                    {
                        if (sub_button.Type == "view")
                        {
                            singleButton = new SingleViewButton()
                            {
                                url = sub_button.Url,
                                name = sub_button.Name
                            };
                        }
                        else
                        {
                            singleButton = new SingleClickButton()
                            {
                                name = sub_button.Name,
                                key = sub_button.Key,
                                type = sub_button.Type,
                            };
                        }
                        subButton.sub_button.Add(singleButton);
                    }
                }
                else
                {
                    if (button.Type == "view")
                    {
                        baseButton = new SingleViewButton()
                        {
                            url = button.Url,
                            name = button.Name
                        };
                    }
                    else
                    {
                        baseButton = new SingleClickButton()
                        {
                            name = button.Name,
                            key = button.Key,
                            type = button.Type,
                        };
                    }
                }
                if (subButton != null && subButton.sub_button.Count > 0)
                {
                    btnGroup.button.Add(subButton);
                }
                else
                {
                    btnGroup.button.Add(baseButton);
                }
            }

            var result = CommonApi.CreateMenu(AppId, btnGroup);
            return Success("设置成功");
        }

        /// <summary>
        /// 删除自定义菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Delete()
        {
            var result = CommonApi.DeleteMenu(AppId);
            return Success("", result);
        }
    }
}
