var totalShuffleArr = [];

function getShuffledQuestionArr(shuffleFlag) {
    if ((addedQuestions.length + addedNewQuestions.length) >= totalShuffleArr.length) //if question is added/Edited
    {
        for (var i = 0; i < addedQuestions.length; i++) {
            var indx = findQuestId(totalShuffleArr, addedQuestions[i].num)
            //console.log("getShuffledQuestionArr==1=", parseFloat(addedQuestions[i].questMark), indx)

            if (indx > -1) {
                totalShuffleArr[indx].num = addedQuestions[i].num;
                totalShuffleArr[indx].id = addedQuestions[i].id;
                totalShuffleArr[indx].questTxt = addedQuestions[i].questTxt;
                totalShuffleArr[indx].questImg = addedQuestions[i].questImg;
                totalShuffleArr[indx].questImgName = addedQuestions[i].questImgName;
                totalShuffleArr[indx].questAns = addedQuestions[i].questAns;
                totalShuffleArr[indx].questAnsImg = addedQuestions[i].questAnsImg;
                totalShuffleArr[indx].questMark = addedQuestions[i].questMark;
                totalShuffleArr[indx].title = addedQuestions[i].title;
                totalShuffleArr[indx].tg_question = addedQuestions[i].tg_question;
                totalShuffleArr[indx].addedQuest = false;
            } else
                if (indx == -1) {
                    //console.log("getShuffledQuestionArr==2=",addedQuestions[i].num, indx)
                    totalShuffleArr.push({ num: addedQuestions[i].num, id: addedQuestions[i].id, questTxt: addedQuestions[i].questTxt, questImg: addedQuestions[i].questImg, questImgName: addedQuestions[i].questImgName, questAns: addedQuestions[i].questAns, questAnsImg: addedQuestions[i].questAnsImg, questMark: addedQuestions[i].questMark, title: addedQuestions[i].title, tg_question: addedQuestions[i].tg_question, addedQuest: false });
                }
        }
        for (var j = 0; j < addedNewQuestions.length; j++) {
            var indx = findQuestId(totalShuffleArr, addedNewQuestions[j].num)
            //console.log("getShuffledQuestionArr==3=",addedNewQuestions[j].num, indx)
            if (indx > -1) {
                totalShuffleArr[indx].num = addedNewQuestions[j].num;
                totalShuffleArr[indx].id = addedNewQuestions[j].id;
                totalShuffleArr[indx].questTxt = addedNewQuestions[j].questTxt;
                totalShuffleArr[indx].questImg = addedNewQuestions[j].questImg;
                totalShuffleArr[indx].questImgName = addedQuestions[j].questImgName;
                totalShuffleArr[indx].questAns = addedNewQuestions[j].questAns;
                totalShuffleArr[indx].questAnsImg = addedNewQuestions[j].questAnsImg;
                totalShuffleArr[indx].questMark = addedNewQuestions[j].questMark;
                totalShuffleArr[indx].title = addedNewQuestions[j].title;
                totalShuffleArr[indx].tg_question = addedQuestions[j].tg_question;
                totalShuffleArr[indx].addedQuest = true;
            } else
                if (indx == -1) {
                    totalShuffleArr.push({ num: addedNewQuestions[j].num, id: addedNewQuestions[j].id, questTxt: addedNewQuestions[j].questTxt, questImg: addedNewQuestions[j].questImg, questImgName: addedNewQuestions[j].questImgName, questAns: addedNewQuestions[j].questAns, questAnsImg: addedNewQuestions[j].questAnsImg, questMark: addedNewQuestions[j].questMark, title: addedNewQuestions[j].title, addedQuest: true });
                }
        }
        totalShuffleArr = shuffleAccordingToTitle(totalShuffleArr);
    }
    //else
    if ((addedQuestions.length + addedNewQuestions.length) < totalShuffleArr.length) //if question is deleted
    {
        //console.log("DELET")
        totalShuffleArr = shuffleAccordingToTitle(totalShuffleArr);
        for (var i = 0; i < totalShuffleArr.length; i++) {
            var indx1 = findQuestId(addedQuestions, totalShuffleArr[i].num);
            var indx2 = findQuestId(addedNewQuestions, totalShuffleArr[i].num);
console.log(totalShuffleArr);

            if (online_offline) {
                deleteQuestion()
            }
            function deleteQuestion() {
                const deleteUrl = `${url}/customwebservice/tg/tgcrud.php?wsfunction=questiondelete&wstoken=${token}`;
                const postDeleteUrlData = {
                    "tg_question_id": totalShuffleArr[i].tg_question,
                    "delete_by": 1
                }
                axios.post(deleteUrl, JSON.stringify(postDeleteUrlData))
                    .then(response => {
                        console.log('Response:', response.data);

                    })
                    .catch(error => {
                        console.error('Error:', error);
                    });
            }
            console.log(indx1, indx2, "|1|", totalShuffleArr[i].num, totalShuffleArr[i].tg_question, totalShuffleArr.length);
            if (indx1 == -1 && indx2 == -1) {
                totalShuffleArr.splice(i, 1);
                break;
            }
        }
    }


    console.log("getShuffledQuestionArr==4=", totalShuffleArr)
    if (!shuffleFlag) {
        return totalShuffleArr;
    } else {
        totalShuffleArr = shuffleArr(totalShuffleArr);
        totalShuffleArr = shuffleAccordingToTitle(totalShuffleArr);

        return totalShuffleArr;
    }
}

function findQuestId(totalShuffleArr1, num) {
    var indx = -1;
    for (var i = 0; i < totalShuffleArr1.length; i++) {
        if (totalShuffleArr1[i].num == num) {
            indx = i;
            break;
        }
    }

    return indx;
}

function shuffleAccordingToTitle(shuffleArr) {
    var tempNewArr = [];
    var count = 0;
    while (shuffleArr.length > 0) {
        tempNewArr.push(shuffleArr[0]);
        shuffleArr.splice(0, 1);

        count = 0;
        while (count < shuffleArr.length) {
            if (shuffleArr[count].title == tempNewArr[tempNewArr.length - 1].title) {
                tempNewArr.push(shuffleArr[count]);
                shuffleArr.splice(count, 1);
            } else
                count++;
        }
    }
    shuffleArr = tempNewArr.concat();
    return shuffleArr;
}

function shuffleArr(array) {
    var currentIndex = array.length,
        temporaryValue, randomIndex;

    // While there remain elements to shuffle...
    while (0 !== currentIndex) {

        // Pick a remaining element...
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;

        // And swap it with the current element.
        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
    }

    return array;
}
/* 
function shuffleArr(arr)
{
    arr.sort(function() {
        return .5 - Math.random();
    });
    return arr;
} */