local this = class("modulePetCatalogMap", G_UIModuleBase)
local _areaTpl = L_GameTpl:getAreaTpl()
local _worldAreaTpl = L_GameTpl:getWorldAreaTpl()
local _petTpl = L_GameTpl:getPetTpl()

function this:ctor(...)
  this.super.ctor(self, ...)
  self.curSelectAreaIdx = 1
  self.isNight = false
end

function this.bind()
  return {
    moduleMap = {
      moduleName = "pages/map/petCatalogMap/modulePetCatalogMap"
    },
    nightSelActive = false,
    morningSelActive = true,
    bgBtnActive = false,
    dropTxt = "",
    listOptions = {
      moduleName = "pages/petCatalogue/cellPetMapItem"
    },
    toggleDrop = false,
    dropNightActive = true,
    dropDayActive = true,
    curOptionIconActive = false,
    txtLock = L_Lang:get(L_WordsTpl:getValue("notice_habitat_02")),
    lockActive = false,
    dropDownActive = false
  }
end

function this.methods()
  return {
    onClickNight = function(self)
      self.bind.nightSelActive = true
      self.bind.morningSelActive = false
      self.isNight = true
      self:showPet(self._petId)
    end,
    onClickMorning = function(self)
      self.bind.nightSelActive = false
      self.bind.morningSelActive = true
      self.isNight = false
      self:showPet(self._petId)
    end,
    onClickDropDown = function(self)
      if not self.isDropping then
        self.bind.toggleDrop = true
        self.bind.bgBtnActive = true
        self.bind.dropNightActive = false
        self.bind.dropDayActive = false
        self.bind.dropDownActive = true
        Unity.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(self.bindComponents.layoutChild)
        Unity.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(self.bindComponents.layout)
        self.isDropping = true
      else
        self.bind.toggleDrop = false
        self.bind.bgBtnActive = false
        self.bind.dropNightActive = true
        self.bind.dropDayActive = true
        self.bind.dropDownActive = false
        self.isDropping = false
      end
    end,
    onClickBg = function(self)
      self.bind.toggleDrop = false
      self.bind.bgBtnActive = false
      self.bind.dropNightActive = true
      self.bind.dropDayActive = true
      self.bind.dropDownActive = false
      self.isDropping = false
    end
  }
end

function this:close(options)
  this.super.close(self, options)
end

function this:showPet(petId)
  self._petId = petId
  self.modules.moduleMap:setPetConfigId(petId, self.isNight)
  local tpl = _petTpl:getTplById(petId)
  if not tpl.iconographyMapPos or #tpl.iconographyMapPos == 0 then
    self.bind.lockActive = true
    self.parent:setBlur(true)
  else
    self.bind.lockActive = false
    self.parent:setBlur(false)
  end
  local mapPosList
  if self.isNight then
    mapPosList = _petTpl:getIconographyMapPos(tpl)[2]
  else
    mapPosList = _petTpl:getIconographyMapPos(tpl)[1]
  end
  local hasList = {}
  if mapPosList then
    for _, pointId in ipairs(mapPosList) do
      hasList[L_MapManager:getAreaIdByKiBoHabitat(pointId)] = true
    end
  end
  for i = 1, #self.mapList do
    if hasList[self.mapList[i].areaId] then
      self.mapList[i].iconActive = true
    else
      self.mapList[i].iconActive = false
    end
    self.mapList[i].goSelect = i == self.curSelectAreaIdx
  end
  self.bind.listOptions:freshAll(self.mapList)
  self:refreshIcon()
end

function this:setArea(areaId)
  local name
  if not self.availableMap[areaId] then
    self.modules.moduleMap:setAreaId(100000)
    name = L_Lang:get(_worldAreaTpl:getName(_worldAreaTpl:getTplById(100000)))
  else
    self.modules.moduleMap:setAreaId(areaId)
    name = L_Lang:get(_worldAreaTpl:getName(_worldAreaTpl:getTplById(areaId)))
  end
  for i, info in pairs(self.mapList) do
    if info.areaId == areaId then
      self.curSelectAreaIdx = i
      break
    end
  end
  self.bind.dropTxt = tostring(name)
  if self._petId then
    self:showPet(self._petId)
  end
end

function this:initMap()
  self.availableMap = {}
  self.mapList = {}
  for areaId, areaData in pairs(_areaTpl.data) do
    if areaData.isShowName then
      self.availableMap[areaData.worldAreaId] = true
      local isLock
      local conds = areaData.unlockCondition
      if conds then
        local complete = L_ConditionManager:isComplete(areaData.unlockCondition)
        isLock = not complete
      else
        isLock = false
      end
      table.insert(self.mapList, {
        idx = areaData.sortID,
        areaId = areaData.worldAreaId,
        name = L_Lang:get(areaData.areaName),
        isLock = isLock,
        goIcon = false,
        onClickCb = function(cell)
          self:onClickItem(cell)
        end
      })
    end
  end
  self.curSelectAreaIdx = 1
  table.sort(self.mapList, function(a, b)
    if a.idx == b.idx then
      return a.areaId < b.areaId
    end
    return a.idx < b.idx
  end)
  for i = 1, #self.mapList do
    self.mapList[i].realIdx = i
    self.mapList[i].goSelect = i == self.curSelectAreaIdx
  end
  self.bind.listOptions:clear()
  self.bind.listOptions:insert_array(self.mapList)
  self.bind.dropTxt = L_Lang:get(self.mapList[self.curSelectAreaIdx].name)
end

function this:onClickItem(cell)
  self.bind.toggleDrop = false
  local oldCell = self.bind.listOptions:getItemCls(self.curSelectAreaIdx)
  local newCell = self.bind.listOptions:getItemCls(cell.bind.realIdx)
  if oldCell then
    oldCell.bind.goSelect = false
  end
  if newCell then
    newCell.bind.goSelect = true
  end
  self.curSelectAreaIdx = cell.bind.realIdx
  self.bind.dropTxt = self.mapList[self.curSelectAreaIdx].name
  self:setArea(cell.bind.areaId)
  self.bind.curOptionIconActive = self.mapList[self.curSelectAreaIdx].iconActive
  self.bind.dropNightActive = true
  self.bind.dropDayActive = true
end

function this:refreshIcon()
  self.bind.curOptionIconActive = self.mapList[self.curSelectAreaIdx].iconActive
end

return this
