local this = class("pagePetDecryptRoulette", G_UIPageBase)

function this.bind()
  return {
    module_petDecryptRoulette = {
      moduleName = "pages/petDecryptRoulette/modulePetDecryptRoulette"
    }
  }
end

function this.methods()
  return {
    onClick_editBtn = function(self)
      L_UI:open("pagePetDecryptEditRoulette")
    end,
    onClick_close = function(self)
      L_UI:close("pagePetDecryptRoulette")
    end
  }
end

function this:open(options)
  L_QuickRouletteStore:listenCallFunc(L_QuickRouletteStore.event.refreshPetPuzzleRouletteInfo, self.onEvent_refreshPetPuzzleRouletteInfo, self)
  self:refreshView()
end

function this:check(options, callback)
  if AzurWorld.UIBattleDataMgr:CheckPetPuzzleInCD() then
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_puzzle_cd_tips"))
    callback(false)
    return
  end
  callback(true)
end

function this:castPetDecryptSkill(guid)
  local entity = C_EntityManager.GetPetByUid(guid)
  if entity then
    if entity.data.entityType == C_EEntityType.Mount then
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_puzzle_tips01"))
      return
    end
    AzurWorld.UIBattleDataMgr:RecordPetPuzzleTime()
    C_EntityManager.PetDecryptRoulette(guid, C_EntityManager.CreatePetData(), function(petEntity, isSuccess)
    end)
  else
    AzurWorld.UIBattleDataMgr:RecordPetPuzzleTime()
    local pet = L_PetStore:getPetItem(guid)
    local createPetData = C_EntityManager.CreatePetData()
    createPetData.uid = guid
    createPetData.configID = pet:petCfgId()
    createPetData.petData = L_CsEntityManager:getHeroPetData(pet)
    createPetData.petType = 3
    createPetData.heroId = pet:getHeroId()
    C_EntityManager.PetDecryptRoulette(guid, createPetData, function(petEntity, isSuccess)
      C_EntityManager.RemoveEntityWithEntityId(petEntity.data.entityId)
    end)
  end
end

function this:refreshView()
  local rouletteInfo = L_QuickRouletteStore:getRouletteInfoByType(L_Const.quickRouletteType.petPuzzle)
  local data = {}
  local onClick_select = function(cell)
    if cell.bind.guid <= 0 then
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_pet_puzzla_roulette01"))
      return
    end
    self:castPetDecryptSkill(cell.bind.guid)
    L_UI:close("pagePetDecryptRoulette")
  end
  local onDragEnter_cell = function(cell)
    cell:setSelectState(true)
  end
  local onDragExit_cell = function(cell)
    cell:setSelectState(false)
  end
  for index, guid in ipairs(rouletteInfo) do
    table.insert(data, {
      guid = guid,
      index = index,
      cbk = onClick_select,
      onDragEnter = onDragEnter_cell,
      onDragExit = onDragExit_cell
    })
  end
  local modulePetDecryptRoulette = self.modules.module_petDecryptRoulette
  modulePetDecryptRoulette:refreshPetData(data)
end

function this:onEvent_refreshPetPuzzleRouletteInfo()
  self:refreshView()
end

function this:close()
  L_QuickRouletteStore:unListenCallFunc(L_QuickRouletteStore.event.refreshPetPuzzleRouletteInfo, self.onEvent_refreshPetPuzzleRouletteInfo, self)
end

return this
