/**********************************************************************************
* Blueprint Reality Inc. CONFIDENTIAL
* 2019 Blueprint Reality Inc.
* All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains, the property of
* Blueprint Reality Inc. and its suppliers, if any.  The intellectual and
* technical concepts contained herein are proprietary to Blueprint Reality Inc.
* and its suppliers and may be covered by Patents, pending patents, and are
* protected by trade secret or copyright law.
*
* Dissemination of this information or reproduction of this material is strictly
* forbidden unless prior written permission is obtained from Blueprint Reality Inc.
***********************************************************************************/

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using BlueprintReality.MixCast.Shared;

namespace BlueprintReality.MixCast.Cameras
{
    public abstract class SetVisibilityForMixCast : MonoBehaviour
    {
        public enum MixCastCameraMode
        {
            Always, IfAnyConditionMet, IfAllConditionsMet
        }

        public enum RenderTypeCondition
        {
            NotUsed, Mixed, Virtual
        }
        public enum PerspectiveCondition
        {
            NotUsed, First, Third
        }

        public bool showForRegularCameras = true;
        public bool showForMixCastCameras = true;
        public MixCastCameraMode mixCastCameraMode = MixCastCameraMode.Always;
        public RenderTypeCondition renderTypeCondition = RenderTypeCondition.NotUsed;
        public PerspectiveCondition perspectiveCondition = PerspectiveCondition.NotUsed;

        protected virtual void OnEnable()
        {
            ExpCameraBehaviour.FrameStarted += HandleMixCastRenderStarted;
            ExpCameraBehaviour.FrameEnded += HandleMixCastRenderEnded;
            
            SetVisibility(showForRegularCameras);
        }
        protected virtual void OnDisable()
        {
            ExpCameraBehaviour.FrameStarted -= HandleMixCastRenderStarted;
            ExpCameraBehaviour.FrameEnded -= HandleMixCastRenderEnded;

            SetVisibility(true);
        }

        private void HandleMixCastRenderStarted(ExpCameraBehaviour cam, VirtualCamera camInfo)
        {
            bool showForThisMixCastCamera = showForMixCastCameras;
            switch(mixCastCameraMode)
            {
                case MixCastCameraMode.Always:
                    break;
                case MixCastCameraMode.IfAnyConditionMet:
                    if (!HasAnyFlag(camInfo))
                        showForThisMixCastCamera = !showForThisMixCastCamera;
                    break;
                case MixCastCameraMode.IfAllConditionsMet:
                    if (!HasAllFlags(camInfo))
                        showForThisMixCastCamera = !showForThisMixCastCamera;
                    break;
            }
            SetVisibility(showForThisMixCastCamera);
        }
        private void HandleMixCastRenderEnded(ExpCameraBehaviour cam)
        {
            SetVisibility(showForRegularCameras);
        }

        protected abstract void SetVisibility(bool visible);

        protected bool HasAnyFlag(VirtualCamera camInfo)
        {
            if (renderTypeCondition == RenderTypeCondition.Mixed)
            {
                if (camInfo.UsesForeground)
                    return true;
            }
            else if (renderTypeCondition == RenderTypeCondition.Virtual)
            {
                if (!camInfo.UsesForeground)
                    return true;
            }

            if( perspectiveCondition == PerspectiveCondition.First )
            {
                if (camInfo.IsFirstPerson)
                    return true;
            }
            else if( perspectiveCondition == PerspectiveCondition.Third )
            {
                if (!camInfo.IsFirstPerson)
                    return true;
            }

            return false;
        }
        protected bool HasAllFlags(VirtualCamera camInfo)
        {
            if (renderTypeCondition == RenderTypeCondition.Mixed)
            {
                if (!camInfo.UsesForeground)
                    return false;
            }
            else if (renderTypeCondition == RenderTypeCondition.Virtual)
            {
                if (camInfo.UsesForeground)
                    return false;
            }

            if (perspectiveCondition == PerspectiveCondition.First)
            {
                if (!camInfo.IsFirstPerson)
                    return false;
            }
            else if (perspectiveCondition == PerspectiveCondition.Third)
            {
                if (camInfo.IsFirstPerson)
                    return false;
            }

            return true;
        }
    }
}
