local this = class("pageHomeCenterPetStationedManage", G_UIPageBase)
local _homeFreeWorkPositionTpl = L_GameTpl:getHomeFreeWorkPositionTpl()
local FilterType = {
  [L_PetConst.filterPetBox.level] = "ui_pageHomeCenterPetStationedManage_01",
  [L_PetConst.filterPetBox.grade] = "ui_pageHomeCenterPetStationedManage_02",
  [L_PetConst.filterPetBox.satiety] = "ui_pageHomeCenterPetStationedManage_03",
  [L_PetConst.filterPetBox.labor] = "ui_pageHomeCenterPetStationedManage_04"
}

function this.bind()
  return {
    text_petCurNum = "",
    text_petMaxNum = "",
    list_cellPetStationedManage = {
      moduleName = "pages/home/homeCenter/pageHomeCenterPetStationedManage/cellPetStationedManage"
    },
    moveToItemIndex = 0,
    snapToItemIndex = 0,
    listBoxIsDragging = false,
    loopListBox = {
      moduleName = "pages/home/homeCenter/pageHomeCenterPetStationedManage/cellHomeCenterPetStationedManageBox"
    },
    curBoxIndex = "",
    curBoxName = "",
    module_pageTabSlider = {
      moduleName = "modulePages/modulePageTabSlider"
    },
    list_dropdown = {
      moduleName = "modulePages/sortFilter/cellSortFilterDropdown"
    },
    txt_dropDown = "",
    dropDownContent = false,
    toggleArrow = false
  }
end

function this.methods()
  return {
    list_cellPetStationedManage = {
      onClickStationedPet = function(self, petGuid)
        self:onClickStationedPet(petGuid)
      end
    },
    loopListBox = {
      onClick_pet = function(self, petGuid)
        self:onClickPetInPetBox(petGuid)
      end
    },
    list_dropdown = {
      onClick_select = function(self, bind)
        self.UIData.filterType = bind.index
        self.bind.dropDownContent = false
        self.bind.txt_dropDown = bind.type
        self.bind.toggleArrow = false
        self:refreshAllBox()
      end
    },
    onSnapFinished = function(self, index)
      self.UIData.realIndex = index
      local snapIndex = index + 1
      local index = self:realSnapToItemIndex(snapIndex)
      self:setBoxInfo(index)
    end,
    onClick_nextBox = function(self)
      self.bind.snapToItemIndex = self.UIData.realIndex + 1
      L_AudioUtil.playSound("Play_SFX_System_UI_QiboHouse_Switch")
    end,
    onClick_lastBox = function(self)
      self.bind.snapToItemIndex = self.UIData.realIndex - 1
      L_AudioUtil.playSound("Play_SFX_System_UI_QiboHouse_Switch")
    end,
    onClick_showDropDownContent = function(self)
      self:refreshFilterActive()
      self.bind.dropDownContent = not self.bind.dropDownContent
      self.bind.toggleArrow = not self.bind.toggleArrow
    end,
    onClick_btnReturn = function(self)
      L_UI:close(self.pageName)
    end
  }
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  self.onClickStationedPetCallback = options.onClickStationedPetCallback
  self.onClickPetInPetBoxCallback = options.onClickPetInPetBoxCallback
  self.closeCallback = options.closeCallback
  self:refreshData()
  self:refreshUI()
end

function this:open()
  self.onHomeHubSyncHandler = handler(self, self.onHomeHubSync)
  L_HomeStore:getCsHomeStore():RegisterEvent(C_EHomeStoreEvent.HomeHubSync, self.onHomeHubSyncHandler)
  self.onRefreshPetHandler = handler(self, self.onRefreshPet)
  AzurWorld.petMgr:RegisterEvent(C_EPetEvent.refreshPet, self.onRefreshPetHandler)
  self.petStationedConditionNodeList = {}
  for _, petStationedUnlockedConditionList in pairs(_homeFreeWorkPositionTpl:getAllUnlockConditionList()) do
    local conditionNode = L_ConditionManager:registGroup("pageHomeCenterPetStationedManage", petStationedUnlockedConditionList, self.onPetStationedUnlockedConditionStateChanged, self)
    if conditionNode ~= nil then
      table.insert(self.petStationedConditionNodeList, conditionNode)
    end
  end
end

function this:close()
  L_HomeStore:getCsHomeStore():UnregisterEvent(C_EHomeStoreEvent.HomeHubSync, self.onHomeHubSyncHandler)
  for _, conditionNode in pairs(self.petStationedConditionNodeList) do
    L_ConditionManager:unRegist(conditionNode, "pageHomeCenterPetStationedManage")
  end
  if self.closeCallback then
    self.closeCallback()
  end
end

function this:refreshData()
  self.UIData = self.UIData or {}
  self.UIData.stationedPetGuidList = L_HomeStore:getHomeHubStationedPetGuidList()
  self.UIData.maxPetStationedNum = _homeFreeWorkPositionTpl:getMaxFreeWorkNum()
  self.UIData.unlockedPetStationedNum = self:getUnlockedPetStationedNum()
  self.UIData.realIndex = 0
  self.UIData.curIndex = 1
  self.UIData.filterType = self.UIData.filterType or L_PetConst.filterPetBox.labor
end

function this:refreshUI()
  self:refreshPetStationedPart()
  self:refreshPetBoxPart()
  self:setBoxInfo(self:realSnapToItemIndex(self.UIData.curIndex))
  self:initDropDown()
end

function this:refreshPetStationedPart()
  self.bind.text_petCurNum = tostring(#self.UIData.stationedPetGuidList)
  self.bind.text_petMaxNum = "/" .. tostring(self.UIData.unlockedPetStationedNum)
  local insertArrayTable = {}
  for _ = 1, self.UIData.maxPetStationedNum do
    table.insert(insertArrayTable, {})
  end
  self.bind.list_cellPetStationedManage:clear()
  self.bind.list_cellPetStationedManage:insert_array(insertArrayTable)
  for i = 1, #self.UIData.stationedPetGuidList do
    local cellPetStationedManage = self.bind.list_cellPetStationedManage:getItemCls(i)
    local petGuid = self.UIData.stationedPetGuidList[i]
    cellPetStationedManage:refreshData(petGuid, i, true)
    cellPetStationedManage:refreshUI()
  end
  for i = #self.UIData.stationedPetGuidList + 1, self.UIData.unlockedPetStationedNum do
    local cellPetStationedManage = self.bind.list_cellPetStationedManage:getItemCls(i)
    cellPetStationedManage:refreshData(0, i, true)
    cellPetStationedManage:refreshUI()
  end
  for i = self.UIData.unlockedPetStationedNum + 1, self.UIData.maxPetStationedNum do
    local cellPetStationedManage = self.bind.list_cellPetStationedManage:getItemCls(i)
    cellPetStationedManage:refreshData(0, i, false)
    cellPetStationedManage:refreshUI()
  end
end

function this:refreshPetBoxPart()
  local tmp = {}
  local petBoxList = L_PetStore:getPetBoxList()
  local petBoxNameList = L_PetStore:getPetBoxNameList()
  for i, v in ipairs(petBoxList) do
    table.insert(tmp, {
      petList = v,
      boxIndex = i,
      boxName = petBoxNameList[i]
    })
  end
  self.bind.loopListBox:clear()
  self.bind.loopListBox:insert_array(tmp)
end

function this:setBoxInfo(itemIndex)
  self.UIData.curIndex = itemIndex or 1
  local indexChange = function(index)
    if index < 10 then
      return string.format("0%d", index)
    end
    return tostring(index)
  end
  local boxIndexStr = indexChange(self.bind.loopListBox[self.UIData.curIndex].boxIndex)
  local boxName = self.bind.loopListBox[self.UIData.curIndex].boxName
  self.bind.curBoxIndex = boxIndexStr
  self.bind.curBoxName = boxName
  local modulePageTabSlider = self.modules.module_pageTabSlider
  modulePageTabSlider:setPageIndex(self.UIData.curIndex, #self.bind.loopListBox)
end

function this:getFilterType()
  return self.UIData.filterType
end

function this:realSnapToItemIndex(snapIndex)
  local index = snapIndex
  if index == 0 then
    index = #self.bind.loopListBox
  else
    index = index % #self.bind.loopListBox
    index = index == 0 and #self.bind.loopListBox or index
  end
  return index
end

function this:refreshCurBox()
  if self.UIData.curIndex then
    self.bind.loopListBox:getItemCls(self.UIData.curIndex):refresh()
  end
end

function this:refreshAllBox()
  for i = 1, #self.bind.loopListBox do
    self.bind.loopListBox:getItemCls(i):refresh()
  end
end

function this:onClickStationedPet(petGuid)
  if self.onClickStationedPetCallback then
    self.onClickStationedPetCallback(petGuid)
  end
  self.bindComponents.stationedAnim:Stop()
  self.bindComponents.stationedAnim:Play("anim_product_stationedManage_cellchange")
end

function this:onClickPetInPetBox(petGuid)
  if self.onClickPetInPetBoxCallback then
    self.onClickPetInPetBoxCallback(petGuid)
  end
  self.bindComponents.stationedAnim:Stop()
  self.bindComponents.stationedAnim:Play("anim_product_stationedManage_cellchange")
end

function this:initDropDown()
  local tmp = {}
  for index, type in ipairs(FilterType) do
    table.insert(tmp, {
      index = index,
      type = L_WordsTpl:getValue(type),
      txt_name = L_WordsTpl:getValue(type),
      go_icon = false
    })
  end
  self.bind.txt_dropDown = tmp[self.UIData.filterType].txt_name
  self.bind.list_dropdown:clear()
  self.bind.list_dropdown:insert_array(tmp)
end

function this:refreshFilterActive()
  for i = 1, #self.bind.list_dropdown do
    local index = self.bind.list_dropdown:getValue(i, "index")
    if index == self:getFilterType() then
      self.bind.list_dropdown:getItemCls(i):setActive(true)
    else
      self.bind.list_dropdown:getItemCls(i):setActive(false)
    end
  end
end

function this:onHomeHubSync()
  self:refreshData()
  self:refreshPetStationedPart()
end

function this:onRefreshPet()
  self:refreshData()
  self:refreshPetStationedPart()
end

function this:onPetStationedUnlockedConditionStateChanged()
  self:refreshData()
  self:refreshPetStationedPart()
end

function this:getUnlockedPetStationedNum()
  local unlockedPetStationedNum = 0
  local freeWorkPosId2UnlockConditionList = _homeFreeWorkPositionTpl:getFreeWorkPosId2UnlockConditionList()
  for freeWorkPositionId, unlockConditionList in pairs(freeWorkPosId2UnlockConditionList) do
    if L_ConditionManager:isComplete(unlockConditionList) and freeWorkPositionId > unlockedPetStationedNum then
      unlockedPetStationedNum = freeWorkPositionId
    end
  end
  return unlockedPetStationedNum
end

function this:check(options, callback)
end

function this:escHandle()
  L_UI:close("pageHomeCenterPetStationedManage")
end

return this
