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

public class PickupManager : MonoBehaviour
{
    // 1 - shield | 2 - speedBoost | 3 - flare | 4 - missle
    public int currentPickup;

    [SerializeField]
    public ShieldPickup shieldPickup;

    [SerializeField]
    public SpeedBoostPickup speedBoostPickup;

    [SerializeField]
    public FlarePickup flarePickup;

    [SerializeField]
    public MisslePickup misslePickup;

    // Start is called before the first frame update
    void Start()
    {
        shieldPickup.Start();
        speedBoostPickup.Start();
        flarePickup.Start();
        misslePickup.Start();
    }

    // Update is called once per frame
    void Update()
    {
        if(shieldPickup.playerLife.shieldEnabled)
            shieldPickup.shieldObj.transform.rotation = Quaternion.Euler(0, 0, 0);
    }

    void PickupPickedUp(int index)
    {
        if(index == 1)
        {
            shieldPickup.EnableShield();
        }
        else if(index == 2)
        {
            speedBoostPickup.ShowButton();
        }
        else if (index == 3)
        {
            flarePickup.ShowButton();
        }
        else if (index == 4)
        {
            misslePickup.ShowButton();
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "shield" && !shieldPickup.playerLife.shieldEnabled)
        {
            Destroy(other.gameObject);
            PickupPickedUp(1);
        }

        if (currentPickup == 0)
        {
            if (other.tag == "speedBoost")
            {
                currentPickup = 2;
                Destroy(other.gameObject);
                PickupPickedUp(currentPickup);
            }else if(other.tag == "flare")
            {
                currentPickup = 3;
                Destroy(other.gameObject);
                PickupPickedUp(currentPickup);
            }
            else if (other.tag == "misslePickup")
            {
                currentPickup = 4;
                Destroy(other.gameObject);
                PickupPickedUp(currentPickup);
            }
        }
    }

    public void ActivateSpeedBoost()
    {
       speedBoostPickup.EnableSpeedBoost();
    }

    public void ActivateFlares()
    {
        flarePickup.EnableFlare();
    }

    public void ActivateMissle()
    {
        misslePickup.EnableMissle();
    }

    public void ShootMissle(Transform target)
    {
        if (missleEnabled)
        {
            FrendlyMissle missl = Instantiate(misslePickup.missle, misslePickup.shootPos.position, misslePickup.shootPos.rotation).GetComponent<FrendlyMissle>();
            missl.target = target;
            HideTargets();
            if (target.gameObject.GetComponent<helicopterEnemy>() != null)
            {
                target.gameObject.GetComponent<helicopterEnemy>().targetUItimer = 10;
                target.gameObject.GetComponent<helicopterEnemy>().targetUI.SetActive(true);
                target.gameObject.GetComponent<helicopterEnemy>().targetUI.GetComponent<RectTransform>().localScale = new Vector3(1f, 1f, 1f);
                target.gameObject.GetComponent<helicopterEnemy>().ChasedByPlayerMissle(missl.gameObject);
            }
            else if (target.gameObject.GetComponent<jetEnemy>() != null)
            {
                target.gameObject.GetComponent<jetEnemy>().targetUItimer = 10;
                target.gameObject.GetComponent<jetEnemy>().targetUI.SetActive(true);
                target.gameObject.GetComponent<jetEnemy>().targetUI.GetComponent<RectTransform>().localScale = new Vector3(1f, 1f, 1f);
                target.gameObject.GetComponent<jetEnemy>().ChasedByPlayerMissle(missl.gameObject);
            }
            else if (target.gameObject.GetComponent<missle>() != null)
            {
                target.gameObject.GetComponent<missle>().targetUItimer = 10;
                target.gameObject.GetComponent<missle>().targetUI.SetActive(true);
                target.gameObject.GetComponent<missle>().targetUI.GetComponent<RectTransform>().localScale = new Vector3(1f, 1f, 1f);
                target.gameObject.GetComponent<missle>().ChasedByPlayerMissle(missl.gameObject);
            }

            currentPickup = 0;
        }

        
    }

    public void ClearPickups()
    {
        misslePickup.missleButton.SetActive(false);
        flarePickup.flareButton.SetActive(false);
        speedBoostPickup.speedBoostButton.SetActive(false);
        currentPickup = 0;
    }

    public bool missleEnabled;
    private Transform missleTarget;

    public void EnableMissle()
    {
        helicopterEnemy[] heli = Object.FindObjectsOfType<helicopterEnemy>();
        jetEnemy[] jet = Object.FindObjectsOfType<jetEnemy>();
        missle[] kami = Object.FindObjectsOfType<missle>();

        if (heli.Length > 0)
            foreach(helicopterEnemy heliItem in heli)
            {
                heliItem.GetComponent<helicopterEnemy>().ShowTargeting();
            }

        if (jet.Length > 0)
            foreach (jetEnemy jetItem in jet)
            {
                jetItem.GetComponent<jetEnemy>().ShowTargeting();
            }

        if (kami.Length > 0)
            foreach (missle kamiItem in kami)
            {
                kamiItem.GetComponent<missle>().ShowTargeting();
            }

        missleEnabled = true;
    }

    public void HideTargets()
    {
        helicopterEnemy[] heli = Object.FindObjectsOfType<helicopterEnemy>();
        jetEnemy[] jet = Object.FindObjectsOfType<jetEnemy>();
        missle[] kami = Object.FindObjectsOfType<missle>();

        if (heli.Length > 0)
            foreach (helicopterEnemy heliItem in heli)
            {
                heliItem.GetComponent<helicopterEnemy>().HideTargeting();
            }

        if (jet.Length > 0)
            foreach (jetEnemy jetItem in jet)
            {
                jetItem.GetComponent<jetEnemy>().HideTargeting();
            }

        if (kami.Length > 0)
            foreach (missle kamiItem in kami)
            {
                kamiItem.GetComponent<missle>().HideTargeting();
            }


        missleEnabled = false;
    }

}

[System.Serializable]
public class ShieldPickup
{
    public PLayerLife playerLife;
    public Image[] ShieldUI;
    public GameObject shieldObj;

    public void Start()
    {
        foreach (Image shieldUI in ShieldUI)
        {
            shieldUI.gameObject.SetActive(false);
        }
        shieldObj.SetActive(false);
    }

    public void EnableShield()
    {
        playerLife.shieldEnabled = true;
        shieldObj.SetActive(true);

        if (playerLife.Hp == 1)
            ShieldUI[0].gameObject.SetActive(true);
        else if (playerLife.Hp == 2)
            ShieldUI[1].gameObject.SetActive(true);
        else
            ShieldUI[2].gameObject.SetActive(true);
    }

    public void DisableShield()
    {
        Debug.Log("Shield disabled!");
        playerLife.shieldEnabled = false;

        foreach (Image shieldUI in ShieldUI)
        {
            shieldUI.gameObject.SetActive(false);
        }

        shieldObj.SetActive(false);
    }

    public void UpdateUI()
    {
        if (playerLife.Hp == 1)
            ShieldUI[0].gameObject.SetActive(true);
        else if (playerLife.Hp == 2)
            ShieldUI[1].gameObject.SetActive(true);
        else
            ShieldUI[2].gameObject.SetActive(true);
    }
}

[System.Serializable]
public class SpeedBoostPickup
{
    public PlayerMovement playerMovement;
    public GameObject speedBoostButton;
    public PickupManager pickupManager;

    public void Start()
    {
        speedBoostButton.SetActive(false);
    }

    public void ShowButton()
    {
        speedBoostButton.SetActive(true);
        speedBoostButton.GetComponent<Animation>().Play();
    }

    public void EnableSpeedBoost()
    {
        playerMovement.speedBoostTimer = 5;
        speedBoostButton.SetActive(false);
        pickupManager.currentPickup = 0;
    }
}

[System.Serializable]
public class FlarePickup
{
    public GameObject flareButton;
    public FlareLauncher flareLauncher;
    public PickupManager pickupManager;

    public int numOfFlares;

    public void Start()
    {
        flareButton.SetActive(false);
    }

    public void ShowButton()
    {
        flareButton.SetActive(true);
        flareButton.GetComponent<Animation>().Play();
    }

    public void EnableFlare()
    {
        flareLauncher.ActivateFlares(2);
        flareButton.SetActive(false);
        pickupManager.currentPickup = 0;
    } 

    public void AddFlareCount(int n)
    {
        numOfFlares += n;
    }

    public void RemoveFlareCount(int n)
    {
        numOfFlares -= n;
    }
}

[System.Serializable]
public class MisslePickup
{
    public GameObject missleButton;
    public PickupManager pickupManager;
    public Transform shootPos;
    public GameObject missle;

    public void Start()
    {
        missleButton.SetActive(false);
    }

    public void ShowButton()
    {
        missleButton.SetActive(true);
        missleButton.GetComponent<Animation>().Play();
    }

    public void EnableMissle()
    {
        missleButton.SetActive(false);
        pickupManager.EnableMissle();
    }
}