let radioBtn = document.querySelector(".radio-play");
let audio = document.querySelector(".radio-audio");
let radioOff = true;

let selectIndex = 0;
let videoElement = document.querySelector("#player");
let articleVideoElement = document.querySelector("#art_player");
let player = null;

function initVideo(vid, vastAdvanced) {
    if (!vastAdvanced) {
        vastAdvanced = {};
    }

    let adList = [];

    if (Array.isArray(window.ADLIST)) {
        adList = window.ADLIST;
    }

    return fluidPlayer(vid, {
        layoutControls: {
            primaryColor: "#1576b7",
            fillToContainer: true,
            mute: true,
            autoplay: true,
            controlBar: {
                autoHide: true,
                autoHideTimeout: 3,
                animated: true,
            },
        },
        vastOptions: {
            allowVPAID: true,
            adList: [
                ...adList,
                // {
                //     roll: "preRoll",
                //     vastTag: vast_xml_url,
                //     options: {
                //         tag: "preroll",
                //         index: 0,
                //     },
                // },
                // {
                //     roll: "midRoll",
                //     timer: 8,
                //     vastTag: vast_xml_url,
                //     options: {
                //         tag: "overlay",
                //         index: 0,
                //     },
                // },
                // {
                //     roll: "midRoll",
                //     timer: 41,
                //     vastTag: vast_xml_url,
                //     options: {
                //         tag: "overlay",
                //         index: 0,
                //     },
                // },
                // {
                //     roll: "midRoll",
                //     timer: 75,
                //     vastTag: vast_xml_url,
                //     options: {
                //         tag: "overlay",
                //         index: 0,
                //     },
                // },
            ],
            skipButtonCaption: "გამოტოვება [seconds]",
            skipButtonClickCaption:
                'გამოტოვება <span class="skip_button_icon"></span>',
            adText: null,
            adTextPosition: "top left",
            adCTAText: "Visit now!",
            adCTATextPosition: "bottom right",
            vastTimeout: 5000,
            showPlayButton: false,
            maxAllowedVastTagRedirects: 1,
            vastAdvanced: vastAdvanced,

            // vastAdvanced: {
            //     vastLoadedCallback: function () {},
            //     noVastVideoCallback: function () {},
            //     vastVideoSkippedCallback: function () {},
            //     vastVideoEndedCallback: function () {},
            // },
        },
    });
}

function initHomeVideoPlayer() {
    const videoList = Object.freeze(window.videoList);

    let vid = document.querySelector("#player");

    const container = vid.parentElement;
    const _vid = vid.cloneNode(true);

    const getVideoSrc = (index) => {
        return videoList[index % videoList.length];
    };

    let vast_end = false;

    const initPlayer = () => {
        vid.src = getVideoSrc(selectIndex);

        if (player) {
            player.pause();
            player.destroy();

            vid = _vid.cloneNode(true);
            container.append(vid);
        }

        player = initVideo(vid, {
            vastLoadedCallback() {
                vast_end = false;
            },
            noVastVideoCallback() {
                vast_end = true;
            },
            vastVideoSkippedCallback() {
                vast_end = true;
            },
            vastVideoEndedCallback() {
                vast_end = true;
                vid.src = getVideoSrc(selectIndex);
            },
        });

        player.on("ended", function () {
            if (videoList.indexOf(vid.src) == -1) {
                return;
            }

            selectIndex += 1;

            initPlayer();
        });

        setTimeout(() => {
            if (vid.paused) {
                player.play();
            }
        }, 500);
    };

    initPlayer();

    setTimeout(initPlayer, 500);
}

function initArticleVideoPlayer() {
    let vid = document.querySelector("#art_player");
    const container = vid.parentElement;
    const _vid = vid.cloneNode(true);

    const video_url = articleVideoElement.src;

    vid.src = video_url;

    let vast_end = false;

    initPlayer = () =>
        initVideo(vid, {
            vastLoadedCallback() {},
            noVastVideoCallback() {},
            vastVideoSkippedCallback() {},
            vastVideoEndedCallback() {
                vid.src = video_url;
                console.log(video_url);
            },
        });

    player = initPlayer();
}

if (videoElement) {
    initHomeVideoPlayer();
} else if (articleVideoElement) {
    initArticleVideoPlayer();
}

if (radioBtn) {
    if (videoElement) {
        player.on("play", () => {
            radioOff = true;
            audio.pause();
            radioBtn.classList.remove("playing");
        });
    }

    radioBtn.addEventListener("click", () => {
        radioBtn.classList.toggle("playing");
        audio.volume = 0.5;
        audio.src = window.radioStream;
        if (radioOff) {
            if (videoElement) {
                player.pause();
            }
            audio.play();
        } else {
            audio.pause();
        }
        radioOff = !radioOff;
    });
}
