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

public class MinimapIcon : MonoBehaviour
{
    private SpriteRenderer spriteRenderer;
    private bool doesRotate;

    private void Awake()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
    }

    private void Start()
    {
        StartCoroutine(DelayedSetup());
    }

    private void FixedUpdate()
    {
        if (doesRotate == false)
            transform.rotation = Quaternion.identity;
    }

    private IEnumerator DelayedSetup()
    {
        yield return new WaitForEndOfFrame();
        
        gameObject.layer = LayerMask.NameToLayer("Minimap");
        transform.localScale = new Vector3(16, 16, 1);
        SpaceObject spaceObject = transform.parent.GetComponent<SpaceObject>();
        if (spaceObject is Junk)   spriteRenderer.sprite = GameManager.instance.iconJunk;
        if (spaceObject is Cat)    spriteRenderer.sprite = GameManager.instance.iconCat;
        if (spaceObject is Fuel)   spriteRenderer.sprite = GameManager.instance.iconFuel;
        if (spaceObject is Repair) spriteRenderer.sprite = GameManager.instance.iconRepair;

        if (spaceObject is Ship)
        {
            spriteRenderer.sprite = GameManager.instance.iconShip;
            doesRotate = true;
        }
    }

    public void HideIcon()
    {
        spriteRenderer.enabled = false;
    }

    public void ShowIcon()
    {
        spriteRenderer.enabled = true;
    }
}
