using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Senparc.Weixin.MP;
using Senparc.Weixin.MP.CommonAPIs;
using Senparc.Weixin.MP.Containers;
using Senparc.Weixin.MP.Entities.Menu;
using ServiceWechat.Api.Controllers.Base;

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

namespace ServiceWechat.Api.Controllers.WechatApis
{
    [Route("wechatapi/Menu/[action]")]
    public class MenuController : BaseController
    {
        private readonly string Token;
        private readonly string AppId;
        private readonly string AppSecret;
        private readonly string EncodingAESKey;
        public MenuController(IConfiguration configuration)
        {
            Token = configuration["WechatStatic:Token"];
            AppId = configuration["WechatStatic:AppId"];
            AppSecret = configuration["WechatStatic:AppSecret"];
            EncodingAESKey = configuration["WechatStatic:EncodingAESKey"];
        }


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

        /// <summary>
        /// 创建/更新自定义菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult CreateMenus()
        {
            ButtonGroup btnGroup = new ButtonGroup();

            //单击
            btnGroup.button.Add(new SingleClickButton()
            {
                name = "单击测试",
                key = "OneClick",
                type = ButtonType.click.ToString(),//默认已经设为此类型，这里只作为演示

            });

            //二级菜单
            var subButton = new SubButton()
            {
                name = "二级菜单"
            };
            subButton.sub_button.Add(new SingleClickButton()
            {
                key = "SubClickRoot_Text",
                name = "返回文本"
            });
            subButton.sub_button.Add(new SingleClickButton()
            {
                key = "SubClickRoot_News",
                name = "返回图文"
            });
            subButton.sub_button.Add(new SingleClickButton()
            {
                key = "SubClickRoot_Music",
                name = "返回音乐"
            });
            subButton.sub_button.Add(new SingleViewButton()
            {
                url = "http://weixin.senparc.com",
                name = "Url跳转"
            });
            btnGroup.button.Add(subButton);

            var result = CommonApi.CreateMenu(AppId, btnGroup);
            return new JsonResult(result);
        }

        /// <summary>
        /// 删除自定义菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult DeleteMenus()
        {
            var result = CommonApi.DeleteMenu(AppId);
            return new JsonResult(result);
        }
    }
}
