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

public class PlayerRoomZone : MonoBehaviour {

    public Equipment equipment;

    public Equipment hoverEquipment;

    void OnMouseOver()
    {
        if(hoverEquipment == null)
        {
            Equipment selectedEquipment = EquipmentPanel.Instance.SelectedEquipment;
            if(selectedEquipment != null)
            {
                hoverEquipment = Instantiate(selectedEquipment);
                WaveSource waveSource = hoverEquipment.GetComponent<WaveSource>();
                waveSource.transform.SetParent(transform, false);
                waveSource.position = new Vector2I(
                    waveSource.position.x + (int)(transform.position.x / Griddy.Instance.cellWidth),
                    waveSource.position.y + (int)(transform.position.y / Griddy.Instance.cellHeight)
                );
                hoverEquipment.SetInHoverMode();
            }
        }
    }

    private void AddWaveSource()
    {
        if (equipment != null && equipment.GetComponent<WaveSource>() != null)
        {
            Level.Instance.AddWave(equipment.GetComponent<WaveSource>());
        }
    }

    void OnMouseExit()
    {
        if(hoverEquipment != null)
        {
            Destroy(hoverEquipment.gameObject);
        }
    }

    void OnMouseDown()
    {
        if(hoverEquipment != null)
        {
            if (hoverEquipment.cost <= Level.Instance.Money)
            {
                equipment = hoverEquipment;
                hoverEquipment = null;
                equipment.SetInNormalMode();
                Level.Instance.Money -= equipment.cost;

                Invoke("AddWaveSource", Level.Instance.attackDelay);

                equipment.onPlaced.Invoke();

                EquipmentPanel.Instance.UpgradePlaced();
            }
            else
            {
                //TODO: signal (audio ?) indiquant qu'on n'a pas assez d'argent pour poser l'objet
            }
        }
    }

    void Start()
    {
        GetComponent<BoxCollider>().enabled = false;
    }

}
