local this = class("moduleCollectSkillBtn", G_UIModuleBase)

function this.bind()
  return {
    collectionToolActive = false,
    collectionToolAdd = true,
    collectionToolSwitch = false,
    collectionContent = false,
    collectionEndurance = 0,
    collectionEnduranceTxt = 0,
    collectionToolIcon = "",
    collectionToolIconActive = false
  }
end

function this.methods()
  return {
    onClick_addCollectionTool = function(self)
      if self.isCollecting then
        return
      end
      L_UI:open("pageCollectionTool", {
        toolType = self.toolType or 0
      })
    end,
    onClick_changeCollectionTool = function(self)
      if self.isCollecting then
        return
      end
      L_UI:open("pageCollectionTool", {
        toolType = self.toolType or 0
      })
    end,
    onClick_switchCollectionTool = function(self)
      if self.isCollecting then
        return
      end
      L_UI:open("pageCollectionTool", {
        toolType = self.toolType or 0
      })
    end
  }
end

function this:open(...)
  this.super.open(self, ...)
  self.isCollecting = false
  self.collectionToolInteract = {}
  L_CollectionToolStore:listenCallFunc(L_CollectionToolStore.event.triggerCollect, self.onEvent_triggerCollect, self)
  L_CollectionToolStore:listenCallFunc(L_CollectionToolStore.event.dataUpdate, self.refreshCollectionTool, self)
  L_CollectionToolStore:listenCallFunc(L_CollectionToolStore.event.collectStatusChange, self.refreshCollectionStatus, self)
  L_CollectionToolStore:listenCallFunc(L_CollectionToolStore.event.collectTriggerBtnChange, self.collectTriggerBtnChange, self)
end

function this:close(...)
  this.super.close(self, ...)
  self.collectionToolInteract = {}
  L_CollectionToolStore:unListenCallFunc(L_CollectionToolStore.event.collectTriggerBtnChange, self.collectTriggerBtnChange)
  L_CollectionToolStore:unListenCallFunc(L_CollectionToolStore.event.collectStatusChange, self.refreshCollectionStatus)
  L_CollectionToolStore:unListenCallFunc(L_CollectionToolStore.event.triggerCollect, self.onEvent_triggerCollect)
  L_CollectionToolStore:unListenCallFunc(L_CollectionToolStore.event.dataUpdate, self.refreshCollectionTool)
end

function this:onEvent_triggerCollect(isIn, toolType)
  self.btnTypeStatus = self.btnTypeStatus or {}
  local canShowCollect = isIn or self.isCollecting
  self.inRange = isIn
  self.bind.collectionToolActive = canShowCollect
  if not self.isCollecting then
    self.toolType = toolType
  end
  if math.isEmpty(self.toolType) then
    return
  end
  if canShowCollect then
    self.toolGuid = nil
    local curTypeTools = L_CollectionToolStore:getToolsMapByType(toolType)
    if table.isEmpty(curTypeTools) then
      self.bind.collectionToolAdd = true
      self.bind.collectionToolIconActive = false
      self.bind.collectionToolSwitch = false
    else
      do
        local toolGuid = L_CollectionToolStore:getSelectTool(toolType)
        if math.isEmpty(toolGuid) then
          if not self.isChanging then
            self.isChanging = true
            L_CollectionToolStore:req_CSProtoSelectTool(curTypeTools[1].guid, function()
              self.toolGuid = curTypeTools[1].guid
              self:refreshCollectionTool()
              self.isChanging = false
            end)
          end
        else
          self.toolGuid = toolGuid
        end
        self:refreshCollectionTool()
      end
    end
  else
    self.toolGuid = nil
  end
end

function this:refreshCollectionTool()
  if math.isEmpty(self.toolType) then
    return
  end
  local tool
  local toolGuid = L_CollectionToolStore:getSelectTool(self.toolType)
  if not math.isEmpty(toolGuid) then
    self.toolGuid = toolGuid
    tool = L_CollectionToolStore:getTool(toolGuid)
  else
    self.toolGuid = nil
    self.bind.collectionToolAdd = true
    self.bind.collectionToolSwitch = false
    self.bind.collectionToolIconActive = false
    return
  end
  self.bind.collectionToolAdd = false
  self.bind.collectionToolSwitch = false
  self.bind.collectionToolIconActive = true
  local configTable = CS.Azur.Gameplay.Table.TDCollectionToolTable.GetData(tool.id)
  local conf = L_ItemTplManager:getItemConfig(L_Const.resType.collectionTool, tool.id)
  self.bind.collectionEndurance = tool.endurance / configTable.endurance
  self.bind.collectionEnduranceTxt = tool.endurance
  self.bind.collectionToolIcon = conf.icon
end

function this:collectTriggerBtnChange(isIn, toolType)
  self.btnTypeStatus = self.btnTypeStatus or {}
  self.btnTypeStatus[toolType] = self.btnTypeStatus[toolType] or {}
  if isIn then
    table.insert(self.btnTypeStatus[toolType], 1)
  elseif not table.isEmpty(self.btnTypeStatus[toolType]) then
    table.remove(self.btnTypeStatus[toolType], 1)
    if self.toolType == toolType and table.isEmpty(self.btnTypeStatus[toolType]) then
      self:onEvent_triggerCollect(isIn, toolType)
    end
  end
end

function this:refreshCollectionStatus(isBegin)
  self.isCollecting = isBegin
  if not self.inRange then
    self.bind.collectionToolActive = false
  end
  self.bind.collectionContent = isBegin
end

return this
