local this = class("superMapPainter", require("ui.manager.map.painter.mapPainter"))
local _areaTpl = L_GameTpl:getWorldAreaTpl()
local PATH_CONTAINER = "Program/Map/pre_mapContainer.prefab"
local PATH_PAINTER = "Program/Map/pre_mapPainter.prefab"
local PATH_MINIMAP = "UI/Texture/MiniMap/%d/tex_miniMap_%d_%d_%d.png"

function this:ctor(...)
  this.super.ctor(self, ...)
  self.index = 0
  self.middle = self.mapData.imgSize / 2
  self.pool = L_PoolManager:getSpawnPool()
end

function this:paint(container)
  local go = self.pool:spawn(PATH_CONTAINER)
  self.paintNode = go.transform
  self.paintNodeGo = go
  self.paintNode:Reset(container, true)
  self.paintNode:SetAsFirstSibling()
  local tpl = _areaTpl:getTplById(self.mapData.areaId)
  self.cellSize = _areaTpl:getRowAndColumn(tpl)
  L_Vector3.setUISize(container, self.mapData.imgSize)
  L_Vector3.setUISize(self.paintNode, self.mapData.imgSize)
  self:refreshCell()
end

function this:refreshCell()
  self.index = self.index + 1
  if self.index > self.cellSize.x * self.cellSize.y then
    return
  else
    local trans = self.pool:spawn(PATH_PAINTER).transform
    trans:Reset(self.paintNode, true)
    L_Vector3.setUISize(trans, L_Vector3.getTemp(1024, 1024))
    self:cellSpawn(trans, self.index)
    self:refreshCell()
  end
end

function this:cellSpawn(trans, index)
  local x = math.ceil(index / self.cellSize.x) - 1
  local y = (index - 1) % self.cellSize.x
  local img = trans:GetComponent(typeof(C_LImage))
  local sp = self:getImage(x, y)
  img:LoadSprite(sp, false)
  local x1 = 1024 * y - self.middle.x + 512
  local y1 = 1024 * (self.cellSize.x - x) - self.middle.y - 512
  L_Vector3.setAnchored(trans, L_Vector3.getTemp(x1, y1))
end

function this:getImage(x, y)
  local path = string.format(PATH_MINIMAP, self.mapData.areaId, self.mapData.areaId, x, y)
  return path
end

function this:cellDespawn(index)
end

function this:onDrag()
end

function this:onSlider()
end

function this:removePaint()
  if self.paintNodeGo ~= nil then
    C_GameObject.Destroy(self.paintNodeGo)
    self.pool:dispose()
    self.paintNodeGo = nil
  end
end

function this:onDestroy()
  self.pool:dispose()
end

return this
