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

public class DrinkManager : MonoBehaviour
{

    private static DrinkManager _instance = null;
    public static DrinkManager Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = FindObjectOfType<DrinkManager>();
            }
            return _instance;
        }
    }


    private Dictionary<Drink, DrinkRenderer> DrinkMappings = new Dictionary<Drink, DrinkRenderer>();

    [SerializeField]
    private Transform DrinkRendererRoot = null;

    [SerializeField]
    private GameObject DrinkRendererPrefab = null;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public Drink CreateNewDrink()
    {
        Drink result = new Drink();

        // This is a physical drink if it's being made by the drink manager (I think?)
        result.drinkOrigination = DrinkRenderMode.Physical;

        GameObject renderedDrinkView = GameObject.Instantiate(DrinkRendererPrefab, DrinkRendererRoot);
        DrinkMappings.Add(result, renderedDrinkView.GetComponent<DrinkRenderer>());

        renderedDrinkView.GetComponent<DrinkRenderer>().Drink = result;

        return result;
    }

    public void DestroyDrink(Drink drink)
    {
        DrinkRenderer renderedDrinkView = DrinkMappings[drink];
        if (renderedDrinkView)
            Destroy(renderedDrinkView.gameObject);

        DrinkMappings.Remove(drink);
    }

    public void SetDrinkRendererAnchor(Drink drink, GameObject target, string hardpoint)
    {
        DrinkRenderer renderedDrinkView = DrinkMappings[drink];
        renderedDrinkView.GetComponent<UIElementOnWorldTransform>().Target= target.transform;
        renderedDrinkView.GetComponent<UIElementOnWorldTransform>().Hardpoint = hardpoint;

    }
}
