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

namespace Machete {
	public class Desk : MonoBehaviour {

    [SerializeField]
    Material deskInCheckMat;
    Material regularMat;
    Renderer rend;

    public bool InCheck {
      get { return inCheck; }
      set {
        rend.material = value ? deskInCheckMat : regularMat;
        inCheck = value;
      }
    }
    bool inCheck;

    public Grid Grid {
      get { return grid; }
      set { grid = value; }
    }
    Grid grid;

    public Message Message {
      get { return message; }
      set {
        if (message != null) {
          message.Desk = null;
        }
        message = value;
        if (message != null) {
          message.Desk = this;
        }
      }
    }

    public bool HasMessage {
      get { return message != null; }
    }

    private Message message;


    public Student Student {
      get { return student; }
      set {
        if (student != null) {
          student.Desk = null;
        }
        student = value;
        student.Desk = this;
      }
    }

    public bool HasStudent {
      get { return student != null; }
    }

    private Student student;

    public Vector2Int Coords {
      get { return coords; }
      set { coords = value; }
    }
    private Vector2Int coords;

    public bool IsBorder {
      get { return isBorder; }
      set { isBorder = value; }
    }
    private bool isBorder;

		// Use this for initialization
		public GridDirection IsNeighbor(Desk desk) {
      for(var i = 0; i < 4; i++) {
        if (desk == grid.GetNeighbor(this, (GridDirection) i)) {
          return (GridDirection) i;
        }
      }

      return GridDirection.NoNeighbor;
    }

    void Start() {
      rend = GetComponentInChildren<Renderer>();
      regularMat = rend.material;
    }
	}
}
