{% extends "base.html" %}

{% block content %}
    <div class="w3-row" style="text-align: center">
        <div class="w3-col s1 w3-hide-small m1 l2"><p></p></div>
    <div class="w3-col s12 m10 l8">

    <video id="player" class="video-js vjs-big-play-centered vjs-16-9" style="display: inline-block; width:100%" controls preload="auto"
           poster="https://image.tmdb.org/t/p/original{{ episode.still_path }}" data-setup="{}">
        <source src="{{ url_for("tv_movies.index") }}/get_episode/{{ episode.tmdb_id }}" type="video/webm">
        <p class='vjs-no-js'>
          To view this video please enable JavaScript, and consider upgrading to a web browser that
          <a href='https://videojs.com/html5-video-support/' target='_blank'>supports HTML5 video</a>
        </p>
    </video>
    {% for episode in episodes %}
        {% if episode.season == season_num and episode.episode == episode_num %}
            {% if loop.index0 - 1 >= 0 %}
                <form style="display: inline-block; float: left; margin-top: 5px" action="" method="get">
                    <input name="season" value="{{ episodes[loop.index0 - 1].season }}" hidden>
                    <input name="episode" value="{{ episodes[loop.index0 - 1].episode }}" hidden>
                <button type="submit">Prev</button>
                </form>
            {% endif %}
            {% if loop.index0 + 1 < episodes|length %}
                <form style="display: inline-block; float: right; margin-top: 5px" action="" method="get">
                    <input name="season" value="{{ episodes[loop.index0 + 1].season }}" hidden>
                    <input name="episode" value="{{ episodes[loop.index0 + 1].episode }}" hidden>
                <button type="submit">Next</button>
                </form>
            {% endif %}
                <h1 style="display: inline-block">Episode {{ episode.episode }}: {{ episode.title }}</h1>

        {% endif %}
    {% endfor %}
    <p style="text-align: left">{{ episode.description }}</p>
    <a class="btn btn-primary" href="{{ url_for("tv_movies.index") }}/get_episode/{{ episode.tmdb_id }}" download="{{ episode.title }}">Download</a>

    {% with %}
        {% set seasons = [] %}
        <form action="" method="get" class="form-row">
        <div class="col-auto">
            <select name="season" onchange='this.form.submit()' class="custom-select">
             {% for episode in episodes %}
                {% if episode.season not in seasons %}
                    <option value="{{ episode.season }}" {% if episode.season == season_num %}selected{% endif %}>
                        Season {{ episode.season }}
                    </option>
                    {{ seasons.append(episode.season) }}
                {% endif %}
            {% endfor %}
            </select>
        </div>
        <button type="submit" hidden>submit</button>
        </form>
    {% endwith %}
    <div id="episode_list" style="overflow: auto; white-space: nowrap">
    <form action="" method="get">
    <input name="season" value="{{ season_num }}" hidden>
    {% for episode in episodes %}
        {% if episode.season == season_num %}
            <button id="{{ episode.episode }}" name="episode" value="{{ episode.episode }}" onclick="this.form.submit()" style="border: none">
                <div class="w3-display-container">
                    <img src="https://image.tmdb.org/t/p/original{{ episode.still_path }}" width="350px">
                    <div class="w3-display-topleft w3-container w3-round" style="background: rgba(105,105,105,0.61);color: white">Episode {{ episode.episode }}</div>
                    <div class="w3-display-bottommiddle w3-container w3-round" style="background: rgba(105,105,105,0.61);color: white">{{ episode.title }}</div>
                    {% for data in user_tv_show_data %}
                        {% if data.tmdb_id == episode.tmdb_id and data.finished %}
                            <div class="w3-display-topright w3-container" style="background: rgba(105,105,105,0.61); border-radius: 5px 0 0 5px">
                                <img src="/static/svg/verified.svg" >
                            </div>
                        {% endif %}
                    {% endfor %}
                </div>
                {% for data in user_tv_show_data %}
                    {% if data.tmdb_id == episode.tmdb_id and data.time != 0 and data.length != 0 %}
                        <div class="w3-light-gray">
                            <div class="w3-red" style="height: 5px; width: {{ (data.time/data.length)*100 }}%"></div>
                        </div>
                    {% endif %}
                {% endfor %}
            </button>
        {% endif %}
    {% endfor %}
    </form>
    </div>
    </div>
    <div class="w3-col s1 w3-hide-small m1 l2"><p></p></div>
    </div>

{% endblock %}

{% block footer_content %}
    <div class="container">
        <img src="/static/svg/tmdb.svg" alt="" style="height: 40px;">
        <p>This product uses the TMDb API but is not endorsed or certified by TMDb.</p>
    </div>
    <script>
    function findPos(obj) {
        var curtop = 0;
        if (obj.offsetParent) {
            do {
                curtop += obj.offsetLeft;
            } while (obj = obj.offsetParent);
        return curtop;
        }
    }
    let episode = document.getElementById({{ episode_num }});
    let width = episode.offsetWidth;
    document.getElementById("episode_list").scrollLeft = findPos(episode)-(width*2);
    videojs("player").ready(function(){
        let myPlayer = videojs.getPlayer("player");
        let saved_time = {{ user_data.time }};
        let length = -1;
        let end_time = -1;
        let finished = {{ user_data.finished|string|lower }}; //converts python bool into javascript bool
        if (saved_time > 5 && !finished) {
            myPlayer.currentTime(saved_time);
        }

        myPlayer.on("loadedmetadata", function () {
            length = myPlayer.duration();
            end_time = length-15;
            {% if "credits" in chapters.keys() %}
            end_time = {{ chapters["credits"]["start"] }};
            {% endif %}
        });

        function reqListener() {
            console.log(this.responseText);
        }

        let time = myPlayer.currentTime();
        myPlayer.on("timeupdate", function () {
            if (myPlayer.currentTime()-time >= 10) {
                finished = myPlayer.currentTime() >= end_time;
                //length = myPlayer.duration();
                let oReq = new XMLHttpRequest();
                oReq.addEventListener("load", reqListener);
                oReq.open("POST", "https://rpi.narnian.us/tv_movies/{{ episode.tmdb_id }}?time="+(time+5)+"&parent={{ episode.parent_tmdb_id }}&length="+length+"&finished="+finished);
                oReq.send();
                time = myPlayer.currentTime();
        }
        });
        myPlayer.on("pause", function () {
            finished = myPlayer.currentTime() >= end_time;
            //length = myPlayer.duration();
            let oReq = new XMLHttpRequest();
            oReq.addEventListener("load", reqListener);
            oReq.open("POST", "https://rpi.narnian.us/tv_movies/{{ episode.tmdb_id }}?time="+(time+5)+"&parent={{ episode.parent_tmdb_id }}&length="+length+"&finished="+finished);
            oReq.send();
            time = myPlayer.currentTime();
        });

    });
    </script>
{% endblock footer_content %}
