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

package kinosearch.core.warez;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
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;
import org.jsoup.select.Elements;

public class Onlinelife implements KinoWarez {
    private static final String DOMAIN = "http://www.online-life.cc";
    private static final String NAME = "OnlineLife";

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

    public String getDomain() {
        return "http://www.online-life.cc";
    }

    public List<Kino> search(String nameKino, boolean strong) {
        Browser browser = Tools.createBrowser();
        browser.setEncoding("windows-1251");
        String postData = "do=search&subaction=search&mode=simple&story=" + Tools.SafeUrlEncode(nameKino, "windows-1251");
        String html = browser.post("http://www.online-life.cc/?do=search", postData);
        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);
            }

            Elements elements = document.getElementsByClass("info");
            if (elements.size() > 0) {
                return Collections.emptyList();
            } else {
                List<Kino> outList = new ArrayList();

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

                    name = name.replaceAll("\\[.+", "").trim();
                    Kino kino = new Kino(name, url, this);
                    kino.setPlayer(true);
                    outList.add(kino);
                }

                return outList;
            }
        }
    }

    public String player(String page) {
        String movie_id = page.substring(1, page.indexOf("-"));
        Browser browser = Tools.createBrowser();
        browser.setEncoding("windows-1251");
        browser.setHeader("Referer", "http://dterod.com/player.php?newsid=" + movie_id);
        String html = browser.get("http://dterod.com/js.php?id=" + movie_id);
        html = html.substring(0, html.indexOf(10)).trim();
        String json = html.substring(html.indexOf(123), html.indexOf("};") + 1);
        JsonObject jsonObj = (JsonObject)(new Gson()).fromJson(json, JsonObject.class);
        if (jsonObj.has("file")) {
            String fileMp4 = jsonObj.get("file").getAsString().substring("http://".length());
            String title = jsonObj.get("comment").getAsString();
            return "{\"file\":\"/proxy/onlinelife/" + fileMp4 + "\",\"title\":\"" + title + "\"}";
        } else if (jsonObj.has("pl")) {
            browser.setEncoding("utf-8");
            return this.replaceToProxy(browser.get(jsonObj.get("pl").getAsString()));
        } else {
            return "{}";
        }
    }

    private String replaceToProxy(String json) {
        JsonObject jsonObject = (JsonObject)(new Gson()).fromJson(json, JsonObject.class);

        for(JsonElement elm1 : jsonObject.get("playlist").getAsJsonArray()) {
            for(JsonElement elm2 : elm1.getAsJsonObject().get("playlist").getAsJsonArray()) {
                JsonObject obj1 = elm2.getAsJsonObject();
                obj1.addProperty("file", obj1.get("file").getAsString().replace("http://", "/proxy/onlinelife/"));
            }
        }

        return (new Gson()).toJson(jsonObject);
    }
}
