local this = class("mapBuilder")
local _areaTpl = L_GameTpl:getWorldAreaTpl()

function this:ctor(mapData, mapContainer)
  self.mapContainer = mapContainer
  self:setData(mapData)
end

function this:getData()
  return self.data
end

function this:setData(mapData)
  self.data = mapData
  self.painter = nil
  self.data.forceNotCut = mapData.forceNotCut and mapData.forceNotCut or false
  self.data.isStream = mapData.isStream and mapData.isStream or false
  local tpl = _areaTpl:getTplById(mapData.areaId)
  local isCut = _areaTpl:getIsCut(tpl)
  if isCut and self.data.isStream then
    if mapData.isBigMap then
      self.painter = require("ui.manager.map.painter.mapStreamPainter_bigMap").new(self)
    else
      self.painter = require("ui.manager.map.painter.mapStreamPainter").new(self)
    end
    return
  end
  if isCut and not self.data.forceNotCut then
    self.painter = require("ui.manager.map.painter.mapSuperPainter").new(self)
  else
    self.painter = require("ui.manager.map.painter.mapSimplePainter").new(self)
  end
end

function this:paint(container)
  self.mapContainer = container
  self.painter:paint(container)
end

function this:setTargetTrans(targetTrans)
  self.painter:setTargetTrans(targetTrans)
end

function this:setMapOffset(deltaPos)
  local imgPos = L_Vector3.getAnchored(self.mapContainer)
  imgPos.x = imgPos.x + deltaPos.x
  imgPos.y = imgPos.y + deltaPos.y
  self:setMapPosition(imgPos)
end

function this:setMapPosition(imgPos)
  imgPos = self.mapData:clampImgPos(imgPos)
  L_Vector3.setAnchored(self.mapContainer, imgPos)
end

function this:setMapSlider(value, point)
  self.mapData:setSlider(value)
  local size = L_Vector3.getSize(self.mapContainer)
  local pos = L_GameUtil.spToLpInRectangle(self.mapContainer, point or self.mapData.showSize)
  local offset = pos * (size - self.mapData.imgScale)
  L_Vector3.setSize(self.mapContainer, self.mapData.imgScale)
  self:setMapOffset(offset)
end

function this:removePaint()
  self.mapContainer = nil
  self.painter:removePaint()
end

function this:onDestroy()
  self.mapContainer = nil
  self.painter:onDestroy()
end

return this
