using System;
using UnityEngine;

namespace RebootKit.Engine.Components {
    [Flags]
    public enum TransformComponents {
        None = 0,
        Position = 1 << 0,
        Rotation = 1 << 1,
        Scale = 1 << 2,
        All = Position | Rotation | Scale
    }
    
    [DefaultExecutionOrder(100)]
    public class CopyTransform : MonoBehaviour {
        enum UpdateLoop {
            Update,
            LateUpdate,
            FixedUpdate
        }
        
        [SerializeField] TransformComponents m_Components = TransformComponents.All;
        [SerializeField] Transform m_Source;
        [SerializeField] UpdateLoop m_UpdateLoop = UpdateLoop.LateUpdate;

        Transform m_Transform;

        void Awake() {
            m_Transform = transform;
        }

        void FixedUpdate() {
            if (m_UpdateLoop == UpdateLoop.FixedUpdate) {
                CopyTransformComponents();
            }
        }

        void Update() {
            if (m_UpdateLoop == UpdateLoop.Update) {
                CopyTransformComponents();
            }
        }

        void LateUpdate() {
            if (m_UpdateLoop == UpdateLoop.LateUpdate) {
                CopyTransformComponents();
            }
        }

        void CopyTransformComponents() {
            if ((m_Components & TransformComponents.Position) != 0) {
                m_Transform.position = m_Source.position;
            }
            
            if ((m_Components & TransformComponents.Rotation) != 0) {
                m_Transform.rotation = m_Source.rotation;
            }

            if ((m_Components & TransformComponents.Scale) != 0) {
                m_Transform.localScale = m_Source.localScale;
            }
        }
    }
}