package ctbrec.ui.sites.flirt4free;

import static ctbrec.io.HttpConstants.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ExecutionException;

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

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.SiteUiFactory;
import ctbrec.ui.tabs.PaginatedScheduledService;
import javafx.concurrent.Task;
import okhttp3.Request;
import okhttp3.Response;

public class Flirt4FreeFavoritesUpdateService extends PaginatedScheduledService {
    private Flirt4Free flirt4free;

    public Flirt4FreeFavoritesUpdateService(Flirt4Free flirt4free) {
        this.flirt4free = flirt4free;
    }

    @Override
    protected Task<List<Model>> createTask() {
        return new Task<List<Model>>() {
            @Override
            public List<Model> call() throws IOException {
                List<Model> models = new ArrayList<>();
                String url = flirt4free.getBaseUrl() + "/my-account/secure/favorites.php?a=models&sort=online&status=all&pg=" + page;
                SiteUiFactory.getUi(flirt4free).login();
                Request request = new Request.Builder()
                        .url(url)
                        .header(ACCEPT, "*/*")
                        .header(ACCEPT_LANGUAGE, Locale.ENGLISH.getLanguage())
                        .header(REFERER, flirt4free.getBaseUrl())
                        .header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
                        .build();
                try (Response response = flirt4free.getHttpClient().execute(request)) {
                    if (response.isSuccessful()) {
                        String body = response.body().string();
                        Elements modelContainers = HtmlParser.getTags(body, "div.model-container");
                        for (Element modelContainer : modelContainers) {
                            String modelHtml = modelContainer.html();
                            Element bioLink = HtmlParser.getTag(modelHtml, "a.common-link");
                            bioLink.setBaseUri(flirt4free.getBaseUrl());
                            Flirt4FreeModel model = (Flirt4FreeModel) flirt4free.createModelFromUrl(bioLink.absUrl("href"));
                            Element img = HtmlParser.getTag(modelHtml, "a > img[alt]");
                            model.setDisplayName(img.attr("alt"));
                            model.setPreview(img.attr("src"));
                            model.setDescription("");
                            model.setOnline(modelHtml.contains("I'm Online"));
                            try {
                                model.setOnlineState(model.isOnline() ? Model.State.ONLINE : Model.State.OFFLINE);
                            } catch (InterruptedException e) {
                                Thread.currentThread().interrupt();
                            } catch (ExecutionException e) {
                                // ignore
                            }
                            models.add(model);
                        }
                        return models;
                    } else {
                        throw new HttpException(response.code(), response.message());
                    }
                }
            }
        };
    }
}
