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

namespace SimplifyXR
{

    [DirectiveCategory(DirectiveCategories.Initiator,
        subCategory = DirectiveSubCategory.Camera,
        directiveInfo = "This <i>initiator</i> starts a sequence when a <b>Camera Capture Photo is saved</b>.",
        prettyName = "Photo Capture Saved")]
    public class OnPhotoCaptureSaved : Initiator
    {
        public CameraCapturePanelFeatures cameraCapture;

        public bool listenForCameraCaptureId;

        [Conditional("listenForCameraCaptureId", true, ComparisonType.Equals)]
        public string cameraCaptureId;

        public override List<KnobKeywords> ReceiveKeywords()
        {
            return new List<KnobKeywords>();
        }

        public override List<KnobKeywords> SendKeywords()
        {
            return new List<KnobKeywords>() { new KnobKeywords("Image", typeof(Sprite)) };
        }

        public new void Awake()
        {
            base.Awake();

            cameraCapture =
                cameraCapture == null ?
                Object.FindObjectOfType<CameraCapturePanelFeatures>() :
                cameraCapture;

            cameraCapture?.OnPhotoCaptureSaved
                .AddListener(HandlePhotoCaptureSaved);
        }

        public new void OnDestroy()
        {
            base.OnDestroy();
            cameraCapture?.OnPhotoCaptureSaved
                .RemoveListener(HandlePhotoCaptureSaved);
        }

        public void HandlePhotoCaptureSaved(string id, Sprite sp)
        {
            if (listenForCameraCaptureId)
            {
                if (id == cameraCaptureId)
                {
                    SendData(sp);

                    base.Initiate();
                }
            }
            else
            {
                SendData(sp);
                base.Initiate();
            }
        }

        void SendData(Sprite content)
        {
            AddPassableData(new List<string> { "Image" }, new List<object> { content });
        }
    }
}
