local cls = class("uiModelMgr")

function cls:ctor()
end

function cls:LoadHeroScenePrefabAsync(heroEnvironmentPath, cbk)
  if self.loadHeroScenePrefab == heroEnvironmentPath then
    if L_CommonUtil.isValid(self.heroEnvironment) then
      L_CommonUtil.setObjActive(self.heroEnvironment, true)
      if cbk then
        cbk(self.heroEnvironment)
      end
    end
    return
  end
  self.loadHeroScenePrefab = heroEnvironmentPath
  local heroEnvironmentLoadHandler
  heroEnvironmentLoadHandler = C_LuaUtility.InstantiateAsync(heroEnvironmentPath, function(go)
    if not L_CommonUtil.isValid(go) then
      errorf("cant load asset from " .. heroEnvironmentPath)
      return
    end
    if not self.loadHeroScenePrefab or self.loadHeroScenePrefab ~= heroEnvironmentPath then
      C_LuaUtility.DestroyObject(heroEnvironmentLoadHandler)
      return
    end
    self:RecycleHeroEnvironment()
    self.heroEnvironmentLoadHandler = heroEnvironmentLoadHandler
    self.heroEnvironment = go
    self.loadHeroScenePrefab = heroEnvironmentPath
    L_Vector3.setPos(go.transform, C_Vector3(-4000, 0, -4000))
    L_Vector3.setRot(go.transform, L_Vector3.zero)
    L_Vector3.setSize(go.transform, 1)
    if cbk then
      cbk(go)
    end
  end, C_LuaUtility.GetRootNode())
end

function cls:RecycleHeroEnvironment()
  self.loadHeroScenePrefab = nil
  if L_CommonUtil.isValid(self.heroEnvironment) then
    C_GameObject.Destroy(self.heroEnvironment)
  end
  self.heroEnvironment = nil
  if self.heroEnvironmentLoadHandler then
    C_LuaUtility.DestroyObject(self.heroEnvironmentLoadHandler)
    self.heroEnvironmentLoadHandler = nil
  end
end

function cls:LoadHeroModelAsync(heroPath, initPos, initRotate, initScale, cbk, parent)
  self.mainHeroInitPos = initPos
  self.mainHeroInitRotate = initRotate
  self.mainHeroInitScale = initScale
  if self.bLoadMainHero == heroPath then
    if L_CommonUtil.isValid(self.heroObj) then
      L_CommonUtil.setObjActive(self.heroObj, true)
      L_Vector3.setLocalPos(self.heroObj.transform, self.mainHeroInitPos or L_Vector3.zero)
      L_Vector3.setRot(self.heroObj.transform, self.mainHeroInitRotate or L_Vector3.zero)
      L_Vector3.setSize(self.heroObj.transform, self.mainHeroInitScale.x or 1)
      if cbk then
        cbk(self.heroObj)
      end
    end
    return
  end
  self.bLoadMainHero = heroPath
  local handler
  handler = C_LuaUtility.InstantiateAsync(heroPath, function(go)
    if not L_CommonUtil.isValid(go) then
      errorf("cant load asset from " .. heroPath)
      return
    end
    if not self.bLoadMainHero or self.bLoadMainHero ~= heroPath then
      C_LuaUtility.DestroyObject(handler)
      return
    end
    self:RecycleHeroModel()
    self.heroHandler = handler
    self.bLoadMainHero = heroPath
    L_Vector3.setLocalPos(go.transform, self.mainHeroInitPos or L_Vector3.zero)
    L_Vector3.setRot(go.transform, self.mainHeroInitRotate or L_Vector3.zero)
    L_Vector3.setSize(go.transform, self.mainHeroInitScale.x or 1)
    self.heroObj = go
    if cbk then
      cbk(go)
    end
  end, parent)
end

function cls:RecycleHeroModel()
  self.bLoadMainHero = nil
  if L_CommonUtil.isValid(self.heroObj) then
    C_GameObject.Destroy(self.heroObj)
  end
  if self.heroHandler then
    C_LuaUtility.DestroyObject(self.heroHandler)
    self.heroHandler = nil
  end
  self.heroObj = nil
end

function cls:LoadViceHeroModelAsync(heroPath, initPos, initRotate, initScale, cbk, parent)
  self.viceHeroInitPos = initPos
  self.viceHeroInitRotate = initRotate
  self.viceHeroInitScale = initScale
  if self.bLoadViceHero == heroPath then
    if L_CommonUtil.isValid(self.heroViceObj) then
      L_CommonUtil.setObjActive(self.heroViceObj, true)
      L_Vector3.setLocalPos(self.heroViceObj.transform, self.viceHeroInitPos or L_Vector3.zero)
      L_Vector3.setRot(self.heroViceObj.transform, self.viceHeroInitRotate or L_Vector3.zero)
      L_Vector3.setSize(self.heroViceObj.transform, self.viceHeroInitScale.x or 1)
      if cbk then
        cbk(self.heroViceObj)
      end
    end
    return
  end
  self.bLoadViceHero = heroPath
  local handler
  handler = C_LuaUtility.InstantiateAsync(heroPath, function(go)
    if not L_CommonUtil.isValid(go) then
      errorf("cant load asset from " .. heroPath)
      return
    end
    if not self.bLoadViceHero or self.bLoadViceHero ~= heroPath then
      C_LuaUtility.DestroyObject(handler)
      return
    end
    self:RecycleViceHeroModel()
    self.heroViceHandler = handler
    self.bLoadViceHero = heroPath
    L_Vector3.setLocalPos(go.transform, self.viceHeroInitPos or L_Vector3.zero)
    L_Vector3.setRot(go.transform, self.viceHeroInitRotate or L_Vector3.zero)
    L_Vector3.setSize(go.transform, self.viceHeroInitScale.x or 1)
    self.heroViceObj = go
    if cbk then
      cbk(go)
    end
  end, parent)
end

function cls:RecycleViceHeroModel()
  self.bLoadViceHero = nil
  if L_CommonUtil.isValid(self.heroViceObj) then
    C_GameObject.Destroy(self.heroViceObj)
  end
  self.heroViceObj = nil
  if self.heroViceHandler then
    C_LuaUtility.DestroyObject(self.heroViceHandler)
    self.heroViceHandler = nil
  end
end

function cls:GetHeroObj()
  if L_CommonUtil.isValid(self.heroObj) then
    return self.heroObj
  end
  return nil
end

function cls:EnableHeroModel(bShow)
  L_CommonUtil.setObjActive(self.heroObj, bShow)
end

function cls:EnableViceHeroModel(bShow)
  L_CommonUtil.setObjActive(self.heroViceObj, bShow)
end

return cls
