local BaseSubUI = {}

local BF_UI_HELPER = GConst.TYPEOF_UNITY_CLASS.BF_UI_HELPER
---- 子对象的层级应该保持在父对象层级 +300 以内 ，300 -- 400 会放一些活动主界面的顶层元素

---- 私有方法供generalActivitymgr调用
function BaseSubUI:_init(parentUI, root, subUIName)
    self.parentUI = parentUI
    self.root = root
    self.subUIName = subUIName
    self:onLoadPanelComplete()
    if self._loadCompleteCallback then
		self._loadCompleteCallback()
		self._loadCompleteCallback = nil
	end

    self:_setUIOrder(self.parentUI:getUIOrder())
end

function BaseSubUI:_addOnDestroyCallback()
    if self._addOnDestroyFlag then
        return
    end
    self._addOnDestroyFlag = true
    self.root:addOnDestroyCallback(function()
        self:unBindAll()
    end)
end

function BaseSubUI:_setUIOrder(order)
    if not self.root then
        return
    end
    if not self._baseRootCanvas then
        self._baseRootCanvas = self.root:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS)
    end

    self._baseRootCanvas.sortingOrder = order + 1
    local uiHelper = self.root:getGameObject():GetComponent(BF_UI_HELPER)
    if not uiHelper then
        return
    end

    uiHelper:SetSortingOrder(self._baseRootCanvas.sortingOrder)
end

function BaseSubUI:_reshow()
    if not self.root then
        return
    end
    self.root:setActive(true)
    self:onReshow()
end

function BaseSubUI:_hide()
    if not self.root then
        return
    end
    self.root:setActive(false)
    self:onHide()
end

function BaseSubUI:_close()
    self:unBindAll()
    self:onClose()
end

function BaseSubUI:getRoot()
    return self.root
end

function BaseSubUI:setLoadCompleteCallback(func)
    if self:getRoot() then
        if func then
            func()
        end
    else
        self._loadCompleteCallback = func
    end
end

---- 公用方法为子类的生命周期
function BaseSubUI:getPrefabPath()
    return --"xxxx"
end

function BaseSubUI:ctor(params)

end

---- after loadPanel
function BaseSubUI:onLoadPanelComplete()

end

---- setActive(true)
function BaseSubUI:onReshow()

end

---- setActive(false)
function BaseSubUI:onHide()

end

---- onclose
function BaseSubUI:onClose()

end

---- 绑定数据
function BaseSubUI:bind(data, fieldName, bindFunc, immediately)
    if not self.parentUI then
        return
    end
    self.parentUI:bind(data, fieldName, bindFunc, immediately)
end

---- 解绑数据 可以不用手动释放，主界面关闭时会全部unbind
function BaseSubUI:unBind(data, fieldName)
    if not self.parentUI then
        return
    end
    self.parentUI:unBind(data, fieldName)
end


function BaseSubUI:bind(data, fieldName, bindFunc, immediately)
    self:_addOnDestroyCallback()
	if not self._baseBindData then
		self._baseBindData = {}
	end
	if not self._baseBindData[data] then
		self._baseBindData[data] = {}
	end
	data:bind(fieldName, self, bindFunc, immediately)
 	table.insert(self._baseBindData[data], fieldName)
end

function BaseSubUI:unBind(data, fieldName)
	if self._baseBindData and self._baseBindData[data] then
		for i, field in ipairs(self._baseBindData[data]) do
			if field == fieldName then
				data:unBind(field, self)
				table.remove(self._baseBindData[data], i)
				break
			end
		end
	end
end

function BaseSubUI:unBindAll()
	if not self._baseBindData then
		return
	end

	for data, fields in pairs(self._baseBindData) do
		for _, field in ipairs(fields) do
			data:unBind(field, self)
		end
	end
	self._baseBindData = nil
end

---- 添加特效
function BaseSubUI:addEffect(effect, parent, order)
    if not self.parentUI then
        return
    end
    self.parentUI:addEffect(effect, parent, order)
end

---- 移除特效
function BaseSubUI:removeEffect(effect, isNeedCleanUp)
    if not self.parentUI then
        return
    end
    self.parentUI:removeEffect(effect, isNeedCleanUp)
end

-- 界面的自定义事件添加监听
function BaseSubUI:addEventListener(key, func, priority)
    if not self.parentUI then
        return
    end
    self.parentUI:addEventListener(key, func, priority)
end

-- 界面的自定义事件移除监听
function BaseSubUI:removeEventListener(key)
    if not self.parentUI then
        return
    end
    self.parentUI:removeEventListener(key)
end

return BaseSubUI