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

public class _GasBarController : MonoBehaviour
{
	public float CurrentGas = 10;
	public float MaxGas = 10;
    public float RunOutGas = 0;
    public float SpeedGas = 1;
	public Transform GasBarPreFab;
	public Transform GasBombPreFab;
	private Transform GasBar;
	private Transform GasBomb;
	private Transform canvas;

    void Start()
    {
    	canvas = GameObject.Find("CanvasUI").transform;
    	GasBar = Instantiate(GasBarPreFab);
    	GasBar.transform.SetParent(canvas);
    	GasBomb = Instantiate(GasBombPreFab);
    	GasBomb.transform.SetParent(canvas);

    	// Set gas bomb original position
        GasBomb.transform.localPosition = new Vector3(
    	   												(Screen.width/2)-(GasBomb.GetComponent<RectTransform>().rect.width/2)-10,
	       												(-Screen.height/2)+(GasBomb.GetComponent<RectTransform>().rect.height/2)+50,
        												0
        											);

        // Set gas bar original position
        GasBar.transform.localPosition = new Vector3(
    	   												(Screen.width/2)-(GasBar.GetComponent<RectTransform>().rect.width/2)-10,
	       												(-Screen.height/2)+(GasBomb.GetComponent<RectTransform>().rect.height/2)+50,
        												0
        											);

		// Set gas meter color as green
        GasBar.GetComponent<Image>().color = new Color (0,1,0);
    }

    void Update()
    {
		// Change the gas meter color based on current gas
		if(CurrentGas<MaxGas*0.5){
			GasBar.GetComponent<Image>().color = new Color (1,1,0);
		}
		if(CurrentGas<MaxGas*0.25){
			GasBar.GetComponent<Image>().color = new Color (1,0,0);
		}
		// Resize the gas meter based on current gas
		GasBar.GetComponent<RectTransform>().localScale = new Vector3(1,CurrentGas/MaxGas,1);


        // Reduce gas bar
        if(RunOutGas == 0){
            CurrentGas = CurrentGas - SpeedGas*Time.deltaTime;
        }

        // Flag Gas ran out
        if(CurrentGas <= 0){
            RunOutGas = 1;
        }

    }
}