using UnityEngine;
using System.Collections;

public class Fire : MonoBehaviour
{
    public float startTime;
    public float spreadTime = 4.0f;
    public bool canSpawn = true;
    public GameObject North, South, East, West;
    public GameObject firePrefab;

	// Use this for initialization
	void Start () {
        startTime = Time.time;
	}
	
	// Update is called once per frame
	void Update () {
        if (canSpawn)
        {
            if (Time.time > startTime + spreadTime)
            {
                startTime = Time.time;
                SpreadFire();
            }
        }
	}

    void SpreadFire()
    {
        var GameObjectToSpawn = GetRandomObject();

        if (!canSpawn)
        {
             return;
        }
    }

    GameObject GetRandomObject()
    {
        if (North != null && South != null && East != null && West != null)
        {
            canSpawn = false;
            return null;
        }

        var direction = Random.Range(1, 5);
        switch (direction)
        {
            //North = GameObject.Instantiate(firePrefab, North.transform.position, Quaternion.identity) as GameObject; 
            case 1:
                if (North == null)
                {
                    North = GameObject.Instantiate(firePrefab, transform.position + Vector3.forward*2, Quaternion.identity) as GameObject;
                    North.GetComponent<Fire>().South = gameObject;
                    return North;
                }
                return null;
                break;
            case 2:
                if (South == null)
                {
                    South = GameObject.Instantiate(firePrefab, transform.position + Vector3.forward*-2, Quaternion.identity) as GameObject;
                    South.GetComponent<Fire>().North = gameObject;
                    return South;
                }
                return null;
                break;
            case 3:
                if (East == null)
                {
                    East = GameObject.Instantiate(firePrefab, transform.position + Vector3.right*2, Quaternion.identity) as GameObject;
                    East.GetComponent<Fire>().West = gameObject;
                    return East;
                }
                return null;
                break;
            case 4:
                if (West == null)
                {
                    West = GameObject.Instantiate(firePrefab, transform.position + Vector3.right*-2, Quaternion.identity) as GameObject;
                    West.GetComponent<Fire>().East = gameObject;
                    return West;
                }
                return null;
                break; 
            default:
                return null;
        }
    }
    
}
