--
-- Author: E.T.A La Marchoise
-- Date: 19-08-22
-- Version: 1.0.0.0
-- Copyright (C) E.T.A La Marchoise, All Rights Reserved
-- ExtendedVinePrepuner.lua
--

ExtendedVinePrepuner = {
    SPEC_NAME = "spec_" .. g_currentModName .. ".grapevineBaseDetector"
}

function ExtendedVinePrepuner.initSpecialization()
end

function ExtendedVinePrepuner.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(VinePrepruner, specializations)
end

function ExtendedVinePrepuner.registerFunctions(vehicleType)
    --SpecializationUtil.registerFunction(vehicleType, "getExtendedVinePrepunerSpec", ExtendedVinePrepuner.getSpec)
end

function ExtendedVinePrepuner.registerOverwrittenFunctions(vehicleType)
    SpecializationUtil.registerOverwrittenFunction(vehicleType, "handleGrapevineBasePlaceable", ExtendedVinePrepuner.handleGrapevineBasePlaceable)
    SpecializationUtil.registerOverwrittenFunction(vehicleType, "getCanStartGrapevineBaseDetection", ExtendedVinePrepuner.getCanStartGrapevineBaseDetection)
    SpecializationUtil.registerOverwrittenFunction(vehicleType, "getIsValidGrapevineBasePlaceable", ExtendedVinePrepuner.getIsValidGrapevineBasePlaceable)
end

function ExtendedVinePrepuner:handleGrapevineBasePlaceable(superFunc, node, placeable, x, y, z, distance)
    if not superFunc(self, node, placeable, x, y, z, distance) then
        return false
    end

    if placeable ~= nil then
        local spec = self.spec_vinePrepruner
        local startPosX, startPosY, startPosZ = self:getFirstGrapevineBaseHitPosition()
        local currentPosX, currentPosY, currentPosZ = self:getCurrentGrapevineBaseHitPosition()
        local area = placeable:prepareVine(node, startPosX, startPosY, startPosZ, currentPosX, currentPosY, currentPosZ)

        if area > 0 then
            spec.lastWorkTime = g_time
        end

        if spec.poleAnimation.name ~= nil then
            local _, _, localStartZ = worldToLocal(node, startPosX, startPosY, startPosZ)
            local _, _, localCurrentZ = worldToLocal(node, currentPosX, currentPosY, currentPosZ)
            local direction = MathUtil.sign(localCurrentZ - localStartZ) >= 0 and 1 or -1
            local posPercentage = localCurrentZ / placeable:getPanelLength()

            if direction < 0 then
                posPercentage = 1 - posPercentage
            end

            if not self:getIsAnimationPlaying(spec.poleAnimation.name) then
                if spec.poleAnimation.poleThreshold < posPercentage then
                    if self:getAnimationTime(spec.poleAnimation.name) < 0.5 then
                        self:playAnimation(spec.poleAnimation.name, spec.poleAnimation.speedScale)
                    end
                elseif self:getAnimationTime(spec.poleAnimation.name) > 0.5 then
                    self:playAnimation(spec.poleAnimation.name, -spec.poleAnimation.speedScale)
                end
            end
        end

        for i = 1, #spec.prunerNodes do
            local prunerNode = spec.prunerNodes[i]
            prunerNode.transTarget = MathUtil.clamp((distance - prunerNode.offset) * prunerNode.direction, prunerNode.transMin, prunerNode.transMax)

            self:raiseDirtyFlags(spec.dirtyFlag)
        end

        return true
    end

    return false
end

function ExtendedVinePrepuner:getCanStartGrapevineBaseDetection(superFunc)
    if not superFunc(self) then
        return false
    end

    local isTurnedOn = self:getIsTurnedOn()

    if not isTurnedOn then
        return false
    end

    return self.movingDirection ~= 0
end

function ExtendedVinePrepuner:getIsValidGrapevineBasePlaceable(superFunc, placeable)
    if not superFunc(self, placeable) then
        return false
    end

    return placeable:getVineFruitType() == self.spec_vinePrepruner.fruitTypeIndex
end