local this = class("pagePetReleaseConfirm", G_UIPageBase)
local propertyPath = "UI/Pages/Formation/moduleFormationPetProperty.prefab"
local propetyLuaPath = "UI/pages/formation/formationModule/moduleFormationPetProperty"

function this.bind()
  return {
    tipboard = L_Const.ModuleInfo.CommonTipBoard,
    toggle_petListAlignLeft = false,
    petList = {
      moduleName = "pages/pet/cellPixelPet"
    },
    petScrollList = {
      moduleName = "pages/pet/cellPixelPet"
    },
    rewardList = L_Const.ModuleInfo.CellIconBag,
    rewardsListActive = true,
    emptyTipActive = false
  }
end

function this.methods()
  return {
    onClick_btnConfirm = function(self)
      self:dealConfirmRelease()
    end,
    onClick_cancel = function(self)
      L_UI:close(self.pageName)
    end,
    onClick_close = function(self)
      L_UI:close(self.pageName)
    end
  }
end

function this:dealConfirmRelease()
  if #self.rewards == 0 then
    local data = {
      txtTitle = L_WordsTpl:getValue("ui_pet_release_confirm_title"),
      txtContent = L_WordsTpl:getValue("ui_pet_release_confirm_content"),
      confirmCallback = function()
        self:reqPetRelease()
      end
    }
    L_GameUtil.showCommonTip(data)
    return
  end
  self:reqPetRelease()
end

function this:reqPetRelease()
  local data = {
    pet_list = self.options.pets
  }
  self.closeReleaseMode = true
  L_PetReleaseManager:setIsReleaseMode(false)
  L_PetStore:req_removePet(data, function()
    L_UI:close(self.pageName)
  end)
end

function this:preOpen(options)
  L_PetReleaseManager:setIsReleaseMode(false, true)
  self.options = options
  table.sort(self.options.pets, function(a, b)
    local petA = L_PetStore:getPetItem(a)
    local petB = L_PetStore:getPetItem(b)
    return petA.box_id < petB.box_id
  end)
  self:initPage()
end

function this:open()
  self.closeReleaseMode = false
end

function this:close()
  if not self.closeReleaseMode then
    L_PetReleaseManager:setIsReleaseMode(true, true)
  end
end

function this:initPage()
  self.currentChooseGuid = self.options.pets[1]
  self:initPetList()
  self:initRewardList()
end

function this:initRewardList()
  self.rewards = L_PetReleaseManager:calculateReleaseRewards(self.options.pets)
  self.bind.rewardList:clear()
  self.bind.rewardList:insert_array(self.rewards)
  self.bind.rewardsListActive = #self.rewards > 0
  self.bind.emptyTipActive = #self.rewards == 0
end

function this:initPetList()
  if not self.isBind then
    return
  end
  local onClick_Choose = function(cell)
    self:onClickCell(cell)
  end
  local lst = {}
  for i = 1, #self.options.pets do
    local cell = {}
    cell.guid = self.options.pets[i]
    cell.itemType = L_Const.resType.pet
    cell.callback = onClick_Choose
    cell.filterType = L_PetConst.filterPetBox.grade
    cell.choice = false
    table.insert(lst, cell)
  end
  self.bind.petList:clear()
  self.bind.petScrollList:clear()
  if #self.options.pets > 8 then
    self.bind.petScrollList:insert_array(lst)
    self.bind.toggle_petListAlignLeft = true
  else
    self.bind.petList:insert_array(lst)
    self.bind.toggle_petListAlignLeft = false
  end
end

function this:onClickCell(cell)
  local guid = cell.bind.guid
  L_UI:open("pageTipBubble", {
    data = {
      propertyPath,
      propetyLuaPath,
      {
        guid = guid,
        arrowActive = false,
        isShow = true
      },
      cell.gameObject.transform,
      2,
      false,
      0,
      C_Vector2(10, 10),
      C_Vector2(60, 60),
      nil,
      true,
      nil,
      {2, 0}
    }
  })
end

return this
