package com.tzy.s.wx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.tzy.s.wx.dto.Accesstoken;
import com.tzy.s.wx.dto.Result;
import com.tzy.s.wx.service.WeixinApi;
import com.tzy.s.wx.service.WeixinMenuApi;
import com.tzy.s.wx.util.HttpClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.text.MessageFormat;

@Slf4j
public class WeixinMenuApiImpl implements WeixinMenuApi {

    /**
     * 刪除
     */
    private static String DELETE_WEIXIN_MENU = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token={0}";

    /**
     * 创建
     */
    private static String CREATE_WEIXIN_MENU = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}";

    /**
     * 查询
     */
    private static String LIST_WEIXIN_MENU = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token={0}";
    /**
     * 删除
     * @param accessToken
     * @return
     */
    @Override
    public Result deleteMenu(String accessToken) {
        if(StringUtils.isEmpty(accessToken))return Result.error(-1,"accesstoken不能为空");
        String url = MessageFormat.format(DELETE_WEIXIN_MENU,accessToken);
        String ret = HttpClient.httpsRequest(url,"GET",null);
        try{
            JSONObject retJson = JSONObject.parseObject(ret);
            Result result = JSONObject.toJavaObject(retJson,Result.class);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(-1,"请求异常，请联系开发人员");
        }
    }

    /**
     * 创建
     * @param accessToken
     * @return
     */
    @Override
    public Result createMenu(String accessToken, JSONObject jsonString) {
        if(StringUtils.isEmpty(accessToken))return Result.error(-1,"accesstoken不能为空");
        if(jsonString == null)return Result.error(-1,"accesstoken不能为空");
        String url = MessageFormat.format(CREATE_WEIXIN_MENU,accessToken);
//        String jsonString = GetJson.getJsonString(button);
        String ret = HttpClient.httpsRequest(url,"POST",jsonString.toString());
        try{
            JSONObject retJson = JSONObject.parseObject(ret);
            Result result = JSONObject.toJavaObject(retJson,Result.class);
            return  result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(-1,"请求异常，请联系开发人员");
        }
    }
//    /**
//     * 创建
//     * @param accessToken
//     * @param button
//     * @return
//     */
//    @Override
//    public Result createMenu(String accessToken, List<WeixinButton> button) {
//        if(StringUtils.isEmpty(accessToken))return Result.error(-1,"accesstoken不能为空");
//        if(button == null)return Result.error(-1,"accesstoken不能为空");
//        String url = MessageFormat.format(CREATE_WEIXIN_MENU,accessToken);
//        String jsonString = GetJson.getJsonString(button);
//        String ret = HttpClient.httpsRequest(url,"POST",jsonString);
//        try{
//            JSONObject retJson = JSONObject.parseObject(ret);
//            Result result = JSONObject.toJavaObject(retJson,Result.class);
//            return  result;
//        }catch (Exception e){
//            e.printStackTrace();
//            return Result.error(-1,"请求异常，请联系开发人员");
//        }
//    }

    @Override
    public JSONObject menuList(String accessToken) {
        String url = MessageFormat.format(LIST_WEIXIN_MENU,accessToken);
        String ret = HttpClient.httpsRequest(url,"GET",null);
        try{
            JSONObject retJson = JSONObject.parseObject(ret);
//            Result result = JSONObject.toJavaObject(retJson,Result.class);
            return  retJson;
        }catch (Exception e){
            e.printStackTrace();
//            return Result.error(-1,"请求异常，请联系开发人员");
        }
        return null;
    }

    public static void main(String[] args){
        WeixinApi weixinApi = new WeixinApiImpl();
        WeixinMenuApiImpl weixinMenuApi = new WeixinMenuApiImpl();
        String appid = "wx9a00dc8b9dec278d";
        String secret = "e244ef54cd19a61f771328376f4bb662";
        Result accessToken = weixinApi.getAccessToken(appid, secret);
        Accesstoken accessTokenStr = (Accesstoken)accessToken;
        String access_token = accessTokenStr.getAccess_token();
//        String access_token = "";
        JSONObject jsonObject = weixinMenuApi.menuList(access_token);
    }

}
