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

public class Player1Controller : MonoBehaviour
{

    Rigidbody rb;
    public Sprite[] sprites;
    public RuntimeAnimatorController[] PopAnims,RockAnims,NeutralAnims;
    public GameObject powerupIndicator;


    void Start()
    {
        rb = GetComponent<Rigidbody>();
        GetComponent<Animator>().speed = 0;
    }

    void FixedUpdate()
    {

        Vector3 playerSpeedX = new Vector3(PlayerPrefs.GetFloat("Player1Count") / GameController.neutralCount, 0, 0);
        Vector3 playerSpeedY = new Vector3(0, PlayerPrefs.GetFloat("Player1Count") / GameController.neutralCount, 0);

        if (Input.GetKey(KeyCode.RightArrow))
        {
            this.transform.Translate((Vector3.right * 1.3f - playerSpeedX) * Time.deltaTime * 1.5f);
            GetComponent<SpriteRenderer>().flipX = true;
            GetComponent<Animator>().speed = Mathf.Abs(PlayerPrefs.GetFloat("Player1Count") - GameController.neutralCount);
        }
        else if (Input.GetKey(KeyCode.LeftArrow))
        {
            this.transform.Translate((Vector3.left * 1.3f + playerSpeedX) * Time.deltaTime * 1.5f);
            GetComponent<SpriteRenderer>().flipX = false;
            GetComponent<Animator>().speed = Mathf.Abs(PlayerPrefs.GetFloat("Player1Count") - GameController.neutralCount);
        }
        if (Input.GetKey(KeyCode.UpArrow))
        {
            this.transform.Translate((Vector3.up * 1.3f - playerSpeedY) * Time.deltaTime * 1.5f);
            GetComponent<Animator>().speed = Mathf.Abs(PlayerPrefs.GetFloat("Player1Count") - GameController.neutralCount);
        }
        else if (Input.GetKey(KeyCode.DownArrow))
        {
            this.transform.Translate((Vector3.down * 1.3f + playerSpeedY) * Time.deltaTime * 1.5f);
            GetComponent<Animator>().speed = Mathf.Abs(PlayerPrefs.GetFloat("Player1Count") - GameController.neutralCount);
        }


    }

    private void Update()
    {
        if (GameController.P1IdleMania)
        {
            powerupIndicator.SetActive(true);
        }
        else
        {
            powerupIndicator.SetActive(false);
        }
    }

    private void LateUpdate()
    {
        if (Input.GetKeyUp(KeyCode.RightArrow) || Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.UpArrow) || Input.GetKeyUp(KeyCode.DownArrow))
        {
            GetComponent<Animator>().speed = 0;
        }
    }

    private void OnCollisionEnter(Collision other)
    {
        if (gameObject.tag == "Player1")
        {
            if (other.gameObject.tag == "Neutral")
            {
                other.gameObject.tag = "Player1";
                other.gameObject.GetComponent<Rigidbody>().AddForce((other.transform.position - transform.position) * UnityEngine.Random.Range(20, 25));
                other.gameObject.GetComponent<NeutralRandomMove>().enabled = false;
                other.gameObject.GetComponent<Player1Controller>().enabled = true;
                other.gameObject.GetComponent<Player2Controller>().enabled = false;
                other.gameObject.GetComponent<SpriteRenderer>().color = Color.white;
                other.gameObject.GetComponent<Animator>().runtimeAnimatorController = RockAnims[UnityEngine.Random.Range(0,5)];
                float temp = PlayerPrefs.GetFloat("Player1Count");
                PlayerPrefs.SetFloat("Player1Count", ++temp);
                GameController.sounds.PlayMetalSound();
                StartCoroutine(StopForce(other));
            }

            else if (other.gameObject.tag == "Player2")
            {
                if (!GameController.P2IdleMania)
                {
                    other.gameObject.tag = "Idle";
                    other.gameObject.GetComponent<NeutralRandomMove>().enabled = true;
                    other.gameObject.GetComponent<Player1Controller>().enabled = false;
                    other.gameObject.GetComponent<Player2Controller>().enabled = false;
                    other.gameObject.GetComponent<SpriteRenderer>().color = Color.grey;
                    other.gameObject.GetComponent<Animator>().runtimeAnimatorController = NeutralAnims[UnityEngine.Random.Range(0, 5)];
                    other.gameObject.GetComponent<Animator>().speed = 1;
                    other.gameObject.GetComponentInChildren<ParticleSystem>().Play();
                }

                if (GameController.P1IdleMania)
                {
                    float temp = PlayerPrefs.GetFloat("Player2Count");
                    PlayerPrefs.SetFloat("Player2Count", --temp);

                }
                else
                {
                    gameObject.tag = "Idle";
                    gameObject.GetComponent<NeutralRandomMove>().enabled = true;
                    gameObject.GetComponent<Player1Controller>().enabled = false;
                    gameObject.GetComponent<Player2Controller>().enabled = false;
                    gameObject.GetComponent<SpriteRenderer>().color = Color.grey;
                    gameObject.GetComponent<Animator>().runtimeAnimatorController = NeutralAnims[UnityEngine.Random.Range(0, 5)];
                    gameObject.GetComponent<Animator>().speed = 1;
                    gameObject.GetComponentInChildren<ParticleSystem>().Play();
                    float temp = PlayerPrefs.GetFloat("Player1Count");
                    PlayerPrefs.SetFloat("Player1Count", --temp);
                    temp = PlayerPrefs.GetFloat("Player2Count");
                    PlayerPrefs.SetFloat("Player2Count", --temp);


                }
                StartCoroutine(StopIdle(other));
            }

            else if (other.gameObject.tag == "IdleMania")
            {
                GameController.sounds.PlayPowerUp();
                GameController.P1IdleMania = true;
                StartCoroutine(WaitIdleMania());
                Destroy(other.gameObject);
            }
        }


    }

    private IEnumerator WaitIdleMania()
    {
        yield return new WaitForSeconds(3);
        GameController.P1IdleMania = false;
    }

    private IEnumerator StopIdle(Collision other)
    {
        GameController.sounds.PlayIdle();
        yield return new WaitForSeconds(5f);
        other.gameObject.tag = "Neutral";
        //other.gameObject.GetComponent<Animator>().runtimeAnimatorController = NeutralAnims[UnityEngine.Random.Range(0, NeutralAnims.Length)];
        other.gameObject.GetComponent<SpriteRenderer>().color = Color.white;

        gameObject.tag = "Neutral";
        //gameObject.GetComponent<Animator>().runtimeAnimatorController = NeutralAnims[UnityEngine.Random.Range(0, NeutralAnims.Length)];
        gameObject.GetComponent<SpriteRenderer>().color = Color.white;

        other.gameObject.GetComponent<Rigidbody>().drag = 0;
        gameObject.GetComponent<Rigidbody>().drag = 0;
    }

    private IEnumerator StopForce(Collision other)
    {
        yield return new WaitForSeconds(0.15f);
        other.gameObject.GetComponent<Rigidbody>().drag = 100;
        other.gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
        other.gameObject.GetComponent<Rigidbody>().isKinematic = false;
    }
}
