// 평점 표시
$(function() {
    let config = {
        spacing: "1px",
        multiColor: {
            "startColor": "#F39C12",
            "endColor": "#E74C3C"
        },
        rating: 0,
        maxValue: 10,
        numStars: 5,
        readOnly: true,
        starWidth: "17px",
    };

    $("var[data-rate]").each(function(index, item) {
        config.rating = (item.dataset.rate || 0);
        $(item).rateYo(config);
    });
});

const Youtube = {
    player: null,
    done: false,
    init: function() {
        let tag = document.createElement('script');

        tag.src = "https://www.youtube.com/iframe_api";
        let firstScriptTag = document.getElementsByTagName('script')[0];
        firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    },
    view: function(e) {
        let key = e.relatedTarget.dataset.key;
        let title = e.relatedTarget.dataset.title;
        e.target.querySelector(".modal-title").innerText = title;

        if(!this.player) {
            this.onYouTubeIframeAPIReady(key);
        }else{
            this.player.loadVideoById(key, 0, "hd1080");
        }
    },
    onYouTubeIframeAPIReady: function (id) {
        this.player = new YT.Player('player', {
            height: '480',
            width: '100%',
            videoId: id,
            events: {
                'onReady': Youtube.onPlayerReady,
                'onStateChange': Youtube.onPlayerStateChange
            }
        });
    },
    onPlayerReady: function(e) {
        e.target.playVideo();
    },
    onPlayerStateChange: function(e) {
        if (e.data === YT.PlayerState.PLAYING && !this.done) {
            setTimeout(this.stopVideo, 6000);
            this.done = true;
        }
    },
    stopVideo: function() {
        this.player.stopVideo();
    }
}

// 영화 구분 검색
$(document).on("click", "#nationK, #nationF", function () {
    document.getElementById("fMovieRank").submit();
});

// 예고편 검색
$(document).on("submit", 'form#fMovieTrailer', function () {
    this.elements["page"].value = "1";
    this.elements["sort"].selectedIndex = "";
    this.submit();
});

// 예고편 정렬
$(document).on("change", 'form#fMovieTrailer select[name="sort"]', function () {
    location.href = (this.form.action + "?sort=" + this.value);
});

// 초기화
$(document).on("click", 'button[type="reset"]', function(e) {
    let form = this.form;
    form.elements["keyword"].value = "";
    form.elements["director"].value = "";
    form.elements["s_year"].selectedIndex = "";
    form.elements["e_year"].selectedIndex = "";
    form.elements["s_open_dt"].value = "";
    form.elements["e_open_dt"].value = "";

    for(let genre of form.elements["genre[]"]) {
        genre.checked = false;
    }

    for(let type of form.elements["type[]"]) {
        type.checked = false;
    }
});

// 평점·후기 모달 열리면 초기화
$(document).on("show.bs.modal", "#trailerModal", function(e) {
    Youtube.view(e);
});

// 평점·후기 모달 닫히면 초기화
$(document).on("hide.bs.modal", "#trailerModal", function(e) {
    Youtube.stopVideo(e);
});
