local moduleBase = class("moduleBase", G_UIBase)
moduleBase.ModuleType = {
  Unknown = 0,
  Static = 1,
  Dynamic = 2,
  TableItem = 3
}

function moduleBase:ctor(moduleType)
  moduleBase.super.ctor(self)
  self._moduleType = moduleType or self.ModuleType.Unknown
end

function moduleBase:created(bindData, emitMethods, parent)
  if self.bind then
    self.bind = self.bind()
  end
  if self.methods then
    self.methods = self.methods()
  end
  self.emitMethods = emitMethods
  self.bind = self:mergeData(bindData, self.bind)
  self.parent = parent
  self:preBind(bindData)
  moduleBase.super.created(self)
end

function moduleBase:getModuleType()
  return self._moduleType
end

function moduleBase:emit(eventName, ...)
  if not self.isOpen then
    return
  end
  if self.emitMethods ~= nil and self.emitMethods[eventName] then
    self.emitMethods[eventName](self.parent, ...)
  end
end

function moduleBase:preBind()
end

function moduleBase:toBind(uiBinding)
  if self.isBind then
    return
  end
  moduleBase.super.toBind(self, uiBinding)
  self.gameObject = self.uiBinding.gameObject
  self._internal_blendDefinition = uiBinding.gameObject:TryGetComponent(typeof(C_BlendDefinition))
  if not self.isOpen then
    self.isOpen = true
    self:open()
  end
  if not self.isShow and uiBinding.gameObject.activeSelf then
    self.isShow = true
    self:show()
  end
  self:refresh()
end

function moduleBase:unBind()
  if not self.isBind then
    return
  end
  if self.isShow then
    self.isShow = false
    self:hide()
  end
  if self.isOpen then
    self.isOpen = false
    self:close()
  end
  self._internal_blendDefinition = nil
  moduleBase.super.unBind(self)
end

function moduleBase:open()
  if self.gameObject.activeSelf and (self._moduleType == self.ModuleType.Static or self._moduleType == self.ModuleType.TableItem) then
    self:internalPlayAnimOpen()
  end
end

function moduleBase:close()
  if self._moduleType == self.ModuleType.Static then
    self:internalPlayAnimClose()
  end
end

function moduleBase:toShow()
  if self.isShow then
    return
  end
  self.isShow = true
  self:show()
  self:internalPlayAnimShow(true)
end

function moduleBase:toHide()
  if not self.isShow then
    return
  end
  self.isShow = false
  self:hide()
  self:internalPlayAnimHide()
end

function moduleBase:_getDeactivateHandler()
  if self._deactivateHandler == nil then
    function self._deactivateHandler()
      if self.uiBinding then
        local go = self.uiBinding.gameObject
        
        if go.activeSelf then
          go:SetActive(false)
        end
      end
    end
  end
  return self._deactivateHandler
end

function moduleBase:internalPlayAnimOpen(controlActive)
  if self._internal_blendDefinition then
    if controlActive then
      local go = self._internal_blendDefinition.gameObject
      if not go.activeSelf then
        go:SetActive(true)
      end
    end
    self._internal_blendDefinition:OpenReset()
    self._internal_blendDefinition:PlayOpen(nil)
  end
end

function moduleBase:internalPlayAnimClose(controlActive)
  if self._internal_blendDefinition then
    local cb
    if controlActive then
      cb = self:_getDeactivateHandler()
    end
    self._internal_blendDefinition:CloseReset()
    self._internal_blendDefinition:PlayClose(cb)
  end
end

function moduleBase:internalPlayAnimShow(controlActive)
  if self._internal_blendDefinition then
    if controlActive then
      local go = self._internal_blendDefinition.gameObject
      if not go.activeSelf then
        go:SetActive(true)
      end
    end
    self._internal_blendDefinition:ShowReset()
    self._internal_blendDefinition:PlayShow(nil)
  end
end

function moduleBase:internalPlayAnimHide(controlActive)
  if self._internal_blendDefinition then
    local cb
    if controlActive then
      cb = self:_getDeactivateHandler()
    end
    self._internal_blendDefinition:HideReset()
    self._internal_blendDefinition:PlayHide(cb)
  end
end

return moduleBase
