package com.happy.common.http;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.happy.Model.weixin.AccessToken;
import com.happy.Model.weixin.WeiXinUtil;
import com.happy.Until.HttpUtils;
import com.happy.common.model.airticle.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.awt.*;
import java.util.List;

public class Get_airticle {

    public static List<Item_content> get(int page, int rows) throws Exception {
        Gson gson = new Gson();
        int start = (page - 1) * rows;// 每页的起始下标
        String access_token = "";
        JSONObject json = new JSONObject();
        json.put("grant_type", "client_credential");
        json.put("appid", "wx19d0b6ab36feedf4");
        json.put("secret","c16af9c2f93e48f8ae9a5492beb261f4");
        String url = "https://api.weixin.qq.com/cgi-bin/stable_token";
        access_token = HttpsClient.sendJson(url,json);
        access_token = gson.fromJson(access_token, AccessToken.class).getAccess_token();
        JSONObject data = new JSONObject();
        data.put("offset", start);
        data.put("count", rows);
        data.put("no_content", 0);
        String msg =  HttpsClient.sendJson("https://api.weixin.qq.com/cgi-bin/draft/batchget?access_token="+access_token, data);
        Item item = JSONObject.parseObject(msg,Item.class);
        return item.getItem();
    }

    public static int getTotal() throws Exception {
        Gson gson = new Gson();
        String access_token = "";
        JSONObject json = new JSONObject();
        json.put("grant_type", "client_credential");
        json.put("appid", "wx19d0b6ab36feedf4");
        json.put("secret","c16af9c2f93e48f8ae9a5492beb261f4");
        String url = "https://api.weixin.qq.com/cgi-bin/stable_token";
        access_token = HttpsClient.sendJson(url,json);
        System.out.println(access_token);
        access_token = gson.fromJson(access_token, AccessToken.class).getAccess_token();
        JSONObject data = new JSONObject();
        String msg =  HttpsClient.sendJson("https://api.weixin.qq.com/cgi-bin/draft/count?access_token="+access_token, data);
        System.out.println(msg);
        New_count item = JSONObject.parseObject(msg, New_count.class);
        return item.getTotal_count();
    }

    public static Content getByMediaId(String media_id) throws Exception {
        Gson gson = new Gson();
        String access_token = "";
        JSONObject json = new JSONObject();
        json.put("grant_type", "client_credential");
        json.put("appid", "wx19d0b6ab36feedf4");
        json.put("secret","c16af9c2f93e48f8ae9a5492beb261f4");
        String url = "https://api.weixin.qq.com/cgi-bin/stable_token";
        access_token = HttpsClient.sendJson(url,json);
        access_token = gson.fromJson(access_token, AccessToken.class).getAccess_token();
        JSONObject data = new JSONObject();
        data.put("media_id", media_id);
        String msg =  HttpsClient.sendJson("https://api.weixin.qq.com/cgi-bin/draft/get?access_token="+access_token, data);
        return JSONObject.parseObject(msg,Content.class);
    }

    public static String getFirstImg(String content){
        Document doc = Jsoup.parse(content);
        Elements links = doc.select("img[data-src]");
        return links.get(0).attr("data-src");
    }

}
