local cls = class("pageEntrustChallenge", G_UIPageBase)
local _dungeonEntrustTypeTpl = L_GameTpl:getDungeonEntrustTypeTpl()
local ModuleStateEnum = {
  [L_Const.EntrustType.None] = 0,
  [L_Const.EntrustType.AreaEntrust] = 1,
  [L_Const.EntrustType.SpecialEntrust] = 2,
  [L_Const.EntrustType.TrainTask] = 4,
  [L_Const.EntrustType.EntrustAssemble] = 8,
  [L_Const.EntrustType.PetDuel] = 16
}

function cls.bind()
  return {
    module_tabList = {
      moduleName = "pages/entrustTask/moduleEntrustTabList"
    },
    moduleCommonTop = {
      moduleName = "modulePages/moduleCommonTop"
    },
    module_currency = {
      moduleName = "modulePages/moduleCurrency"
    },
    go_tipsNode = false,
    go_imgNode = false
  }
end

function cls.methods()
  return {}
end

function cls:preOpen(options)
  cls.super.preOpen(self, options)
  self:registerModule()
end

function cls:registerModule()
  if not self.subModuleMgr then
    self.subModuleMgr = require("ui.manager.ui.uiSubModuleManager").new(self)
  end
  self.subModuleMgr:init()
  self.subModuleMgr:initSubModules(self, ModuleStateEnum[L_Const.EntrustType.None])
  self.subModuleMgr:addSubModules(ModuleStateEnum[L_Const.EntrustType.AreaEntrust], L_SubModuleConfig.ModuleCfg.moduleEntrustTask.uiName, self.bindComponents.transParent)
  self.subModuleMgr:addSubModules(ModuleStateEnum[L_Const.EntrustType.TrainTask], L_SubModuleConfig.ModuleCfg.moduleEntrustTrainTask.uiName, self.bindComponents.transParent)
  self.subModuleMgr:addSubModules(ModuleStateEnum[L_Const.EntrustType.PetDuel], L_SubModuleConfig.ModuleCfg.moduleEntrustBattleTask.uiName, self.bindComponents.transParent)
end

function cls:open(options)
  self.selectType = options and options.entrustType or L_Const.EntrustType.AreaEntrust
  self:initPage()
end

function cls:initPage()
  self:initCommonTopModule()
  self:initTabList()
end

function cls:check(options, callback)
  local result = L_SystemUnlockManager:checkAndTip(L_SystemConst.enum.entrustTask)
  if not result then
    callback(result)
    return
  end
  local condId = AzurWorld.WorldCondMgr:CreateCond({70001}, nil)
  if not AzurWorld.WorldCondMgr:CheckCond(condId) then
    local cond = AzurWorld.WorldCondMgr:GetCond(condId)
    if cond then
      local str = cond:ShowMsg()
      L_FlyMsgManager:showNormalMsg(str)
    end
    callback(false)
    return
  end
  callback(true)
end

function cls:initCommonTopModule()
  local data = {
    name = "\229\156\176\229\140\186\229\167\148\230\137\152",
    helpBtnBool = false,
    mainBtnBool = false,
    closeCurPage = function()
      L_UI:close(self.pageName)
    end,
    helpBtn = function()
    end,
    mainBtn = function()
      L_UI:backToRoot()
    end
  }
  self.modules.moduleCommonTop:initModule(data)
end

function cls:initTabList()
  local tabList = {}
  local selectIndex = 1
  for index, tpl in ipairs(_dungeonEntrustTypeTpl:getShowData()) do
    table.insert(tabList, _dungeonEntrustTypeTpl:getId(tpl))
    if _dungeonEntrustTypeTpl:getId(tpl) == self.selectType then
      selectIndex = index
    end
  end
  self.modules.module_tabList:setData(tabList, function(tabId)
    local tpl = _dungeonEntrustTypeTpl:getTplById(tabId)
    if not tpl then
      return
    end
    local result = L_SystemUnlockManager:checkAndTip(_dungeonEntrustTypeTpl:getSystemUnLockId(tpl))
    if not result then
      self.modules.module_tabList:setSelectTabId(self.selectType)
      return
    end
    self.selectType = tabId
    self:switchState()
  end, selectIndex)
  self:switchState()
end

function cls:switchState()
  self.subModuleMgr:openSubModulesByIndex(ModuleStateEnum[self.selectType])
  self:refreshCurrencyList()
  self.bind.go_tipsNode = self.selectType == L_Const.EntrustType.TrainTask
  self.bind.go_imgNode = self.selectType ~= L_Const.EntrustType.PetDuel
end

function cls:refreshCurrencyList()
  local tpl = _dungeonEntrustTypeTpl:getTplById(self.selectType)
  local tpl_sys = L_GameTpl:getSystemUnlockTpl():getTplById(_dungeonEntrustTypeTpl:getSystemUnLockId(tpl))
  self.modules.module_currency:refreshRes(tpl_sys)
end

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

function cls:destroy(options)
  self.subModuleMgr:destroyAllSubModules()
end

return cls
