<div id="captcharequired">
    <p class="text-gray-800 dark:text-gray-200 font-bold">
        <i class="fa-solid fa-lock-open pr-[4px] text-yellow-400"></i> 1080p
    </p>
    <p class="text-gray-800 dark:text-gray-200">
        Please solve the following captcha:
    </p>
    <p id="message" class="text-red-600">
    </p>
    <div class="flex pt-2">
        <div id="captchaImg">
            {!! captcha_img() !!}
        </div>
        <button type="button" class="inline-flex items-center ml-2 px-2 py-2 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-500 rounded-md font-semibold text-xs text-gray-700 dark:text-gray-300 uppercase tracking-widest shadow-sm hover:bg-gray-50 dark:hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 dark:focus:ring-offset-gray-800 disabled:opacity-25 transition ease-in-out duration-150" id="reloadcaptcha" >
            <i class="fa-solid fa-rotate-right"></i>
        </button>
    </div>
    <div class="flex pt-2 mt-1">
        <x-text-input id="captcha_text" class="block " type="text" name="captcha_text"/>
        <button type="button" class="inline-flex items-center ml-2 px-2 -pt-1 bg-rose-600 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-rose-700 active:bg-rose-900 focus:outline-none focus:ring-2 focus:ring-rose-500 focus:ring-offset-2 dark:focus:ring-offset-gray-800 transition ease-in-out duration-150" id="submitcaptcha" >
            Submit
        </button>
    </div>
    <br>
    <p class="text-gray-800 dark:text-gray-200 text-sm">
        You can log in with Discord to avoid the captcha.
    </p>
</div>

<div id="captchsolved" style="display: none;">
    <p class="text-gray-800 dark:text-gray-200 font-bold">
        <i class="fa-solid fa-lock-open pr-[4px] text-green-400"></i> 1080p
    </p>
    <div id="dlbutton">
        <a href="" id="downloadEpisode">
            <button class="group rounded-md shadow bg-rose-600 text-white cursor-pointer flex justify-between items-center overflow-hidden transition-all hover:glow m-1 w-[190px] h-[65px]">
                <div class="relative w-12 h-[65px] bg-white bg-opacity-20 text-white flex justify-center items-center transition-all">
                    <svg class="w-4 h-4 transition-all group-hover:-translate-y-1" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 14l-7 7m0 0l-7-7m7 7V3"></path>
                    </svg>
                </div>
                <div class="flex flex-col text-center w-full">
                    <p class="text-xl">Episode {{ $episode->episode }}</p>
                    <p class="text-xs">HEVC MKV {{ $episode->getDownloadByType('FHD')->getFileSize() ?? '' }}</p>
                    <p class="text-xs" id="downloadCount">Downloaded {{ $episode->getDownloadByType('FHD')->count }} times</p>
                </div>
            </button>
        </a>
    </div>
</div>

<script>
    var downloadCounter = 0;
    function reloadCaptcha() {
        window.axios.get('/reload-captcha').then(function (response) {
            if (response.status == 200) {
                document.querySelector("#captchaImg").innerHTML = response.data.captcha;
            }
        }).catch(function (error) {
            console.log(error);
        });
    }

    function submitCaptcha() {
        document.querySelector("#message").innerHTML = '';
        window.axios.post('/get-download', {
            captcha: document.getElementById('captcha_text').value,
            episode_id: document.getElementById('e_id').value
        }).then(function (response) {
            document.querySelector("#captcharequired").style.display = "none";
            document.querySelector("#captchsolved").style.display = "block";
            document.querySelector("#downloadEpisode").href = '{{ $dldomains[array_rand($dldomains)] }}/'+ response.data.download_url;
            document.querySelector("#downloadCount").innerText = 'Downloaded ' + response.data.download_count + ' times';
            downloadCounter = response.data.download_count;
        }).catch(function (error) {
            if (error.response.data.message == 'validation.captcha') {
                document.querySelector("#message").innerHTML = 'Captcha Incorrect!';
                reloadCaptcha();
            } else {
                document.querySelector("#message").innerHTML = error.response.data.message;
            }
        });
    }

    function increaseDownloadCounter() {
        downloadCounter += 1;
        document.querySelector("#downloadCount").innerText = 'Downloaded ' + downloadCounter + ' times';
    }

    document.querySelector("#downloadEpisode").addEventListener("click", increaseDownloadCounter);

    document.querySelector("#reloadcaptcha").addEventListener("click", reloadCaptcha);
    document.querySelector("#submitcaptcha").addEventListener("click", submitCaptcha);
</script>
