local this = class("modulePhotoMenu", G_UIModuleBase)
local _photoGroupTpl = L_GameTpl:getPhotographGroupTpl()
local _photoParamTpl = L_GameTpl:getPhotographParamTpl()
local STATE_MENU = {MAIN = 1, GROUP = 2}

function this.bind()
  return {
    goMain = true,
    txtSelection = "",
    curTabID = 1,
    group = this.registBindGroup(),
    groupName = "",
    isActive_tab = true,
    mainList = {
      moduleName = "pages/photo/cell/cellPhotoMenu"
    },
    tabList = {
      moduleName = "pages/photo/cell/cellPhotoMenuTab"
    },
    groupId = 0
  }
end

function this.registBindGroup()
  local tab = {
    type = "toggleModule"
  }
  for key, groupId in pairs(L_Const.photoGroup) do
    local cell = {
      assetName = "UI/Pages/Photo/Group/modulePhotoGroup_" .. key,
      moduleName = "pages/photo/group/modulePhotoGroup_" .. key
    }
    tab[tostring(groupId)] = cell
  end
  return tab
end

function this.methods()
  return {
    mainList = {
      onClick_menu = function(self, groupId)
        self:refreshGroup(groupId)
      end
    },
    onClick_handle = function(self)
      if self.state == STATE_MENU.GROUP then
        self:translateState(STATE_MENU.MAIN)
        L_PhotoManager:sendEvent(L_PhotoManager.event.photoMenuStateChange, true)
      elseif self.state == STATE_MENU.MAIN then
        local action = L_PhotoManager:getMediator()
        action.isMenu = false
      end
    end,
    onSelect_tab = function(self, tabID)
      if self.bind.curTabID == tabID then
        return
      end
      self.bind.curTabID = tabID
      self:refreshMain(tabID)
    end
  }
end

function this:toBind(...)
  this.super.toBind(self, ...)
  self:initTab()
end

function this:open()
  L_PhotoManager:addListener(L_PhotoManager.event.photoMenuForceRefresh, self.onEvent_photoMenuForceRefresh, self)
end

function this:close()
  L_PhotoManager:removeListener(L_PhotoManager.event.photoMenuForceRefresh, self.onEvent_photoMenuForceRefresh, self)
end

function this:initTab()
  local data1 = {
    tabId = 1,
    txt_name = L_WordsTpl:getValue("ui_modulePhotoMenu_02"),
    tabGroup = self.bindComponents.tabGroup
  }
  local data2 = {
    tabId = 0,
    txt_name = L_WordsTpl:getValue("ui_modulePhotoMenu_03"),
    tabGroup = self.bindComponents.tabGroup
  }
  self.bind.tabList:clear()
  self.bind.tabList:insert(data1)
  self.bind.tabList:insert(data2)
  if L_PhotoManager.tabCache == 0 or L_PhotoManager.tabCache == 1 then
    self.bind.curTabID = L_PhotoManager.tabCache
  else
    self.bind.curTabID = 1
  end
  self:refreshMain(self.bind.curTabID)
end

function this:refreshMain(tabId)
  local cfg = _photoGroupTpl:getConfig()
  local list = {}
  for _, v in ipairs(cfg) do
    local t = _photoGroupTpl:getType(v)
    if _photoGroupTpl:getShow(v) and (t == tabId or t == 1) then
      table.insert(list, {
        id = _photoGroupTpl:getId(v),
        txtName = _photoGroupTpl:getName(v),
        tabGroup = self.bindComponents.mainGroup
      })
    end
  end
  for _, v in pairs(self.modules.mainList) do
    v:clearTabGroup()
  end
  self.bindComponents.mainGroup.Items:Clear()
  self.bind.mainList:clear()
  self.bind.mainList:insert_array(list)
  self:translateState(STATE_MENU.MAIN)
  L_PhotoManager.tabCache = tabId
  if self.groupId ~= nil then
    self.bind.groupId = self.groupId
  end
end

function this:refreshGroup(groupId)
  self.groupId = groupId
  self:translateState(STATE_MENU.GROUP)
  self.bind.groupName = tostring(groupId)
  L_PhotoManager:sendEvent(L_PhotoManager.event.photoMenuStateChange, false, self.groupId)
end

function this:translateState(state)
  self.state = state
  self.bind.goMain = state == STATE_MENU.MAIN
  self.bind.isActive_tab = state == STATE_MENU.MAIN
  if self.state == STATE_MENU.MAIN then
    self.bind.txtSelection = L_WordsTpl:getValue("ui_modulePhotoMenu_01")
    self.bind.groupName = string.empty
  elseif self.groupId == L_Const.photoGroup.wind then
    local tpl = _photoParamTpl:getTplById(L_Const.photoParam.windDirection)
    local name = _photoParamTpl:getName(tpl)
    self.bind.txtSelection = name
  elseif self.groupId == L_Const.photoGroup.pet then
    self:changeTxtSelecton_pet()
  elseif self.groupId == L_Const.photoGroup.template then
    self.bind.txtSelection = ""
  else
    local tpl = _photoGroupTpl:getTplById(self.groupId)
    if tpl then
      local name = _photoGroupTpl:getName(tpl)
      self.bind.txtSelection = name
    end
  end
end

function this:changeTxtSelecton_pet()
  if self.bind and self.bind.txtSelection then
    local Mediator = L_PhotoManager:getMediator()
    if Mediator and Mediator.isPetAction then
      self.bind.txtSelection = ""
    else
      local tpl = _photoGroupTpl:getTplById(L_Const.photoGroup.pet)
      if tpl then
        local name = _photoGroupTpl:getName(tpl)
        local str = string.format("%s(%s/%s)", name, L_PhotoManager:getPhotoPetEntityCount(), L_PhotoStore.canShowPetMaxVal)
        self.bind.txtSelection = str
      end
    end
  end
end

function this:onEvent_photoMenuForceRefresh()
  self:translateState(self.state)
end

return this
