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

public class GameManager : MonoBehaviour
{
    private int levelIdx;

    private bool isShakeDone;
    private bool isTurtleAwake;
    private bool isGateOpened;
    private bool isBatteriesCharged;

    public GameObject[] Levels;
    public static GameManager Singleton;

    public SoundManager SoundManagerComponent;

    public CharacterManager animationManager;

    //Battery
    private float batteryCharge = 0;
    private float maxBatteryCharge = 100;

    public Transform[] BatteryLevels;
    public float BatteryMaxLevelYScale;

    public GameObject Portal;
    public GameObject TurtleDestination;

    //Gate
    public GameObject Gate;
    private float GateOpeningSpeed = 2;
    
    // Earthquake
    public EarthquakeCamera MainCamera;
    public GameObject RocksWall;

    //Turtle
    public Animator TurtleAnimator;
    private float TurtleSpeed = 5;
    
	public void LoadLevel(int level)
    {
        SceneManager.LoadScene(level);
    }

    void Awake()
    {
        Singleton = this;

        Screen.sleepTimeout = SleepTimeout.NeverSleep;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
            SceneManager.LoadScene(0);

        //Debug
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            ShakeCommand();
        }
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            Shout();
        }
        else if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            OpenGate();
        }
        else if (Input.GetKeyDown(KeyCode.Alpha4))
        {
            ChargeBattery(5);
        }
    }

    public void ShakeCommand()
    {
        if (levelIdx == 0)
        {
            if (!isShakeDone)
            {
                isShakeDone = true;
                animationManager.PlayAnimation(AnimationType.Cast);
                SoundManagerComponent.PlaySound(SoundEffect.Rumble);

                MainCamera.smoothAmount = 3;
                MainCamera.ShakeCamera(5, 3);

                Rigidbody[] rbs = RocksWall.GetComponentsInChildren<Rigidbody>();
                foreach (Rigidbody rb in rbs)
                    rb.isKinematic = false;

                StartCoroutine(animationManager.MoveToNextLevel());
            }
        }

    }


    public void OpenGate()
    {
        if (levelIdx == 2)
        {
            if (!isGateOpened)
            {
                isGateOpened = true;
                StartCoroutine(OpenGateAnimation());
            }
        }
    }

    private IEnumerator OpenGateAnimation()
    {
        animationManager.AntennaEffect.SetActive(true);
        yield return new WaitForSeconds(1f);
        
        SoundManagerComponent.PlaySound(SoundEffect.OpenGate);

        float AnimationTime = 0.8f;
        float t = 0;
        while(t < AnimationTime)
        {
            t += Time.deltaTime;
            Gate.transform.Translate(-transform.forward * GateOpeningSpeed * Time.deltaTime);
            yield return null;
        }
        
        StartCoroutine(animationManager.MoveToNextLevel());
    }

    public void ChargeBattery(float charge)
    {
        if (levelIdx == 3)
        {
            if (!isBatteriesCharged)
            {
                if (charge > 0)
                    SoundManagerComponent.PlaySound(SoundEffect.Charge);
                else
                    SoundManagerComponent.StopSound(SoundEffect.Charge);
                if (batteryCharge < maxBatteryCharge)
                {
                    batteryCharge += charge;

                    if (batteryCharge >= maxBatteryCharge)
                    {
                        isBatteriesCharged = true;
                        batteryCharge = maxBatteryCharge;
                        OpenPortal();
                    }

                    foreach(Transform t in BatteryLevels)
                    t.localScale = new Vector3(t.localScale.x, BatteryMaxLevelYScale * batteryCharge / maxBatteryCharge, t.localScale.z);

                }
            }
        }
    }

    public void Shout()
    {
        if (levelIdx == 1)
        {
            if (!isTurtleAwake)
            {
                isTurtleAwake = true;
                animationManager.PlayAnimation(AnimationType.Shout);

                StartCoroutine(MoveTurtle());
            }
        }
    }

    private IEnumerator MoveTurtle()
    {
        TurtleAnimator.GetComponent<AudioSource>().Stop();

        TurtleAnimator.SetTrigger("Walk");

        while(Vector3.Distance( TurtleAnimator.transform.position, TurtleDestination.transform.position) > 0.1f)
        {
            TurtleAnimator.transform.position = Vector3.MoveTowards(TurtleAnimator.transform.position, TurtleDestination.transform.position, 0.6f * Time.deltaTime);
            yield return null;
        }
        StartCoroutine(animationManager.MoveToNextLevel());
        
    }

    private void OpenPortal()
    {
        Portal.SetActive(true);
        StartCoroutine(animationManager.MoveToNextLevel());
    }

    public void ChangeScene()
    {
        Levels[levelIdx++].SetActive(false);
        if (levelIdx < 4)
        {
            Levels[levelIdx].SetActive(true);
            animationManager.transform.position = animationManager.StartingPoint.position;
        }
        else
            SceneManager.LoadScene(0);
    }

}
