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

public class PlayerShooting : MonoBehaviour {
    [Header("No need to set this ones")]
    public int playerID;
    public string[] inputNames;

    [Header("The max ammo per weapon")]
    public int[] maxAmmo;
    private int[] currentAmmo;
    //TODO: Change names here to the shoot types
    //0 is default, 1 is ----, 2 is ------
    private int shootType;
    [Header("The types of bullets")]
    public GameObject[] bulletTypes;
    [Header("The cooldown between shooting")]
    public float[] maxBulletCooldowns;
    [Header("Where bullets will come from")]
    public Transform bulletPivot;
    public Transform emitterPivot;
    [Header("The difference in between weapons UI")]
    public int diff;

    private RectTransform weaponDisplay;
    private float[] currentBulletCooldowns= new float[3];
    public GameObject arms;
    public GameObject shootEmitter1;
    public GameObject shootEmitter2;
    public GameObject shootEmitter3;
    public AudioClip[] shootSFX;
    private PlayerDisease playerDis;
	// Use this for initialization
	void Start ()
    {
        playerDis = this.gameObject.GetComponent<PlayerDisease>();
        currentAmmo = maxAmmo;
        if (playerID == 0)
        {
            weaponDisplay = InputManager.INPUTMANAGER.player1WeaponDisplay;
            inputNames = InputManager.INPUTMANAGER.player1Inputs;
            
        }
        else
        {
            weaponDisplay = InputManager.INPUTMANAGER.player2WeaponDisplay;
            inputNames = InputManager.INPUTMANAGER.player2Inputs;
        }
        playerDis.inputName = inputNames[3];
        //TODO: Change the 115 for some other number just to get the right amount
        weaponDisplay.anchoredPosition = new Vector2(0,diff);
    }
	
	// Update is called once per frame
	void Update ()
    {
        ShootCooldowns();
        //Shoot depending on the type
	    if(Input.GetButton(inputNames[2]))
        {
            switch (shootType)
            {
                case 0:
                    if(currentBulletCooldowns[0]==0)
                    {
                        AudioSource.PlayClipAtPoint(shootSFX[0], bulletPivot.position);
                        StartCoroutine(ArmMovement());
                        GameObject emitter = Instantiate(shootEmitter1);
                        emitter.transform.position = emitterPivot.position;
                        Destroy(emitter, 0.2f);
                        emitter.transform.SetParent(this.transform);
                        GameObject bullet = Instantiate(bulletTypes[0]);
                        bullet.transform.position = bulletPivot.position;
                        currentBulletCooldowns[0] = maxBulletCooldowns[0];
                    }
                    break;
                case 1:
                    if (currentBulletCooldowns[1] == 0)
                    {
                        AudioSource.PlayClipAtPoint(shootSFX[1], bulletPivot.position);
                        StartCoroutine(ArmMovement());
                        GameObject emitter = Instantiate(shootEmitter2);
                        emitter.transform.position = emitterPivot.position;
                        Destroy(emitter, 0.25f);
                        emitter.transform.SetParent(this.transform);
                        GameObject bullet = Instantiate(bulletTypes[1]);
                        bullet.transform.position = bulletPivot.position;
                        currentBulletCooldowns[1] = maxBulletCooldowns[1];
                    }
                    break;
                case 2:
                    if (currentBulletCooldowns[2] == 0)
                    {
                        AudioSource.PlayClipAtPoint(shootSFX[2], bulletPivot.position);
                        StartCoroutine(ArmMovement());
                        GameObject emitter = Instantiate(shootEmitter3);
                        emitter.transform.position = emitterPivot.position;
                        Destroy(emitter, 0.25f);
                        emitter.transform.SetParent(this.transform);
                        GameObject bullet = Instantiate(bulletTypes[2]);
                        bullet.transform.position = bulletPivot.position;
                        currentBulletCooldowns[2] = maxBulletCooldowns[2];
                    }
                    break;
            }
        }
        //Reload depending on the type
        if (Input.GetButton(inputNames[3]))
        {
            switch (shootType)
            {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
            }
        }
        //Switch weapon
        if (Input.GetButtonDown(inputNames[4]))
        {
            shootType++;
            if(shootType>2)
            {
                weaponDisplay.anchoredPosition = new Vector2(0, 0);
                shootType = 0;
            }
            StopCoroutine(MoveWeaponUI());
            StartCoroutine(MoveWeaponUI());
           
        }
    }

    public IEnumerator MoveWeaponUI()
    {
        float y;
        float start = diff * shootType;
        float end = diff + diff * shootType;
        for (float i = 0; i < 1; i+=0.1f)
        {
            y = Mathf.Lerp(start, end, i);
            weaponDisplay.anchoredPosition = new Vector2(0, y);
            yield return new WaitForEndOfFrame();
        }
        weaponDisplay.anchoredPosition = new Vector2(0,end);
    }

    public void ShootCooldowns()
    {
        if(currentBulletCooldowns[0]>=0)
        {
            currentBulletCooldowns[0] -= Time.deltaTime;
            if(currentBulletCooldowns[0]<=0)
            {
                currentBulletCooldowns[0] = 0;
            }
        }
        if (currentBulletCooldowns[1] >= 0)
        {
            currentBulletCooldowns[1] -= Time.deltaTime;
            if (currentBulletCooldowns[1] <= 0)
            {
                currentBulletCooldowns[1] = 0;
            }
        }
        if (currentBulletCooldowns[2] >= 0)
        {
            currentBulletCooldowns[2] -= Time.deltaTime;
            if (currentBulletCooldowns[2] <= 0)
            {
                currentBulletCooldowns[2] = 0;
            }
        }
    }

    public void MoveArms()
    {
        
    }
    public IEnumerator ArmMovement()
    {
        arms.transform.Translate(Vector3.forward * -0.05f);
        yield return new WaitForSeconds(0.1f);
        arms.transform.Translate(Vector3.forward * 0.05f);
    }
}
