using NatSuite.Examples;
using UnityEngine;
using UnityEngine.Events;

namespace XvXR.Foundation
{
    /// <summary>
    /// Ҫ𱾵Ƶ¼ƺͽͼ
    /// </summary>
    [RequireComponent(typeof(ReplayCam))]
    [RequireComponent(typeof(JPG))]

    public sealed class XvMediaRecorderManager : MonoBehaviour
    {
        private XvMediaRecorderManager() { }
        private ReplayCam replayCam;
        private JPG jpg;

        [SerializeField]
        private XvMRVideoCaptureManager xvMRVideoCaptureManager;
        public XvMRVideoCaptureManager XvMRVideoCaptureManager {

            get {

                if (xvMRVideoCaptureManager == null)
                {
                    xvMRVideoCaptureManager = FindObjectOfType<XvMRVideoCaptureManager>();
                }

                if (xvMRVideoCaptureManager == null)
                {
                    GameObject newObj = Instantiate(Resources.Load<GameObject>("XvMRVideoCaptureManager"));
                    xvMRVideoCaptureManager = newObj.GetComponent<XvMRVideoCaptureManager>();
                    newObj.name = "XvMRVideoCaptureManager";
                }

                return xvMRVideoCaptureManager;
            }
        }
     
        public int Width { 
        get { return XvMRVideoCaptureManager.CameraManager.Width; }
        }
       
        public int Height { 
        get { return XvMRVideoCaptureManager.CameraManager.Height; }
        }

        private void Awake()
        {
            replayCam=GetComponent<ReplayCam>();
            jpg=GetComponent<JPG>();
            replayCam.videoWidth = Width;
            replayCam.videoHeight = Height;
            jpg.imageWidth= Width; 
            jpg.imageHeight= Height;
            jpg.cam = XvMRVideoCaptureManager.BgCamera;
            replayCam.cam = XvMRVideoCaptureManager.BgCamera;

        }


        /// <summary>
        /// ׽ʵƵ
        /// </summary>
        public void StartCapture() {
            XvMRVideoCaptureManager.StartCapture();
        }
        /// <summary>
        /// ֹͣ׽ʵƵ
        /// ע⣺ǹõģرյʱҪģǷҪõģʹcloseCamera==false
        /// </summary>
        /// <param name="closeCamera">true:ر   false:ر</param>
        public void StopCapture(bool closeCamera=false)
        {
            XvMRVideoCaptureManager.StopCapture(closeCamera);
        }

        /// <summary>
        /// ʼ¼ٿʼ¼ǰҪȷStartCaptureѾãʵƵѾ
        /// </summary>

        public void StartRecording()
        {
            StartCapture();
            replayCam.StartRecording();
        }

        /// <summary>
        /// ֹͣ¼
        /// </summary>
        /// <param name="callback"></param>
        public void StopRecording(UnityAction<string> callback)
        {

            replayCam.StopRecording(callback);
        }

        public bool IsVideoRecording() {
            return replayCam.IsRecording;
        }

        /// <summary>
        /// ͼͼǰҪȷStartCaptureѾãʵƵѾ
        /// </summary>
        /// <param name="callback"></param>
        public void SaveScreenshot(UnityAction<string> callback)
        {
            StartCapture();

            jpg.SaveScreenshot(callback);
        }

        public bool IsTakingScreenshots() {
            return jpg.IsRecording;


        }
    }
}
