using UnityEngine;

namespace Jovian.Utilities {
    public static class CachedMainCamera {
        private static int lastFrame = -1;
        private static Camera mainCamera;
        private static Transform mainCameraTransform;

        public static Camera MainCamera {
            get {
                if(mainCamera) {
                    return mainCamera;
                }
                AssignCameraReferences();
                return mainCamera;
            }
        }

        public static Transform MainCameraTransform {
            get {
                if(mainCameraTransform) {
                    return mainCameraTransform; 
                }
                AssignCameraReferences();
                return mainCameraTransform;
            }
        }

        private static void AssignCameraReferences() {
            int frame = Time.frameCount;
            if(lastFrame != frame) {
                mainCamera = Camera.main;
                mainCameraTransform = (mainCamera ? mainCamera.transform : null);
                lastFrame = frame;
            }
        }
    }
}