//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package kinosearch.core.warez;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import kinosearch.core.Kino;
import kinosearch.core.Tools;
import kinosearch.core.browser.Browser;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class Hdrezka implements KinoWarez {
    private static final String DOMAIN = "http://hdrezka.me";
    private static final String NAME = "HDREZKA";

    public String getName() {
        return "HDREZKA";
    }

    public String getDomain() {
        return "http://hdrezka.me";
    }

    public List<Kino> search(String nameKino, boolean strong) {
        Browser browser = Tools.createBrowser();
        browser.setEncoding("UTF-8");
        String html = browser.get("http://hdrezka.me/?do=search&subaction=search&q=" + Tools.SafeUrlEncode(nameKino, "UTF-8"));
        if (html.isEmpty()) {
            return Collections.emptyList();
        } else {
            Document document = Jsoup.parse(html);
            Pattern pattern = null;
            if (strong) {
                String nk = nameKino.toLowerCase();
                pattern = Tools.getStrongPattern(nk);
            }

            List<Kino> outList = new ArrayList();

            for(Element element : document.getElementsByClass("b-content__inline_item-cover")) {
                Element childElement = element.child(0);
                String url = childElement.attr("href");
                childElement = childElement.child(0);
                String name = childElement.attr("alt");
                if (strong) {
                    Matcher matcher = pattern.matcher(name.toLowerCase());
                    if (!matcher.find()) {
                        continue;
                    }
                }

                outList.add(new Kino(name, url, this));
            }

            return outList;
        }
    }

    public String player(String page) {
        return "";
    }
}
