package ctbrec.ui.sites.flirt4free;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ctbrec.Config;
import ctbrec.Model;
import ctbrec.io.HtmlParser;
import ctbrec.io.HttpException;
import ctbrec.sites.flirt4free.Flirt4Free;
import ctbrec.sites.flirt4free.Flirt4FreeModel;
import ctbrec.ui.PaginatedScheduledService;
import javafx.concurrent.Task;
import okhttp3.Request;
import okhttp3.Response;

public class Flirt4FreeUpdateService extends PaginatedScheduledService {

    private static final transient Logger LOG = LoggerFactory.getLogger(Flirt4FreeUpdateService.class);
    private static final int MODELS_PER_PAGE = 40;
    private String url;
    private Flirt4Free flirt4Free;

    public Flirt4FreeUpdateService(Flirt4Free flirt4Free, String url) {
        this.flirt4Free = flirt4Free;
        this.url = url;
    }

    @Override
    protected Task<List<Model>> createTask() {
        return new Task<List<Model>>() {
            @Override
            public List<Model> call() throws IOException {
                LOG.debug("Fetching page {}", url);
                Request request = new Request.Builder()
                        .url(url)
                        .addHeader("User-Agent", Config.getInstance().getSettings().httpUserAgent)
                        .build();
                try (Response response = flirt4Free.getHttpClient().execute(request)) {
                    if (response.isSuccessful()) {
                        List<Model> models = new ArrayList<>();
                        String body = response.body().string();
                        Elements tags = HtmlParser.getTags(body, "div#live_models div[class*=modelNumber]");
                        for (Element tag : tags) {
                            tag.setBaseUri(url);
                            String modelHtml = tag.html();
                            Element modelLink = HtmlParser.getTag(modelHtml, "a[class*=modelLink]");
                            modelLink.setBaseUri(url);
                            String href = modelLink.attr("href");
                            String name = href.substring(0, href.length()-1);
                            name = name.substring(name.indexOf('/', 1) + 1);
                            Flirt4FreeModel model = (Flirt4FreeModel) flirt4Free.createModel(name);
                            Element img = HtmlParser.getTag(modelHtml, "a[class*=modelLink] img");
                            img.setBaseUri(url);
                            if(img.hasAttr("data-image-url")) {
                                model.setPreview(img.absUrl("data-image-url"));
                            } else {
                                // background-image: url('https://cdn1.vscdns.com/images/models/samples-640x480/3241715.jpg')
                                Matcher m = Pattern.compile("background-image: url\\('(.*?)'\\)").matcher(img.attr("style"));
                                if(m.find()) {
                                    model.setPreview(m.group(1));
                                }
                            }
                            Element link = HtmlParser.getTag(modelHtml, "a.name");
                            model.setDisplayName(link.attr("title"));
                            model.setUrl(modelLink.absUrl("href"));
                            model.setDescription("");
                            String videoHost = modelLink.attr("data-video-host");
                            String modelId = modelLink.attr("data-model-id").substring(5);
                            model.setId(modelId);
                            String streamUrl = "https://manifest.vscdns.com/manifest.m3u8.m3u8?key=nil&provider=level3&secure=true&host=" + videoHost + "&model_id=" + modelId;
                            model.setStreamUrl(streamUrl);
                            model.setOnlineState(ctbrec.Model.State.ONLINE);
                            model.setOnline(true);
                            models.add(model);
                        }
                        return models.stream()
                                .skip((page-1) * MODELS_PER_PAGE)
                                .limit(MODELS_PER_PAGE)
                                .collect(Collectors.toList());
                    } else {
                        throw new HttpException(response.code(), response.message());
                    }
                }
            }
        };
    }
}
