require "TimedActions/ISBaseTimedAction"
local TABAS_Utils = require "TABAS_Utils"

TABAS_WalkInTheTubAction = ISBaseTimedAction:derive("TABAS_WalkInTheTubAction")

function TABAS_WalkInTheTubAction:isValid()
    return true
end

local function switchObjectSolidrans(object, unSet)
    if not object then return end
    if instanceof(object, "IsoThumpable") then
        if unSet then
            object:setBlockAllTheSquare(false)
        else
            object:setBlockAllTheSquare(true)
        end
    end
    local prop = object:getSprite():getProperties()
    if unSet and prop:Is(IsoFlagType.solidtrans) then
        prop:UnSet(IsoFlagType.solidtrans)
    else
        prop:Set(IsoFlagType.solidtrans)
        object:getSquare():RecalcPropertiesIfNeeded()
    end
end

function TABAS_WalkInTheTubAction:getFacingDirection()
    local square = self.character:getCurrentSquare()
    if (square:getX() - self.targetSqr:getX()) <= -1 then
        return IsoDirections.E
    elseif (square:getX() - self.targetSqr:getX()) >= 1 then
        return IsoDirections.W
    elseif (square:getY() - self.targetSqr:getY()) <= -1 then
        return IsoDirections.S
    else
        return IsoDirections.N
    end
end

function TABAS_WalkInTheTubAction:waitToStart()
    local currentSquare = self.character:getCurrentSquare()
    if currentSquare:getX() == self.targetSqr:getX() and currentSquare:getY() == self.targetSqr:getY() then
        return false
    end
    self.character:faceLocationF(self.targetSqr:getX() + 0.5, self.targetSqr:getY()+0.5)
    return self.character:shouldBeTurning()
end

function TABAS_WalkInTheTubAction:update()
    if self.character:pressedMovement(true) or self.character:pressedCancelAction() then
        self:forceStop()
        self.stopped = true
    end
    if self.stopped then return end
    if self.direction == IsoDirections.S then
        local diff = self.character:getY() - (self.targetSqr:getY()+0.5)
        if diff >= -0.025 then
            self:forceComplete()
        end
    elseif self.direction == IsoDirections.E then
        local diff = self.character:getX() - (self.targetSqr:getX()+0.5)
        if diff >= -0.025 then
            self:forceComplete()
        end
    elseif self.direction == IsoDirections.N then
        local diff = self.character:getY() - (self.targetSqr:getY()+0.5)
        if diff <= 0.025 then
            self:forceComplete()
        end
    else
        local diff = self.character:getX() - (self.targetSqr:getX()+0.5)
        -- print("Diff = " .. tostring(diff))
        if diff <= 0.025 then
            self:forceComplete()
        end
    end
end

function TABAS_WalkInTheTubAction:start()
    switchObjectSolidrans(self.object_1, true)
    switchObjectSolidrans(self.object_2, true)
    self.direction = self:getFacingDirection()
    self:setActionAnim("WalkInTheTub")
    if self.tfc_Base then
        local ratio = self.tfc_Base:getRatio()
        if ratio > self.tfc_Base.filledConst.HalfLow then
            self.sound = self.character:getEmitter():playSound("tabas_bath_wave01")
        elseif ratio > self.tfc_Base.filledConst.Low then
            self.sound = self.character:getEmitter():playSound("tabas_walkintub01")
        end
    end
end

-- function TABAS_WalkInTheTubAction:stopSound()
--     if self.sound and self.character:getEmitter():isPlaying(self.sound) then
--         self.character:stopOrTriggerSound(self.sound)
--     end
-- end

function TABAS_WalkInTheTubAction:stop()
    -- self:stopSound()
    switchObjectSolidrans(self.object_1, false)
    switchObjectSolidrans(self.object_2, false)
    self.character:setIgnoreMovement(false)
    ISBaseTimedAction.stop(self)
end

function TABAS_WalkInTheTubAction:perform()
    -- self:stopSound()
    switchObjectSolidrans(self.object_1, false)
    switchObjectSolidrans(self.object_2, false)
    self.character:setIgnoreMovement(false)
    TABAS_Utils.setPlayerPositionC(self.character, self.targetSqr)
    ISBaseTimedAction.perform(self)
end

function TABAS_WalkInTheTubAction:new(character, tfc_Base, object_1, targetSqr, object_2)
    local o = ISBaseTimedAction.new(self, character)
    o.character = character
    o.targetSqr = targetSqr

    o.object_1 = object_1
    o.object_2 = object_2 or TABAS_Utils.getGridExtensionBath(object_1)
    o.tfc_Base = tfc_Base or TABAS_Utils.getTfcBaseOnBathObject(object_1)

    o.maxTime = 150
    o.useProgressBar = false
    o.ignoreHandsWounds = true
    o.stopOnWalk = true
    o.stopOnRun = true
    o.stopOnaim = true
    return o
end
