local UIHelper = {}

function UIHelper:OpenUI(scriptPath)
    print("UIHelper:OpenUI -> " .. scriptPath)
    local luaRoot = CS.UnityEngine.GameObject.Find("LuaRoot")
    assert(luaRoot ~= nil)
    local uiScriptHolder = luaRoot.transform:Find("uiScriptHolder")
    if not uiScriptHolder then
        uiScriptHolder = CS.UnityEngine.GameObject("uiScriptHolder")
        uiScriptHolder.transform:SetParent(luaRoot.transform)
    end

    local uiController = require(scriptPath)
    local Utils = require("Base/Utils.lua")
    local scriptName = Utils:GetScriptNameByScriptPath(scriptPath)
	local scriptGameObject = CS.UnityEngine.GameObject(scriptName)
	scriptGameObject.transform:SetParent(uiScriptHolder.transform)
    CS.SFramework.SLuaComponent.Add(scriptGameObject, uiController, scriptPath)
    return uiController
end

function UIHelper:CloseUI(scriptPath)
    print("UIHelper:CloseUI -> " .. scriptPath)

    local Utils = require("Base/Utils.lua")
    local scriptName = Utils:GetScriptNameByScriptPath(scriptPath)
    local go = CS.UnityEngine.GameObject.Find("LuaRoot/uiScriptHolder/" .. scriptName)
    CS.UnityEngine.GameObject.Destroy(go)
end

function UIHelper:SwitchUI(dstUIControllerPath, srcUIControllerPath)
    UIHelper:OpenUI(dstUIControllerPath)
    UIHelper:CloseUI(srcUIControllerPath)
end

function UIHelper:AddClickEvent(rootPanel, buttonPath, clickFunc)
    print(string.format("UIHelper:AddClickEvent rootPanel.name:%s buttonPath:%s clickFunc:%s", rootPanel.name, buttonPath, clickFunc))
	local buttonGameObject = rootPanel.transform:Find(buttonPath).gameObject
	buttonGameObject:GetComponent("Button").onClick:AddListener(clickFunc)
end

function UIHelper:AddSliderEvent(rootPanel, sliderPath, valueChangeFunc)
    print(string.format("UIHelper:AddSliderEvent rootPanel.name:%s buttonPath:%s clickFunc:%s", rootPanel.name, sliderPath, valueChangeFunc))
    local sliderGameObject = rootPanel.transform:Find(sliderPath).gameObject
    sliderGameObject:GetComponent("Slider").onValueChanged:AddListener(valueChangeFunc)
end

function UIHelper:AddToggleEvent(rootPanel, togglePath, valueChangeFunc)
    print(string.format("UIHelper:AddToggleEvent rootPanel.name:%s buttonPath:%s toggleChangeFunc:%s", rootPanel.name, togglePath, valueChangeFunc))
    local toggleGameObject = rootPanel.transform:Find(togglePath).gameObject
    toggleGameObject:GetComponent("Toggle").onValueChanged:AddListener(valueChangeFunc)
end

function UIHelper:SetTextInfo(rootPanel, textPath, textInfo)
    print(string.format("UIHelper:SetTextInfo rootPanel.name:%s buttonPath:%s textInfo:%s", rootPanel.name, textPath, textInfo))
    local textGameObject = rootPanel.transform:Find(textPath).gameObject
    textGameObject:GetComponent("Text").text = textInfo
end

function UIHelper:LoadSprite(rootPanel, resourcePath, spriteName)
    print(string.format("UIHelper:LoadSprite rootPanel.name:%s resourcePath:%s spriteName:%s", rootPanel.name, resourcePath, spriteName))
    local texture2D = CS.SFramework.SResourceManager.LoadResource(resourcePath)
    local skillSprite = CS.UnityEngine.Sprite.Create(texture2D, CS.UnityEngine.Rect(0,0,texture2D.width, texture2D.height), CS.UnityEngine.Vector2.zero)
    skillSprite.name = spriteName
    rootPanel.transform:GetComponent("Image").sprite = skillSprite
end

function UIHelper:IsPointerOverGameObject(mousePos)
    -- local eventSystem  = CS.UnityEngine.EventSystems.EventSystem
    -- local eventData    = CS.UnityEngine.EventSystems.PointerEventData(eventSystem.current)
    -- eventData.position = mousePos
    -- local listRaycast  = CS.System.Collections.Generic.List(CS.UnityEngine.EventSystems.RaycastResult)()
    -- eventSystem.current:RaycastAll(eventData, listRaycast)

    local eventSystem       = CS.UnityEngine.EventSystems.EventSystem
    local eventData         = CS.UnityEngine.EventSystems.PointerEventData(eventSystem.current)
    local RaycastInCanvas   = CS.UnityEngine.GameObject.Find("Canvas"):GetComponent("GraphicRaycaster")
    eventData.pressPosition = CS.UnityEngine.Input.mousePosition
    eventData.position      = CS.UnityEngine.Input.mousePosition
    local listRaycast       = CS.System.Collections.Generic.List(CS.UnityEngine.EventSystems.RaycastResult)()
    RaycastInCanvas:Raycast(eventData, listRaycast)

    local bTouchUI = false
    if listRaycast.Count ~= 0 then
        for _, list in pairs(listRaycast) do
            print("---->  list.name", list.gameObject.name)
        end

        -- local rawImageRootPanel = require("UI/ZUIPanelPlayerMove.lua").rootPanel
        -- local rawImage          = rawImageRootPanel.transform:Find("RawImage").gameObject
        -- local firstRaycast      = listRaycast[0].gameObject

        -- bTouchUI = rawImage.name ~= firstRaycast.name

        local firstRaycast      = listRaycast[0].gameObject
        bTouchUI = firstRaycast.name == "RawImage"
    end

    return bTouchUI
end

function UIHelper:SetButtonInteractable(rootPanel, buttonPath, bEnable)
    local button = rootPanel.transform:Find(buttonPath).gameObject
    button:GetComponent("Button").interactable = bEnable
end

return UIHelper