import { getChannelIDInfo, getVideoID } from "../../maze-utils/src/video";
import Config, { ConfigurationID, CustomConfiguration } from "../config";
import { SponsorHideType, SponsorTime } from "../types";

let currentTabSkipProfile: ConfigurationID = null;

export function getSkipProfileIDForTime(): ConfigurationID | null {
    if (Config.local.skipProfileTemp !== null && Config.local.skipProfileTemp.time > Date.now() - 60 * 60 * 1000) {
        return Config.local.skipProfileTemp.configID;
    } else {
        return null;
    }
}

export function getSkipProfileIDForTab(): ConfigurationID | null {
    return currentTabSkipProfile;
}

export function setCurrentTabSkipProfile(configID: ConfigurationID | null) {
    currentTabSkipProfile = configID ?? null;
}

export function getSkipProfileIDForVideo(): ConfigurationID | null {
    return Config.local.channelSkipProfileIDs[getVideoID()] ?? null;
}

export function getSkipProfileIDForChannel(): ConfigurationID | null {
    const channelInfo = getChannelIDInfo();

    if (!channelInfo) {
        return null;
    }

    return Config.local.channelSkipProfileIDs[channelInfo.id]
        ?? Config.local.channelSkipProfileIDs[channelInfo.author]
       ?? null;
}

export function getSkipProfileID(): ConfigurationID | null {
    const configID =
        getSkipProfileIDForTime()
        ?? getSkipProfileIDForTab()
        ?? getSkipProfileIDForVideo()
        ?? getSkipProfileIDForChannel();
    
    return configID ?? null;
}

export function getSkipProfile(): CustomConfiguration | null {
    const configID = getSkipProfileID();
    
    if (configID) {
        return Config.local.skipProfiles[configID];
    }

    return null;
}

type SkipProfileBoolKey =
    "showAutogeneratedChapters"
    | "autoSkipOnMusicVideos"
    | "skipNonMusicOnlyOnYoutubeMusic"
    | "muteSegments"
    | "fullVideoSegments"
    | "manualSkipOnFullVideo";

export function getSkipProfileBool(key: SkipProfileBoolKey): boolean {
    return getSkipProfileValue<boolean>(key);
}

export function getSkipProfileNum(key: "minDuration"): number {
    return getSkipProfileValue<number>(key);
}

function getSkipProfileValue<T>(key: keyof CustomConfiguration): T {
    const profile = getSkipProfile();
    if (profile && profile[key] !== null) {
        return profile[key] as T;
    }

    return Config.config[key];
}

export function hideTooShortSegments(sponsorTimes: SponsorTime[]) {
    const minDuration = getSkipProfileNum("minDuration");

    if (minDuration !== 0) {
        for (const segment of sponsorTimes) {
            const duration = segment.segment[1] - segment.segment[0];
            if (duration > 0 && duration < minDuration && (segment.hidden === SponsorHideType.Visible || SponsorHideType.MinimumDuration)) {
                segment.hidden = SponsorHideType.MinimumDuration;
            } else if (segment.hidden === SponsorHideType.MinimumDuration) {
                segment.hidden = SponsorHideType.Visible;
            }
        }
    }
}