using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class SceneContainer : ObjectContainer
{
    public CameraContainer MainCameraOrbit;
    public CameraContainer WorkCameraOrbit;

    public List<ObjectContainer> AllObjects = new List<ObjectContainer>();

    public List<T> Objects<T>() where T : ObjectContainer
    {
        return AllObjects.Cast<T>().ToList();
    }

    public static SceneContainer Create<T>(ModelViewerMain main) where T : SceneContainer
    {
        var container = new GameObject("Scene").AddComponent<T>();
        container.Init(main);
        return container;
    }

    public virtual void Init(ModelViewerMain main)
    {
        GUID = "scene";
        MainCameraOrbit = main.MainCameraOrbit;
        WorkCameraOrbit = main.WorkCameraOrbit;
        SetDefaultFrame();
        AllObjects.Add(MainCameraOrbit);
    }

    public override ObjectContainerSerializable Serialize()
    {
        return new SceneSerializable(this);
    }

    protected override void OnDestroy()
    {
        if (_applicationQuitting) return;

        for (int i = AllObjects.Count - 1; i >= 0; i--)
        {
            var obj = AllObjects[i];
            if (obj == this) continue;
            obj.Destroy();
        }
    }
}