using System.Collections;
using System;
using UnityEngine;

public class Launcher : MonoBehaviour
{
    [SerializeField] private Rocket _rocketPrefab = null;
    [SerializeField] private Transform _launchPosition = null;
    [SerializeField] private Vector2 _launchPositionMinMax = Vector2.zero;
    [SerializeField] private float _launchSpeed = 1.0f;
    [SerializeField] private float _rotationAngle = 60.0f;
    [SerializeField] private float _translationSpeed = 1.0f;

    private bool _isActive = false;
    private bool _isLock = false;
    private Rocket _currentRocket = null;
    private float _translationCumulatedTime = 0.0f;
    private float _rotationCumulatedTime = 0.0f;

    private bool _translationActive = false;
    private bool _rotationActive = false;

    public Rocket CurrentRocket => _currentRocket;

    public void Start()
    {
        GameManager.Instance.GameStateChanged += OnGameStateChanged;
    }

    public void OnDestroy()
    {
        GameManager.Instance.GameStateChanged -= OnGameStateChanged;
    }

    public void OnGameStateChanged(GameState state)
    {
        Debug.Log("Game Mode changed !");
        if (state == GameState.TrajectoryMix)
        {
            Initialize();
        }
        else
        {
            TearDown();
        }
    }

    private void Initialize()
    {
        //CreateRocket();
        //InputManager.Instance.PrimaryAction += LaunchRocket;
        //InputManager.Instance.SecondaryAction += ChooseRocketSpeed;
        //InputManager.Instance.TertiaryAction += CreateRocket;

        _isActive = true;
        _translationActive = true;
    }

    private void TearDown()
    {
        _isActive = false;

        //InputManager.Instance.PrimaryAction -= LaunchRocket;
        //InputManager.Instance.SecondaryAction -= ChooseRocketSpeed;
        //InputManager.Instance.TertiaryAction -= CreateRocket;
    }

    public void StartRotation()
    {
        _translationActive = false;
        _rotationActive = true;
    }

    public void StopRotation()
    {
        _rotationActive = false;
    }

    public void CreateRocket()
    {
        _currentRocket = Instantiate(_rocketPrefab, _launchPosition);
    }

    public void Reset()
    {
        _isActive = true;
        _isLock = false;
        _translationActive = true;
        _rotationActive = false;
    }

    public void LaunchRocket(Trajectory trajectory, float speed)
    {
        _currentRocket.Launch(trajectory, transform.position, speed);
    }

    private void ChooseRocketSpeed()
    {
        
    }

    public void LockRotation()
    {
        _isLock = true;
    }

    private void Update()
    {
        if (_isActive == false || _isLock)
        {
            return;
        }

        // Move the launcher from left to right automatically
        if (_translationActive)
        {
            LateralMovesCoroutine();
        } 
        else if (_rotationActive)
        {
            _rotationCumulatedTime += Time.deltaTime;
            float angle = Mathf.Cos(_rotationCumulatedTime);
            //Debug.Log(angle);

            Vector3 nextEulerRotation = Vector3.Lerp(new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, _rotationAngle),
                new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, -_rotationAngle), Mathf.Abs(angle));

            _currentRocket.transform.eulerAngles = nextEulerRotation;
        }
    }

    private void LateralMovesCoroutine()
    {
        Vector3 position = _launchPosition.localPosition;

        float sin = Mathf.Sin(_translationCumulatedTime * _translationSpeed) * 0.5f + 0.5f;

        float positionX = Mathf.Lerp(_launchPositionMinMax.x, _launchPositionMinMax.y, sin);

        position.x = positionX;
        _launchPosition.localPosition = position;

        _translationCumulatedTime += Time.deltaTime;
    }
}
