var lastAlbum = "all";
var modelFiles = [];
var lan;
var btn;
var albumFiles;
var multi;
var exist;
var model;
var modelId;
var modelFilesName;
var files = [];

function albumPopup(inLan, inBtn, inAlbumFiles, inMulti=true, inExist=false, inModel=null, inModelId=null, inModelFilesName='Files') {
    console.log("welcome to file manager")

    lan = inLan
    btn = inBtn
    albumFiles = inAlbumFiles
    multi = inMulti
    exist = inExist
    model = inModel
    modelId = inModelId
    modelFilesName = inModelFilesName

    console.log("lan: " + lan + ", btn: " + btn + ", albumFiles: " + albumFiles + ", multi: " + multi + ", exist: " + exist + ", model: " + model + ", modelId: " + modelId )

    makePopup(lastAlbum)

}

function makePopup(albumName) {
    console.log('makePopup')
    axios
        .get("/" + lan + "/album/getsubalbums/" + albumName)
        .then((res) => {
            console.log(res.data)
            lastAlbum = res.data.album._id
            var parent = res.data.parent
            var album = res.data.album
            var subAlbums = res.data.subAlbums
            files = res.data.files

            console.log($('#printModal .modal-title'))
            $('#printModal .modal-title').text(album.Name)

            var next = "'" + parent._id.toString() + "'"
            console.log("next")
            console.log(next)

            $('#printModal .return-btn').attr('onclick','makePopup('+ next +')');
            $('#printModal .showAlbums').empty()

            for(var i=0; i < subAlbums.length; i++) {
                var next = "'" + subAlbums[i]._id.toString() + "'"
                console.log("next")
                console.log(next)
                $('#printModal .showAlbums').append('<div class="col-1" onclick="makePopup(' + next +')"><img class="w-100" src="/website/img/albumPopup/folder.png"><p class="text-center">'+ subAlbums[i].Name +'</p></div>')
            }
            for(var i=0; i < files.length; i++) {
                if(modelFiles.includes(files[i]._id)) {
                    if(files[i].Type == 0 ) {
                        $('#printModal .showAlbums').append('<div class="col-1" onclick="selectFile('+ i +', this)"><img class="w-100 activefile" src="https://ansedownload.ir/anseteam.com/panel.anseteam.com/album/' + files[i].Url +'/' + files[i].Name +'"><p class="text-center">'+ files[i].Name +'</p></div>')
                    } else if(files[i].Type == 1 ) {
                        $('#printModal .showAlbums').append('<div class="col-1" onclick="selectFile('+ i +', this)"><video class="w-100 activefile" src="https://ansedownload.ir/anseteam.com/panel.anseteam.com/album/' + files[i].Url +'/' + files[i].Name +'"></video><p class="text-center">'+ files[i].Name +'</p></div>')
                    }
                } else {
                    if(files[i].Type == 0 ) {
                        $('#printModal .showAlbums').append('<div class="col-1" onclick="selectFile('+ i +', this)"><img class="w-100" src="https://ansedownload.ir/anseteam.com/panel.anseteam.com/album/' + files[i].Url +'/' + files[i].Name +'"><p class="text-center">'+ files[i].Name +'</p></div>')
                    } else if(files[i].Type == 1 ) {
                        $('#printModal .showAlbums').append('<div class="col-1" onclick="selectFile('+ i +', this)"><video class="w-100" src="https://ansedownload.ir/anseteam.com/panel.anseteam.com/album/' + files[i].Url +'/' + files[i].Name +'"></video><p class="text-center">'+ files[i].Name +'</p></div>')
                    }
                }

            }

        })
}

function getModelFiles(lan, albumFiles, model, modelId, modelFilesName) {
    console.log('getModelFiles')
    console.log("/" + lan + "/" + model +"/file/" + modelId)
    axios
        .get("/" + lan + "/" + model +"/file/" + modelId)
        .then((res) => {
            console.log('res.data[modelFilesName]')
            console.log(res.data[modelFilesName])
            modelFiles = res.data[modelFilesName]

            for(var i=0; i < modelFiles.length; i++) {
                if (modelFiles[i].Type == 0) {
                    $('#' + albumFiles).append('<div id="' + modelFilesName + modelFiles[i]._id + '" class="profile-mail m-2 p-0" style="width: 20%; max-height: 75px; display: inline-block;"><input type="text" name="'+ modelFilesName +'" id="'+ modelFilesName +'" class="d-none" value="' + modelFiles[i]._id + '" /><img style="width: 100%; " src="https://ansedownload.ir/anseteam.com/panel.anseteam.com/album/' + modelFiles[i].Url + '/' + modelFiles[i].Name + '"><p>'+ modelFiles[i].Name +'</p><button class="btn btn-danger p-2 m-1" type="button" onclick=removeModelFile(this)" name="button"><i class="fa-solid fa-xmark"></i></button><button class="btn btn-info m-1" type="button" data-url="'+ modelFiles[i].Url +'" data-name="'+ modelFiles[i].Url +'" onclick="copyText(this)" name="button">کپی لینک</button></div>')
                } else if (modelFiles[i].Type == 1) {
                    $('#' + albumFiles).append('<div id="' + modelFilesName + modelFiles[i]._id + '" class="profile-mail m-2 p-0" style="width: 20%; max-height: 75px; display: inline-block;"><input type="text" name="'+ modelFilesName +'" id="'+ modelFilesName +'" class="d-none" value="' + modelFiles[i]._id + '" /><video style="width: 100%; " src="https://ansedownload.ir/anseteam.com/panel.anseteam.com/albumm/' + modelFiles[i].Url + '/' + modelFiles[i].Name + '"></video><p>'+ modelFiles[i].Name +'</p><button class="btn btn-danger p-2 m-1" type="button" onclick="removeModelFile(this)" name="button"><i class="fa-solid fa-xmark"></i></button><button class="btn btn-info m-1" type="button" data-url="'+ modelFiles[i].Url +'" data-name="'+ modelFiles[i].Url +'" onclick="copyText(this)" name="button">کپی لینک</button></div>')
                } else {
                    console.log("unknown format")
                }
            }
        })

    return
}

function selectFile(index, fileTag) {

    var index = index

    if (fileTag.children[0].classList.contains("activefile")) {
        fileTag.children[0].classList.remove("activefile")
        $("#" + modelFilesName + files[index]._id).remove()
    } else {
        fileTag.children[0].classList.add("activefile")

        if(multi == true) {
            if (files[index].Type == 0) {
                $('#' + albumFiles).append('<div id="' + modelFilesName + files[index]._id + '" class="profile-mail m-2 p-0" style="width: 20%; max-height: 75px; display: inline-block;"><input type="text" name="'+ modelFilesName +'" id="'+ modelFilesName +'" class="d-none" value="' + files[index]._id + '" /><img style="width: 100%; " src="https://ansedownload.ir/anseteam.com/panel.anseteam.com/album/' + files[index].Url + '/' + files[index].Name + '"><p>'+ files[index].Name +'</p><button class="btn btn-danger p-2 m-1" type="button" onclick="removeModelFile(this)" name="button"><i class="fa-solid fa-xmark"></i></button><button class="btn btn-info m-1" type="button" data-url="'+ files[index].Url +'" data-name="'+ files[index].Url +'" onclick="copyText(this)" name="button">کپی لینک</button></div>')
            } else if (files[index].Type == 1) {
                $('#' + albumFiles).append('<div id="' + modelFilesName + files[index]._id + '" class="profile-mail m-2 p-0" style="width: 20%; max-height: 75px; display: inline-block;"><input type="text" name="'+ modelFilesName +'" id="'+ modelFilesName +'" class="d-none" value="' + files[index]._id + '" /><video style="width: 100%; " src="https://ansedownload.ir/anseteam.com/panel.anseteam.com/albumm/' + files[index].Url + '/' + files[index].Name + '"></video><p>'+ files[index].Name +'</p><button class="btn btn-danger p-2 m-1" type="button" onclick="removeModelFile(this)" name="button"><i class="fa-solid fa-xmark"></i></button><button class="btn btn-info m-1" type="button" data-url="'+ files[index].Url +'" data-name="'+ files[index].Url +'" onclick="copyText(this)" name="button">کپی لینک</button></div>')
            }
        } else {
            $('#' + albumFiles).empty()
            if (files[index].Type == 0) {
                $('#' + albumFiles).append('<div id="' + modelFilesName + files[index]._id + '" class="profile-mail m-2 p-0" style="width: 20%; max-height: 75px; display: inline-block;"><input type="text" name="'+ modelFilesName +'" id="'+ modelFilesName +'" class="d-none" value="' + files[index]._id + '" /><img style="width: 100%; " src="https://ansedownload.ir/anseteam.com/panel.anseteam.com/album/' + files[index].Url + '/' + files[index].Name + '"><p>'+ files[index].Name +'</p><button class="btn btn-danger p-2 m-1" type="button" onclick="removeModelFile(this)" name="button"><i class="fa-solid fa-xmark"></i></button><button class="btn btn-info m-1" type="button" data-url="'+ files[index].Url +'" data-name="'+ files[index].Url +'" onclick="copyText(this)" name="button">کپی لینک</button></div>')
            } else if (files[index].Type == 1) {
                $('#' + albumFiles).append('<div id="' + modelFilesName + files[index]._id + '" class="profile-mail m-2 p-0" style="width: 20%; max-height: 75px; display: inline-block;"><input type="text" name="'+ modelFilesName +'" id="'+ modelFilesName +'" class="d-none" value="' + files[index]._id + '" /><video style="width: 100%; " src="https://ansedownload.ir/anseteam.com/panel.anseteam.com/album/' + files[index].Url + '/' + files[index].Name + '"></video><p>'+ files[index].Name +'</p><button class="btn btn-danger p-2 m-1" type="button" onclick="removeModelFile(this)" name="button"><i class="fa-solid fa-xmark"></i></button><button class="btn btn-info m-1" type="button" data-url="'+ files[index].Url +'" data-name="'+ files[index].Url +'" onclick="copyText(this)" name="button">کپی لینک</button></div>')
            }
        }


    }
}

function copyText(file) {

    var fileUrl = file.getAttribute("data-url");
    var fileName = file.getAttribute("data-name");

    var url = "https://ansedownload.ir/anseteam.com/panel.anseteam.com/album/" + fileUrl + "/" + fileName;
    navigator.clipboard.writeText(url);
    alert("متن کپی شد!");
}

function removeModelFile(file) {
    file.parentElement.remove()
}
