var waitingForLicenseUpdateFunction = function() {};
var licenseUpdateFunction = function() {};

targetWidth = 700;
targetHeight = 628;
if (typeof hero !== 'undefined') {
    hero.minWidth = targetWidth;
    hero.maxWidth = targetWidth;
    hero.minHeight = targetHeight;
    hero.maxHeight = targetHeight;
}

function updateTrialStatus(trialStatus) {
    currentTrialStatus = trialStatus;
    var visibleFreeTrial;
    if (trialStatus.canStartTrial) {
        visibleFreeTrial = "TrialAvailable";
    } else if (trialStatus.isInTrialMode || trialStatus.isUnlicensed) {
        visibleFreeTrial = "Trial";
        document.getElementById("localizedTrialStatusTitle").innerText = trialStatus.trialStatusTitle;
        document.getElementById("localizedTrialStatusSubtitle").innerText = "(" + trialStatus.trialStatusSubtitle + ")";
        var button = document.getElementById('beginFreeTrialBlock');
        if (trialStatus.isUnlicensed) {
            visibleFreeTrial = "Over";
            button.classList.add('inactive');
        } else {
            button.classList.remove('inactive');
        }
    } else {
        visibleFreeTrial = "Licensed";
        shouldSkipBuyRestorePage = true;
        var buyButton = document.getElementById('buyRestoreBlock');
        buyButton.style.display = "none";
        document.getElementById("buyRestorePage").classList.add('centerMe');
        // document.getElementById("subfooter").classList.add('remove');
        document.getElementById("trialStarted").classList.add('remove');
    }
    showFreeViewer(trialStatus.isUnlicensed);
    showFreeTrial(visibleFreeTrial);
    licenseUpdateFunction(trialStatus);
}

function showFreeTrial(visibleFreeTrial) {
    DebugLog("showFreeTrial(\"" + visibleFreeTrial + "\")");
    for (var beginFreeTrial of ["TrialAvailable", "Trial", "Licensed", "Over"]) {
        const blockId = "beginFreeTrialBlock" + beginFreeTrial;
        document.getElementById(blockId).style.display = beginFreeTrial == visibleFreeTrial ? "block" : "none";
    }
}

function showFreeViewer(shouldShowFreeViewer) {
    DebugLog("showFreeViewer(\"" + shouldShowFreeViewer + "\")");
//    const blockId = "subfooter";
    // change to this when we have a workingn localizedTrialSubtitle substitution...
     const blockId = "viewerBlock"; 
    document.getElementById(blockId).style.display = shouldShowFreeViewer ? "flex" : "none";
    const blockIdTwo = "trialStarted";
    document.getElementById(blockIdTwo).style.display = shouldShowFreeViewer ? "none" : "block";
    DebugLog("showFreeViewer(\"" + shouldShowFreeViewer + "\"): DONE");
}

function beginFreeTrialAction() {
    if (currentTrialStatus.canStartTrial || !currentTrialStatus.isUnlicensed) {
        callback("beginFreeTrial");
    }
}

function buyRestoreAction() {
    callback("buyRestore");
}

function viewerAction() {
    callback("viewer");
}

function DebugLog(parameter) {
    console.log(parameter);
    // hero.callback = "log:" + String(parameter);
}

function callback(parameter) {
    DebugLog("hero.callback(\"" + parameter + "\")");
    if (typeof hero !== 'undefined') {
        hero.callback = parameter;
    }
}

function setupVideo() {
    var appVideo = document.getElementById("appVideo");

    // For customers who don't speak English, set up the default subtitle track based on their preferred language
    var userLanguage = navigator.language;
    if (!userLanguage.startsWith("en")) {
        var tracks = appVideo.getElementsByTagName("track");
        for (var i = 0; i < tracks.length; i++) {
            var trackTag = tracks[i];
            var trackLanguage = trackTag.srclang;
            if (trackLanguage == userLanguage) {
                trackTag.default = true;
                return; // Short circuit once we've found a match
            }
        }
        // We didn't find an exact language match, but let's see if we can find a match for the main language code (e.g. "en_US" becomes "en")
        var mainLanguage = userLanguage.slice(0, 2);
        for (var i = 0; i < tracks.length; i++) {
            var trackTag = tracks[i];
            var trackLanguage = trackTag.srclang;
            if (trackLanguage.startsWith(mainLanguage)) {
                trackTag.default = true;
                return; // Short circuit once we've found a match
            }
        }
    }
}

function setupLocalizedText(localizedText) {
    function updateLocalizedLabel(labelId) {
        const value = localizedText[labelId];
        if (value) {
            const element = document.getElementById(labelId);
            if (!element) {
                throw "Missing element: " + labelId
            }
            if (labelId.endsWith("HTML")) {
                element.innerHTML = value;
            } else {
                element.innerText = value;
            }
        }
    }

    for (var key in localizedText) {
        if (localizedText.hasOwnProperty(key)) {
            updateLocalizedLabel(key);
        }
    }
}

function setupWindow() {
    updateTrialStatus({"isInTrialMode":true, "canStartTrial":false, "isUnlicensed":false, "trialStatusSubtitle":"14 days remaining", "trialStatusTitle":"Trial Has Begun"});
    setupVideo();
    
    if (typeof hero !== 'undefined' && hero.hasDarkAppearance()) {
        document.body.classList.add('dark-mode');
    } else {
        document.body.classList.remove('dark-mode');
    }
}
