package com.cnd3b.utility.qywechat.tools;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cnd3b.utility.qywechat.base.GetAccessToken;
import com.cnd3b.utility.qywechat.base.Parameter;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class CreateMenu {
    public static void main(String args[]) {
        JSONObject object = new JSONObject();

        JSONArray array = new JSONArray();

        {
            JSONObject click = new JSONObject();
            click.put("type", "click");
            click.put("name", "基本信息");
            click.put("key", "01");
            array.add(click);
        }
        {
            JSONObject click = new JSONObject();
            click.put("type", "click");
            click.put("name", "账户信息");
            click.put("key", "02");
            array.add(click);
        }

        object.put("button", array);
        CreateMenu.createMenu(object.toString(), Parameter.appid_DRP);
    }

    /**
     * 创建Menu
     *
     * @param @return
     * @param @throws IOException 设定文件
     * @return int 返回类型
     * @throws
     * @Title: createMenu
     * @Description: 创建Menu
     */
    public static String createMenu(String menu, String appid) {
        // 此处改为自己想要的结构体，替换即可
        String access_token = GetAccessToken.get();
        String action = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token="
                + access_token + "&agentid=" + appid;
        try {
            URL url = new URL(action);
            HttpURLConnection http = (HttpURLConnection) url.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            http.setDoOutput(true);
            http.setDoInput(true);
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
            http.connect();
            OutputStream os = http.getOutputStream();
            os.write(menu.getBytes(StandardCharsets.UTF_8));// 传入参数
            os.flush();
            os.close();

            InputStream is = http.getInputStream();
            int size = is.available();
            byte[] jsonBytes = new byte[size];
            is.read(jsonBytes);
            String message = new String(jsonBytes, StandardCharsets.UTF_8);
            return "返回信息" + message;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "createMenu 失败";
    }

    /**
     * 删除当前Menu
     *
     * @param @return 设定文件
     * @return String 返回类型
     * @throws
     * @Title: deleteMenu
     * @Description: 删除当前Menu
     */
    public static String deleteMenu() {
        String access_token = GetAccessToken.get();
        String action = "https://api.weixin.qq.com/cgi-bin/menu/delete? access_token="
                + access_token;
        try {
            URL url = new URL(action);
            HttpURLConnection http = (HttpURLConnection) url.openConnection();

            http.setRequestMethod("GET");
            http.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            http.setDoOutput(true);
            http.setDoInput(true);
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
            http.connect();
            OutputStream os = http.getOutputStream();
            os.flush();
            os.close();

            InputStream is = http.getInputStream();
            int size = is.available();
            byte[] jsonBytes = new byte[size];
            is.read(jsonBytes);
            String message = new String(jsonBytes, StandardCharsets.UTF_8);
            return "deleteMenu返回信息:" + message;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "deleteMenu 失败";
    }
}
