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

namespace Arugula.Prototypical
{
    public class OverCamera : MonoBehaviour
    {
        public enum UpdateMode
        {
            Update, LateUpdate, FixedUpdate, BeforeRender
        }

        public Transform target;
        public UpdateMode updateMode;
        public bool useUnscaledTime = true;
        public float dampener = 15;
        public Vector3 positionOffset;



        private void Start()
        {
            if (updateMode == UpdateMode.BeforeRender)
                Application.onBeforeRender += OnBeforeRender;
        }

        private void OnBeforeRender()
        {
            Apply();
        }

        private void Update()
        {
            if (updateMode == UpdateMode.Update)
                Apply();
        }

        private void LateUpdate()
        {
            if (updateMode == UpdateMode.LateUpdate)
                Apply();
        }

        private void FixedUpdate()
        {
            if (updateMode == UpdateMode.FixedUpdate)
                Apply();
        }

        void Apply()
        {
            if (target == null)
                return;

            transform.position = Vector3.Lerp(transform.position, target.position + positionOffset, (useUnscaledTime ? Time.unscaledDeltaTime : Time.deltaTime) * dampener);
        }

        [ContextMenu("Set Offset")]
        void SetPositionOffset()
        {
            if (target == null)
                return;

            positionOffset = transform.position - target.position;
        }

        [ContextMenu("Look At Target")]
        void LookAtTarget()
        {
            if (target == null)
                return;

            transform.LookAt(target);
        }
    }

}
