var historyVar = {
    "change_text":0,
    "italic":1,
    "bold":2,
    "delete":3,
}
var historyBox = [];
var redoBox = [];
var historyPosition=-1;
function addHistory(action, relatedElements, val) {
    if (historyBox.length-1 > historyPosition) {
        let lastIdx = historyBox.length-1;
        redoBox = [];
        historyBox.splice(historyPosition+1, lastIdx-historyPosition);
    }
    historyPosition++;
    historyBox[historyPosition] = [action, relatedElements, val];
    // console.log("history update", historyBox);
}

function undoHistory() {
    if (historyPosition < 0) return false;

    let prevData = historyBox[historyPosition];
    let actionType = prevData[0];
    let allIndex, data;
    switch(actionType) {
        case historyVar["change_text"]:
            // console.log("undo work duds");
            allIndex = prevData[1];
            data = prevData[2];
            for(let idx=0; idx < allIndex.length; idx++) {
                let target = $("#" + allIndex[idx]);
                if (idx == 0) {
                    redoBox.push([historyVar["change_text"], allIndex, [target.text(), data[1]]]);
                    // console.log("update redo box", redoBox);
                }
                target.css({"background-color": ""});
                target.text(data[0]);
                target.removeClass("txt-edited");
                if (data[1]) target.addClass(data[1]);
            }
            break;
        case historyVar["italic"]:
            // console.log("undo work duds");
            allIndex = prevData[1];
            data = prevData[2];
            for(let idx=0; idx < allIndex.length; idx++) {
                let target = document.getElementById(allIndex[idx]);
                if (idx == 0) {
                    redoBox.push([historyVar["italic"], allIndex, [target.style.fontStyle, target.getAttribute("italic")]]);
                    // console.log("update redo box", redoBox);
                }
                target.style.fontStyle = data[0];
                target.setAttribute("italic", data[1]);
            }
            break;
        case historyVar["bold"]:
            // console.log("undo work duds");
            allIndex = prevData[1];
            data = prevData[2];
            for(let idx=0; idx < allIndex.length; idx++) {
                let target = document.getElementById(allIndex[idx]);
                // console.log("target", allIndex[idx], target);
                if (idx == 0) {
                    redoBox.push([historyVar["bold"], allIndex, [target.style.fontStyle, target.getAttribute("bold")]]);
                    // console.log("update redo box", redoBox);
                }
                target.style.fontWeight = data[0];
                target.setAttribute("bold", data[1]);
            }
            break;
        case historyVar["delete"]:
            allIndex = prevData[1];
            data = prevData[2];
            let newEle = $(data[0]).insertAfter("#"+data[1]);
            for(let loop=0; loop < removedId.length; loop++) {
                if (removedId[loop] == newEle.id) {
                    removedId.splice(loop-1,1);
                    // console.log("removedId after splice", removedId);
                }
            }
            redoBox.push([historyVar["delete"], allIndex, []]);
            // console.log(redoBox);
            break;

    }
    historyPosition--;
    // console.log("undo to", historyPosition);
}

function redoHistory() {
    if (historyPosition == historyBox.length-1) return false;
    let redoLastIdx = redoBox.length-1;
    let nextData = redoBox[redoLastIdx];
    let allIndex, data;
    // console.log("next data");
    switch (nextData[0]) {
      case historyVar["change_text"]:
        allIndex = nextData[1];
        data = nextData[2];
        for (let idx = 0; idx < allIndex.length; idx++) {
          let target = $("#" + allIndex[idx]);
          target.css({ "background-color": "rgba(127,235,235,0.3)" });
          target.text(data[0]);
          target.addClass("txt-edited");
          if (data[1]) target.removeClass(data[1]);
        }
        redoBox.splice(redoLastIdx, 1);
        // console.log("after splace", redoBox);
        // console.log("redo work duds");
        break;
      case historyVar["italic"]:
        allIndex = nextData[1];
        data = nextData[2];
        for (let idx = 0; idx < allIndex.length; idx++) {
          let target = document.getElementById(allIndex[idx]);
          target.style.fontStyle = data[0];
          target.setAttribute("italic", data[1]);
        }
        redoBox.splice(redoLastIdx, 1);
        // console.log("redo work duds");
        break;
      case historyVar["bold"]:
        allIndex = nextData[1];
        data = nextData[2];
        for (let idx = 0; idx < allIndex.length; idx++) {
          let target = document.getElementById(allIndex[idx]);
          target.style.fontWeight = data[0];
          target.setAttribute("bold", data[1]);
        }
        redoBox.splice(redoLastIdx, 1);
        // console.log("redo work duds");
        break;
      case historyVar["delete"]:
        //   console.log("next data", nextData, redoBox);
        allIndex = nextData[1];
        document.getElementById(allIndex[0]).remove();
        redoBox.splice(redoLastIdx, 1);
        removedId.push(allIndex[0])
        break;
    }

    historyPosition++;
    // console.log("redo to", historyPosition);
}