package WowneroChanBot;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

public class Memes {

    public String getLatestMeme() {
        return getImgUrlString("https://suchwow.xyz/");
    }

    public String getTopMeme() {
        return getImgUrlString("https://suchwow.xyz/leaderboards/top_posts?days=9999");
    }

    public String[] getRandomMemes() {
        return null;
    }

    private static String getImgUrlString(String postUrl) {
        Document doc = null;
        try {
            return Jsoup.connect(postUrl).userAgent("Mozilla/5.0").timeout(10000).get().select("img").get(1).absUrl("src");
        }

        catch (IOException | NullPointerException getRequestFailedException) {
            getRequestFailedException.printStackTrace();
            System.out.println("Could not retrieve image :(");
            return "null";
        }
    }

    public String getLatestMemeUrl() {
        Document doc = null;
        try {
            doc = Jsoup.connect("https://suchwow.xyz/").userAgent("Mozilla/5.0").timeout(10000).get();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Element link = doc.select("p.title is-4 > a").first();
        String url = link.attr("href");
        return url;
    }

//    private static int getNumberOfMemes() {
//
//    }

//    private String getRandomMemeUrl(int totalMemes) {
//        Random random = new Random();
//        random.nextInt(1, )
//    }
}
