local GridEdgeEntity = class("GridEdgeEntity", BaseData)

local BattleConst = GConst.BattleConst
local HALF_GRID_STEP_H = 47
local DIRECTION_OFFSET = {
    [BattleConst.BOARD_RANGE_TYPE.UP] = {x = 0, y = HALF_GRID_STEP_H},
    [BattleConst.BOARD_RANGE_TYPE.DOWN] = {x = 0, y = - HALF_GRID_STEP_H},
    [BattleConst.BOARD_RANGE_TYPE.RIGHT] = {x = HALF_GRID_STEP_H, y = 0},
    [BattleConst.BOARD_RANGE_TYPE.LEFT] = {x = - HALF_GRID_STEP_H, y = 0},
}

local DIRECTION_EULER_ANGLES = {
    [BattleConst.BOARD_RANGE_TYPE.UP] = {x = 0, y = 0, z = 0},
    [BattleConst.BOARD_RANGE_TYPE.DOWN] = {x = 0, y = 0, z = 180},
    [BattleConst.BOARD_RANGE_TYPE.RIGHT] = {x = 0, y = 0, z = -90},
    [BattleConst.BOARD_RANGE_TYPE.LEFT] = {x = 0, y = 0, z = 90},
}

function GridEdgeEntity:ctor(data)
    self.edgeType = data.edgeType
    self.posId = data.posId
    self.direction = data.direction
    self.battleData = data.battleData
    self.isIdle = false
    self.data.isDirty = not self.data.isDirty

    self:getConfig(true)
end

function GridEdgeEntity:getSnapshoptInfo()
    return {
        posId = self.posId,
        edgeType = self.edgeType,
        isIdle = self.isIdle,
        direction = self.direction,
    }
end

function GridEdgeEntity:getConfig(force)
    if not self.config or force then
        self.config = ConfigManager:getConfig("grid_edge_type")[self.edgeType]
    end
    return self.config
end

function GridEdgeEntity:setDirty()
    self.data.isDirty = not self.data.isDirty
end

function GridEdgeEntity:getPos()
    local pos = ModuleManager.BattleManager:getPosInfo(self.posId, self.battleData:getRowCount())
    if not self.posInfo then
        self.posInfo = {}
    end
    self.posInfo.x = pos.x
    self.posInfo.y = pos.y
    if DIRECTION_OFFSET[self.direction] then
        self.posInfo.x = self.posInfo.x + DIRECTION_OFFSET[self.direction].x
        self.posInfo.y = self.posInfo.y + DIRECTION_OFFSET[self.direction].y
    end
    return self.posInfo
end

function GridEdgeEntity:getEulerAngles()
    return DIRECTION_EULER_ANGLES[self.direction] or DIRECTION_EULER_ANGLES[BattleConst.BOARD_RANGE_TYPE.UP]
end

function GridEdgeEntity:setCell(cell)
    self.cell = cell
end

function GridEdgeEntity:getCell()
    return self.cell
end

function GridEdgeEntity:getIcon()
    return self:getConfig().icon
end

function GridEdgeEntity:getNextType()
    return self:getConfig().next_type
end

function GridEdgeEntity:getType()
    return self.edgeType
end

function GridEdgeEntity:setGridType(edgeType)
    self.edgeType = edgeType
    self:getConfig(true)
end

function GridEdgeEntity:getBreakCondition()
    return self:getConfig().break_condition
end

function GridEdgeEntity:getBreakSfx()
    local sfxName = self:getConfig().break_sfx
    if not sfxName then
        return
    end

    if not self.cacheBreakSfxPaths then
        self.cacheBreakSfxPaths = {}
    end
    if not self.cacheBreakSfxPaths[sfxName] then
        self.cacheBreakSfxPaths[sfxName] = "assets/prefabs/effects/battle/" .. sfxName .. ".prefab"
    end
    return self.cacheBreakSfxPaths[sfxName]
end

function GridEdgeEntity:getIsIdle()
    return self.isIdle
end

function GridEdgeEntity:setIsIdle(isIdle)
    self.isIdle = isIdle
end

function GridEdgeEntity:tryBreak()
    if self:getNextType() then
        self:setGridType(self:getNextType())
    else
        self:setIsIdle(true)
    end
    self:setDirty()
end

return GridEdgeEntity