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

public class HouseProcessor : MonoBehaviour {

    private static List<House> Houses = new List<House>();
    public static void AddHouse(House a)
    {
        Houses.Add(a);
        a.Destroyed += A_Destroyed;
    }

    private static void A_Destroyed(object sender, System.EventArgs e)
    {
        Houses.Remove(sender as House);
    }

    public GameObject FireFX = null;
    public GameObject SmokeFX = null;

    public bool firstTime = true;

    void DrawFirstMinimap()
    {
        Minimap mm = GetComponent<Minimap>();
        foreach (House h in Houses)
        {
            mm.DrawHouse(h);
        }
    }

    public int HousesLost = 0;

    // Update is called once per frame
    void Update()
    {
        if (firstTime)
        {
            DrawFirstMinimap();
            firstTime = false;
        }

        foreach (House house in Houses)
        {
            Combustible c = house.GetComponent<Combustible>();

            if (house.ShowingSuppression && !c.IsSuppressed)
            {
                // Hide suppression
                house.transform.Find("Retardant").GetComponent<MeshRenderer>().enabled = false;
                house.ShowingSuppression = false;
            }
            if (!house.ShowingSuppression && c.IsSuppressed)
            {
                // Show suppression
                house.transform.Find("Retardant").GetComponent<MeshRenderer>().enabled = true;
                house.ShowingSuppression = true;
            }

            if (house.burningState == BurnState.Waiting && c.CurrentTemperature > c.CombustionTemperature * 0.01f)
            {
                house.burningState = BurnState.Smoking;
                if (house.AttachedParticleEffect != null)
                    Destroy(house.AttachedParticleEffect);
                house.AttachedParticleEffect = GameObject.Instantiate(SmokeFX, house.transform);
            }
            if (house.burningState != BurnState.Burning && c.IsBurning)
            {
                // Transitioning to the burning state
                house.burningState = BurnState.Burning;
                GetComponent<Minimap>().DrawHouse(house);

                if (house.AttachedParticleEffect != null)
                    Destroy(house.AttachedParticleEffect);
                house.AttachedParticleEffect = GameObject.Instantiate(FireFX, house.transform);
            }
            if (house.burningState == BurnState.Smoking && c.CurrentTemperature < Mathf.Epsilon)
            {
                // If a house was smoking but the temperature has since dropped back to 0 then remove the smoke
                if (house.AttachedParticleEffect != null)
                    Destroy(house.AttachedParticleEffect);

                house.burningState = BurnState.Waiting;
            }
            if (house.burningState == BurnState.Burning && !c.IsBurning)
            {
                // Transition OUT of burning state - we cooled down for some reason - probably water
                house.burningState = BurnState.Waiting;
                GetComponent<Minimap>().DrawHouse(house);

                if (house.AttachedParticleEffect != null)
                    Destroy(house.AttachedParticleEffect);
            }

            if (house.burningState == BurnState.Burning && c.Fuel < Mathf.Epsilon)
            {
                c.Fuel = 0.0f;
                c.CurrentTemperature = 0.0f;
                house.burningState = BurnState.Smothered;
                GetComponent<Minimap>().DrawHouse(house);

                if (house.AttachedParticleEffect != null)
                {
                    house.AttachedParticleEffect.GetComponent<ParticleSystem>().Stop();
                    Destroy(house.AttachedParticleEffect, 5.0f);
                }

                // Swap out to smothered house model
                house.transform.Find("HouseGeo").gameObject.SetActive(false);
                house.transform.Find("FoundationGeo").GetComponent<MeshRenderer>().enabled = true;

                HousesLost++;
            }
        }
    }
}
