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

public class Engines : MonoBehaviour
{
    public GameObject repairPrompt, repairButton, engineButton;
    public Text fuelText, brokenText;
    public bool isRepaired { get; private set; }

    private void Awake()
    {
        repairPrompt.SetActive(true);
        repairButton.SetActive(false);
        engineButton.SetActive(false);
    }

    public void RefreshRepairButton()
    {
        int repairItemCount = 0;
        foreach(SpaceObject item in GameManager.instance.inventory.items)
        {
            if (item is Repair || item is Fuel)
            {
                repairItemCount++;
            }            
        }

        if (repairItemCount == 3)
        {
            repairButton.SetActive(true);
        }
        else
        {
            repairButton.SetActive(false);
        }
    }

    public void RepairEngines()
    {
        isRepaired = true;
        repairButton.SetActive(false);
        repairButton.SetActive(false);
        engineButton.SetActive(true);
        brokenText.gameObject.SetActive(false);
        GameManager.instance.engine.RefreshFuelStatus();

        List<SpaceObject> items = GameManager.instance.inventory.items;

        foreach (InventorySlot slot in GameManager.instance.inventorySlots)
        {
            if (slot.spaceObject is Repair || slot.spaceObject is Fuel)
            {
                GameManager.instance.inventory.RemoveFromInventory(slot.spaceObject);
                slot.RemoveItem();
            }
        }

        foreach (SpaceObject spaceObject in GameManager.instance.spaceObjects)
        {
            if (spaceObject is Repair || spaceObject is Fuel)
            {
                spaceObject.transform.position = new Vector3(-999999, 0, 0);
                spaceObject.velocity = Vector3.zero;                
                StartCoroutine(spaceObject.DestroyAfterDelay());
            }
        }
    }

    public void RefreshFuelStatus()
    {
        fuelText.text = "Fuel: " + GameManager.instance.ship.fuelAmount.ToString("0") + "%";
    }
}
