package ctbrec.ui.sites.showup;

import static ctbrec.io.HttpConstants.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.json.JSONArray;
import org.json.JSONObject;

import ctbrec.Config;
import ctbrec.Model;
import ctbrec.UnexpectedResponseException;
import ctbrec.io.HttpException;
import ctbrec.sites.showup.Showup;
import ctbrec.sites.showup.ShowupModel;
import ctbrec.ui.SiteUiFactory;
import ctbrec.ui.tabs.PaginatedScheduledService;
import javafx.concurrent.Task;
import okhttp3.Request;
import okhttp3.Response;

public class ShowupFollowedUpdateService extends PaginatedScheduledService {

    private Showup site;
    private boolean showOnline = true;

    public ShowupFollowedUpdateService(Showup site) {
        this.site = site;
    }

    @Override
    protected Task<List<Model>> createTask() {
        return new Task<List<Model>>() {
            @Override
            public List<Model> call() throws IOException {
                SiteUiFactory.getUi(site).login();

                Request request = new Request.Builder()
                        .url(site.getBaseUrl() + "/site/favorites")
                        .header(ACCEPT, MIMETYPE_APPLICATION_JSON)
                        .header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
                        .header(X_REQUESTED_WITH, XML_HTTP_REQUEST)
                        .build();
                try (Response response = site.getHttpClient().execute(request)) {
                    if (response.isSuccessful()) {
                        String body = response.body().string();
                        JSONObject json = new JSONObject(body);
                        if (json.optString("status").equalsIgnoreCase("success")) {
                            Map<String, String> onlineModels = parseOnlineModels(json);
                            return parseFavorites(json).stream()
                                    .filter(m -> onlineModels.containsKey(m.getName()) == showOnline)
                                    .collect(Collectors.toList());
                        } else {
                            throw new UnexpectedResponseException("Request was not successful: " + body);
                        }
                    } else {
                        throw new HttpException(response.code(), response.message());
                    }
                }
            }

            private List<ShowupModel> parseFavorites(JSONObject json) {
                var favorites = new ArrayList<ShowupModel>();
                JSONArray list = json.getJSONArray("list");
                for (int i = 0; i < list.length(); i++) {
                    JSONObject m = list.getJSONObject(i);
                    ShowupModel model = new ShowupModel();
                    model.setSite(site);
                    model.setName(m.optString("username"));
                    model.setUrl(site.getBaseUrl() + '/' + model.getName());

                }
                return favorites;
            }

            private Map<String, String> parseOnlineModels(JSONObject json) {
                var onlineModels = new HashMap<String, String>();
                JSONArray online = json.getJSONArray("online");
                for (int i = 0; i < online.length(); i++) {
                    JSONObject m = online.getJSONObject(i);
                    String preview = site.getBaseUrl() + "/files/" + m.optString("big_img") + ".jpg";
                    onlineModels.put(String.valueOf(m.optLong("uid")), preview);
                }
                return onlineModels;
            }
        };
    }

    void showOnline(boolean online) {
        this.showOnline = online;
    }
}
