if (document.getElementById("playlist-add")) {
    function addToPlaylist() {
        window.axios.post('/hentai/add-to-playlist', {
            playlist: document.getElementById('playlist').value,
            episode_id: document.getElementById('e_id').value
        }).then(function (response) {
            if (response.status == 200) {
                if (response.data.message == 'already-added') {
                    Swal.fire({
                        title: "Already added!",
                        text: "Episode was already added to that Playlist!",
                        icon: "warning"
                    });
                }

                if (response.data.message == 'success') {
                    document.getElementById("playlist-cancel").click();

                    Swal.fire({
                        title: "Success!",
                        text: "Added episode to the playlist!",
                        icon: "success"
                    });
                }
            }
        }).catch(function (error) {
            console.log(error);
            Swal.fire({
                title: "Error!",
                text: "Could not add episode to playlist.",
                icon: "error"
            });
        });
    }

    document.querySelector("#playlist-add").addEventListener("click", addToPlaylist);
}

if (document.getElementById("playlist-create-and-add")) {
    function createAndAddPlaylist() {
        const nameField = document.getElementById('playlist-name');
        const visibilityField = document.getElementById('playlist-visibility');

        if (!nameField.value.trim()) {
            Swal.fire({
                title: "Name required!",
                text: "Please enter a playlist name.",
                icon: "warning"
            });
            return;
        }

        window.axios.post('/hentai/create-playlist', {
            name: nameField.value,
            visiblity: visibilityField.value
        }).then(function (response) {
            window.axios.post('/hentai/add-to-playlist', {
                playlist: response.data.playlist_id,
                episode_id: document.getElementById('e_id').value
            }).then(function (addResponse) {
                if (addResponse.status == 200) {
                    const cancelBtn = document.getElementById("playlist-cancel");
                    if (cancelBtn) cancelBtn.click();

                    Swal.fire({
                        title: "Success!",
                        text: "Playlist created and episode added!",
                        icon: "success"
                    });
                }
            }).catch(function (error) {
                console.log(error);
                Swal.fire({
                    title: "Error!",
                    text: "Could not add episode to the new playlist.",
                    icon: "error"
                });
            });
        }).catch(function (error) {
            console.log(error);
            Swal.fire({
                title: "Error!",
                text: "Could not create playlist.",
                icon: "error"
            });
        });
    }

    document.querySelector("#playlist-create-and-add").addEventListener("click", createAndAddPlaylist);
}