package com.izouma.dingdong.service;

import com.tencent.xinge.XingeApp;
import com.tencent.xinge.bean.AudienceType;
import com.tencent.xinge.bean.Message;
import com.tencent.xinge.bean.MessageType;
import com.tencent.xinge.push.app.PushAppRequest;
import com.tencentyun.TLSSigAPIv2;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;

@SpringBootTest
@RunWith(SpringRunner.class)
public class UserSigServiceTest {
//    @Autowired
//    private UserSigService userSigService;

    @Test
    public void test() {
//        System.out.println(userSigService.generateUserSig("83"));
        TLSSigAPIv2 api = new TLSSigAPIv2(1400375593, "7fe225c3b41cf2e7f0c79ebaf0e3b7ec60f6c7188e1561dcfddacb4767654417");

        System.out.println(api.genSig("administrator", 30000));
    }

    @Test
    public void test1() {
//        String url1 = "https://console.tim.qq.com/v4/openim/batchsendmsg?random=99999999&contenttype=json";
//        String url2 = "&sdkappid=1400375593&identifier=administrator&usersig=eJw1jrEOgjAURf*ls4GWV0ogcXFgwmgCRgcWYot5KohtAzXGf7cBvdu9Jye5b1IVZaDcgFqRDKjPap5GpUlGooCSpRt5a4YBJckYpxSSOE5hIShVb7HFWWhkhz0aqxv70H8VL544XZScTdVrd3Z1KKAOT8oIzmyRX8VB7vPnSO*RmTbHdmvWP9Vi52*xOOUJAGP88wUitDTf";
//        url1 = url1 + url2;

        String url1 = "https://console.tim.qq.com/v4/openim/sendmsg?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json";

        String result = "";
        try {
            URL url = new URL(url1);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            // 腾讯地图使用GET
            conn.setRequestMethod("GET");
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
//            String line;
//            // 获取地址解析结果
//            while ((line = in.readLine()) != null) {
//                result += line + "\n";
//            }
            System.out.println(in);
            in.close();
        } catch (Exception e) {
            e.getMessage();
        }
        System.out.println(result);
    }

    @Test
    public void test2() {
        XingeApp xingeApp = new XingeApp.Builder()
                .appId("1400375593")
                .secretKey("7fe225c3b41cf2e7f0c79ebaf0e3b7ec60f6c7188e1561dcfddacb4767654417")
                .domainUrl("https://api.tpns.tencent.com/")
                .build();

        PushAppRequest pushAppRequest = new PushAppRequest();
        //完善PushAppRequest  消息
        pushAppRequest.setMessage_type(MessageType.notify);
        pushAppRequest.setAudience_type(AudienceType.all);

        Message message = new Message();
        message.setTitle("系统消息");
        message.setContent("提现成功！！");
        pushAppRequest.setMessage(message);

        JSONObject ret = xingeApp.pushApp(pushAppRequest);
        System.out.println(ret);
    }

    @Test
    public void test3() {
        String PayDomain = "https://console.tim.qq.com";

        String url = "/v4/all_member_push/im_push";

        String url1 = "/v4/openim/sendmsg";

        HttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(PayDomain + url1);

        Map params = new HashMap();

        //设置公共参数
        params.put("sdkappid", "1400375593");
        params.put("usersig", "eJw1jl0LgjAYhf-LbgvZ3EY66CpiXaRIRl0PN*3F-GgbYUT-vaF17s55eOC80flYRmYawRokKA5Zz9PTWCRQHGG0dKdbNY6gkSAMY7rhPKULAW16DzXMgtId9OC8VX6wfxWaQHbNdBhWhdN4eOS3smoveVWle3aXNpN5S9npWtBXLSeSbX*ihy6cIjzlCeExTj5fwBE0Ow__");
        params.put("identifier", "administrator");

        //回调
        params.put("random", 9999);
        params.put("contenttype", "json");

        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        for (Iterator iter = params.keySet().iterator(); iter.hasNext(); ) {
            String name = (String) iter.next();
            String value = String.valueOf(params.get(name));
            urlParameters.add(new BasicNameValuePair(name, value));
        }
//        String sign = KsherSign(params);
//        urlParameters.add(new BasicNameValuePair("sign", sign));
//        post.setEntity(new UrlEncodedFormEntity(urlParameters));
        try {
            HttpResponse response = client.execute(post);
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer result = new StringBuffer();
            String line = "";
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            rd.close();
            System.out.println(result.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }


//        com.alibaba.fastjson.JSONObject json = com.alibaba.fastjson.JSONObject.parseObject(result.toString());
//        boolean isVerify = KsherVerify(json.getJSONObject("data"), json.getString("sign"));
//        if (isVerify) {
//        System.out.println(result.toString());
//        } else {
//            throw new Exception("verify signature failed");
//        }
    }
}
