local module = class("commonTopBar", G_UIModuleBase)
local _systemUnlockTpl = L_GameTpl:getSystemUnlockTpl()

function module:ctor(...)
  module.super.ctor(self, ...)
  self.data = {
    returnBtnShow = false,
    helpBtnShow = false,
    mainBtnShow = false,
    pageName = nil,
    resource = nil
  }
end

function module.bind()
  return {
    txtName = "",
    txtNameEn = "",
    returnBtn = false,
    helpBtn = false,
    mainBtn = false,
    boardActive = false,
    resList = {
      moduleName = "modulePages/cellCurrency"
    }
  }
end

function module.methods()
  return {
    onClick_closeCurPageBtn = function(self)
      if self.data.closeFunc then
        self.data.closeFunc()
      elseif self.data.pageName then
        L_UI:close(self.data.pageName)
      end
    end,
    onClick_helpBtn = function(self)
      if self.data.helpBtnFunc then
        self.data.helpBtnFunc()
      elseif not math.isEmpty(self.data.introId) then
        L_UI:open("pageCommonIntroduction", {
          groupId = self.data.introId
        })
      end
    end,
    onClick_mainBtn = function(self)
      if self.data.mainBtnFunc then
        self.data.mainBtnFunc()
      else
        L_UI:backToRoot()
      end
    end
  }
end

function module:initModule(data)
  self.bind.txtName = data.backName or ""
  self.bind.txtNameEn = data.nameEn or "BACK"
  self.bind.returnBtn = data.returnBtnShow or false
  self.bind.helpBtn = data.helpBtnShow or false
  self.bind.mainBtn = data.mainBtnShow or false
  self.data.closeFunc = data.closeFunc
  self.data.helpBtnFunc = data.helpBtnFunc
  self.data.mainBtnFunc = data.mainBtnFunc
  self.data.pageName = data.pageName
  self.data.resource = data.resource
  self.data.introId = data.introId
  self:setResources(self.data.resource)
end

function module:setBackName(txtName)
  self.bind.txtName = txtName
end

function module:setReturnBtnVisible(isShow)
  self.bind.returnBtn = isShow
end

function module:setHelpBtnVisible(isShow)
  self.bind.helpBtn = isShow
end

function module:setMainBtnVisible(isShow)
  self.bind.mainBtn = isShow
end

function module:setCloseFunc(closeFunc)
  self.data.closeFunc = closeFunc
end

function module:setHelpBtnFunc(helpBtnFunc)
  self.data.helpBtnFunc = helpBtnFunc
end

function module:setMainBtnFunc(mainBtnFunc)
  self.data.mainBtnFunc = mainBtnFunc
end

function module:setVisible(isShow)
  if self.data.closeFunc or self.data.pageName then
    self.bind.boardActive = isShow
  end
end

function module:refreshTopBarResources(tpl_system)
  if table.isEmpty(tpl_system) then
    self.bind.resList:clear()
    return
  end
  local res = _systemUnlockTpl:getResourceBar(tpl_system)
  self:setResources(res)
end

function module:setResources(res)
  self.bind.resList:clear()
  if table.isEmpty(res) then
    return
  end
  self.data.resource = res
  local tmp = {}
  for _, resId in ipairs(res) do
    table.insert(tmp, {resId = resId})
  end
  self.bind.resList:insert_array(tmp)
end

function module:close()
  self.data = nil
end

function module:playOpen()
  if self.closeTimer ~= nil then
    Timer.remove(self.closeTimer)
    self.closeTimer = nil
  end
  self.bind.boardActive = true
  self.bindComponents.animation:Play("anim_commontop_show")
end

function module:playClose()
  self.data.returnBtnShow = false
  self.data.helpBtnShow = false
  self.data.mainBtnShow = false
  self.data.closeFunc = nil
  self.data.helpBtnFunc = nil
  self.data.mainBtnFunc = nil
  self.data.pageName = nil
  self.data.introId = nil
  self.data.resource = {}
  self.bindComponents.animation:Play("anim_commontop_hide")
  self.closeTimer = Timer.once(L_TopBarManager:getCloseAniamtionTime(), function()
    self.bind.boardActive = false
    self.closeTimer = nil
  end)
end

return module
