using ExposeProperty;
using System;

using UnityEngine;

public class GridElement : ExposableMonobehaviour
{

    [HideInInspector] [SerializeField] public int x;

    [HideInInspector] [SerializeField] public int y;

    [ExposeProperty]
    public int X
    {
        get
        {
            return x;
        }
        set
        {
            x = value;
            UpdateTransform();
        }
    }

    [ExposeProperty]
    public int Y
    {
        get
        {
            return y;
        }
        set
        {
            y = value;
            UpdateTransform();
        }
    }

    public void UpdateTransform()
    {
        transform.localPosition = new Vector3(x * Griddy.Instance.cellWidth, y * Griddy.Instance.cellHeight, transform.localPosition.z);
    }

    public bool IsIn(AbsoluteZone zone)
    {
        return (x >= zone.x && x < zone.x + zone.width && y >= zone.y && y < zone.y + zone.height);
    }

    public void UpdatePositionOnGrid()
    {
        x = (int)(transform.localPosition.x / Griddy.Instance.cellWidth);
        y = (int)(transform.localPosition.y / Griddy.Instance.cellHeight);
        UpdateTransform();
    }

    public void Start()
    {
        UpdatePositionOnGrid();
    }

}
