package ctbrec.ui.sites.chaturbate;

import ctbrec.Config;
import ctbrec.sites.chaturbate.Chaturbate;
import ctbrec.ui.sites.AbstractTabProvider;
import ctbrec.ui.tabs.PaginatedScheduledService;
import ctbrec.ui.tabs.ThumbOverviewTab;
import javafx.scene.Scene;
import javafx.scene.control.Tab;

import java.util.ArrayList;
import java.util.List;

public class ChaturbateTabProvider extends AbstractTabProvider {

    private final String apiUrl;
    private final ChaturbateFollowedTab followedTab;
    private final boolean regionNAEnabled; // Store the setting

    public ChaturbateTabProvider(Chaturbate chaturbate) {
        super(chaturbate);
        apiUrl = site.getBaseUrl() + "/api/ts";
        regionNAEnabled = Config.getInstance().getSettings().filterNAcamsOnly; // Load setting from Config
        this.followedTab = new ChaturbateFollowedTab("Followed", buildUrl("/roomlist/room-list/?enable_recommendations=false&follow=true"), chaturbate);
    }

    @Override
    protected List<Tab> getSiteTabs(Scene scene) {
        List<Tab> tabs = new ArrayList<>();
        tabs.add(createTab("Featured", buildUrl("/roomlist/room-list/?enable_recommendations=false")));
        tabs.add(createTab("Female", buildUrl("/roomlist/room-list/?enable_recommendations=false&genders=f")));
        tabs.add(createTab("New Female", buildUrl("/roomlist/room-list/?enable_recommendations=false&genders=f&new_cams=true")));
        tabs.add(createTab("Milf", buildUrl("/roomlist/room-list/?enable_recommendations=false&hashtags=milf")));
        tabs.add(createTab("Teen", buildUrl("/roomlist/room-list/?enable_recommendations=false&hashtags=teen")));
        tabs.add(createTab("Creampie", buildUrl("/roomlist/room-list/?enable_recommendations=false&hashtags=creampie")));
        tabs.add(createTab("BBW", buildUrl("/roomlist/room-list/?enable_recommendations=false&hashtags=bbw")));
        tabs.add(createTab("Chubby", buildUrl("/roomlist/room-list/?enable_recommendations=false&hashtags=chubby")));
        tabs.add(createTab("Pregnant", buildUrl("/roomlist/room-list/?enable_recommendations=false&hashtags=pregnant")));
        tabs.add(createTab("Male", buildUrl("/roomlist/room-list/?enable_recommendations=false&genders=m")));
        tabs.add(createTab("New Male", buildUrl("/roomlist/room-list/?enable_recommendations=false&genders=m&new_cams=true")));
        tabs.add(createTab("Couples", buildUrl("/roomlist/room-list/?enable_recommendations=false&genders=c")));
        tabs.add(createTab("Trans", buildUrl("/roomlist/room-list/?enable_recommendations=false&genders=t")));
        tabs.add(createTab("N.American Cams", buildUrl("/roomlist/room-list/?enable_recommendations=false&regions=NA")));
        tabs.add(createTab("6TPM private", buildUrl("/roomlist/room-list/?enable_recommendations=false&private_prices=6")));
        tabs.add(createTab("Private", buildUrl("/roomlist/room-list/?enable_recommendations=false&private=true")));
        tabs.add(createTab("Hidden", buildUrl("/roomlist/room-list/?enable_recommendations=false&hidden=true")));
        tabs.add(createTab("Gaming", buildUrl("/roomlist/room-list/?enable_recommendations=false&gaming=true")));

        followedTab.setScene(scene);
        followedTab.setRecorder(recorder);
        followedTab.setImageAspectRatio(9.0 / 16.0);
        tabs.add(followedTab);

        tabs.add(createApiTab("Top Rated", buildUrl("/discover/carousels/top-rated/")));
        tabs.add(createApiTab("Trending", buildUrl("/discover/carousels/trending/")));

        return tabs;
    }

    @Override
    public Tab getFollowedTab() {
        return followedTab;
    }

    private Tab createTab(String title, String url) {
        var updateService = new ChaturbateUpdateService(url, false, (Chaturbate) site);
        return createTab(title, updateService);
    }

    private Tab createTab(String title, PaginatedScheduledService updateService) {
        var tab = new ThumbOverviewTab(title, updateService, site);
        tab.setRecorder(recorder);
        tab.setImageAspectRatio(9.0 / 16.0);
        return tab;
    }

    private Tab createApiTab(String title, String apiUrl) {
        var updateService = new ChaturbateApiUpdateService(apiUrl, (Chaturbate) site);
        return createTab(title, updateService);
    }

    // Helper function to append region filter if enabled
    private String buildUrl(String endpoint) {
        return apiUrl + endpoint + (regionNAEnabled ? "&region=NA" : "");
    }
}