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

public class PlayerCar : MonoBehaviour
{
    public float speed;
    public AudioClip[] radioStations;
    public AudioSource radio;

    private Rigidbody2D rb2D;
    private Vector2 velocity;
    private float rotation;
    bool acceptingInput;
    bool radioOn;
    int radioStationIndex;

    void Awake()
    {
        rb2D = gameObject.GetComponent<Rigidbody2D>();
        acceptingInput = true;
    }

    // Start is called before the first frame update
    void Start()
    {
        velocity = new Vector2(0.0f, 0.0f);
        rotation = 0.0f;
        radioOn = true;
        radioStationIndex = 0;
        radio.clip = radioStations[radioStationIndex];
        radio.Play();
    }

    void Update()
    {
        if (acceptingInput)
        {
            float horizontalInput = Input.GetAxis("Horizontal");
            velocity.x = speed * horizontalInput;
            rotation = -35.0f * horizontalInput;
        }

        if(Input.GetKeyDown(KeyCode.M))
        {
            ToggleRadioMute();
        }

        if(Input.GetKeyDown(KeyCode.R))
        {
            NextRadioStation();
        }
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        // If kinematic dynamics
        //rb2D.MovePosition(rb2D.position + velocity * Time.fixedDeltaTime);
        //rb2D.rotation = rotation;

        //if(acceptingInput )
        //{
        //    rb2D.rotation = rotation;
        //    rb2D.velocity = velocity;
        //}
        //else
        //{
        //    //rb2D.rotation = 0.0f;
        //    //rb2D.velocity = new Vector2();
        //}

        rb2D.rotation = rotation;
        rb2D.velocity = velocity;

    }

    void OnTriggerEnter2D(Collider2D other)
    {
        //Debug.Log("Car OnTriggerEnter2D");
        CarHitSomething(other.gameObject);
    }

    void OnCollisionEnter2D(Collision2D other)
    {
        //Debug.Log("Car OnCollisionEnter2D");
        CarHitSomething(other.gameObject);
    }

    void CarHitSomething(GameObject hitObject)
    {
        Vector2 collisionNormal = new Vector2(1.0f, 1.0f);
        Vector2 collisionForce = collisionNormal * 150.0f;
        GameObject ogo = hitObject.gameObject;
        if (ogo.tag == "Obstruction")
        {
            Obstruction obstruction = ogo.GetComponent<Obstruction>();
            obstruction.HitByCar(collisionForce);
            ImpartImpactEffect(obstruction);
        }
    }

    void ImpartImpactEffect(Obstruction obstruction)
    {
        //Debug.Log("Imparting Effect: " + obstruction.impactEffect.ToString());
        switch(obstruction.impactEffect)
        {
            case ImpactEffect.GoalReached:
                //Debug.Log("Goal reached");
                acceptingInput = false;
                Messenger.Broadcast("GameOverGoalReached");
                break;

            case ImpactEffect.Catastrophic:
                //Debug.Log("Imparting Catastrophic impact effect");
                acceptingInput = false;
                Messenger<string>.Broadcast("GameOverFailure", "You hit something!");
                break;

            case ImpactEffect.BrokenAxle:
                StartCoroutine(AxelDamageEffect());
                //Debug.Log("Imparting BrokenAxle impact effect");
                break;

            case ImpactEffect.SlipperyControl:
                //Debug.Log("Imparting SlipperyControl impact effect");
                break;

            case ImpactEffect.WindshieldDamage:
                //Debug.Log("Imparting WindshieldDamage impact effect");
                break;

            //default:
            //    Debug.Log("Unknown ImpactEffect: " + obstruction.impactEffect.ToString());
        }
    }

    IEnumerator AxelDamageEffect()
    {
        float horizontalInput = 1.0f;
        acceptingInput = false;

        velocity.x = speed * horizontalInput;
        rotation = -35.0f * horizontalInput;
        yield return new WaitForSeconds(0.15f);

        horizontalInput = -1.0f;
        velocity.x = speed * horizontalInput;
        rotation = -35.0f * horizontalInput;
        yield return new WaitForSeconds(0.15f);

        horizontalInput = 1.0f;
        velocity.x = speed * horizontalInput;
        rotation = -35.0f * horizontalInput;
        yield return new WaitForSeconds(0.15f);

        horizontalInput = -1.0f;
        velocity.x = speed * horizontalInput;
        rotation = -35.0f * horizontalInput;
        yield return new WaitForSeconds(0.15f);

        acceptingInput = true;
    }

    void NextRadioStation()
    {
        radioStationIndex = (radioStationIndex + 1) % radioStations.Length;
        Debug.Log("radioStationIndex: " + radioStationIndex.ToString());
        radio.clip = radioStations[radioStationIndex];
        radio.Play();
    }

    void ToggleRadioMute()
    {
        radioOn = !radioOn;
        if (radioOn)
        {
            radio.Play();
        }
        else
        {
            radio.Stop();
        }
    }

    // Handle Impact Result...bcast message. may be better
}
