import {MoveKeyframeAction, RemoveKeyframeAction, SelectionAction} from "./curveActions.js";

/**
 *
 * @param {KeyframeStateManager} keyframeStateManager
 * @param {List} kfToDelete
 * @param {ActionProcessor} actionProcessor
 * @param {keyframesContext} actionProcessorCTX
 */
export function applyActionRemove(keyframeStateManager, kfToDelete, actionProcessor, actionProcessorCTX) {
    actionProcessor.mark('delete keyframe(s)');
    actionProcessor.do(new SelectionAction(keyframeStateManager.selectedKeyframes, keyframeStateManager.curKeyframeList, keyframeStateManager.prevKeyframeList, actionProcessorCTX))
    actionProcessor.do(new RemoveKeyframeAction(kfToDelete, keyframeStateManager.NULL_KEYFRAME, actionProcessorCTX));
}

/**
 *
 * @param {D} actionLabel
 * @param {KeyframeStateManager} keyframeStateManager
 * @param {ActionProcessor} actionProcessor
 * @param {keyframesContext} actionProcessorCTX
 */
export function applyActionSelection(actionLabel, keyframeStateManager, actionProcessor, actionProcessorCTX) {
    //Applies only is there is a change
    if (!keyframeStateManager.selectedKeyframes.equals(keyframeStateManager.curKeyframeList)) { //Prevent action when no change to selection
        actionProcessor.mark(actionLabel);
        actionProcessor.do(new SelectionAction(keyframeStateManager.selectedKeyframes, keyframeStateManager.curKeyframeList, keyframeStateManager.prevKeyframeList, actionProcessorCTX))
    }
}

/**
 *
 * @param {D} actionLabel
 * @param curveSet
 * @param {ActionProcessor} actionProcessor
 * @param {keyframesContext} actionProcessorCTX
 */
export function applyActionMove(actionLabel, curveSet, actionProcessor, actionProcessorCTX) {
    actionProcessor.mark(actionLabel);
    actionProcessor.do(new MoveKeyframeAction(curveSet.curveStartValues, curveSet.curveStartIndices, curveSet.curveEndValue, curveSet.curveEndIndices, actionProcessorCTX));
}


/**
 *
 * @param {Boolean} isClickedOnGraph
 * @param {Boolean} isSelectionKeyHeld
 * @param {ActionProcessor} actionProcessor
 * @param {keyframesContext} actionProcessorCTX
 * @param {KeyframeStateManager} keyframeStateManager
 */
export function applyGlobalKeyframeDeselection(isClickedOnGraph, isSelectionKeyHeld, actionProcessor, actionProcessorCTX, keyframeStateManager) {
    if (isClickedOnGraph) {
        keyframeStateManager.observedActiveKeyframe.set(keyframeStateManager.NULL_KEYFRAME);
        if (!isSelectionKeyHeld) {
            keyframeStateManager.prevKeyframeList.copy(keyframeStateManager.selectedKeyframes);
            keyframeStateManager.curKeyframeList.reset();
            applyActionSelection('Deselection', keyframeStateManager, actionProcessor, actionProcessorCTX);
        }
    }
}