using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
/*
{
    {
      Name: "South Enterance";
      NodeID: 0;
      connectedNode[]:[4,7];
      Type: BoothType.safe;
    }
    {
      Name: "Lip Gloss Testing";
      NodeID: 1;
      connectedNode[]:[11,10,3,2];
      Type: BoothType.source;
    }
    {
      Name: "Cold Sore Clinic ";
      NodeID: 2;
     connectedNode[]:{3,6,1,9,10];
     Type: BoothType.safe;
    }
    {
     Name: "Bottle Spin Nationals";
     NodeID: 3;
     connectedNode[]:[11,1,2,6,4,5];
     Type: BoothType.trade;
    }
    {
     Name: "Kissing Booth";
     NodeID: 4;
     connectedNode[]:[0,3,5,6];
     Type: BoothType.source;
    }
    {
     Name: "CPR Training";
     NodeID: 5;
     connectedNode[]:[11,3,4];
     Type: BoothType.trade;
    }
    {
     Name: "Water Fountain";
     NodeID: 6;
     connectedNode[]:[3,2,9,7,4];
     Type: BoothType.source;
    }
    {
     Name: "Church";
     NodeID: 7;
     connectedNode[]:[0,6,12];
     Type: BoothType.source;
    }
    {
     Name: "Ball Pit";
     NodeID: 8;
     connectedNode[]:[12,9];
     Type: BoothType.source;
    }
    {
     Name: "food Court";
     NodeID: 9;
     connectedNode[]:[10,6,8,12];
     Type: BoothType.safe;
    }
    {
     Name: "bathroom";
     NodeID: 10;
     connectedNode[]:[1,2,9,];
     Type: BoothType.safe;
    }
    {
     Name: "West Enterance";
     NodeID: 11;
     connectedNode[]:[1,3,5];
     Type: BoothType.safe;
    }
    {
     Name: "East Enterance";
     NodeID: 12;
     connectedNode[]:[9,8,6,7];
     Type: BoothType.safe;
    }

*/

public class Booth : MonoBehaviour
{
    private static int CompareName(Npc x, Npc y)
    {
        string nameX = x.gameObject.name;
        string nameY = y.gameObject.name;
        return nameX.CompareTo(nameY);
    }
    
    private GameManager gm;
    public enum BoothType {safe, source, trade};
    public Booth[] adjacentBooths;
    public int boothNode;
    public bool[] infectedTimeLine;
    public List<Npc> npcs = new List<Npc>();
    public BoothType boothType;

    public static int currTime;

    // initalize the booth 
    public void InitBooth(int numberOfTurns)
    {
        infectedTimeLine = new bool[numberOfTurns];
    }
    

	// Use this for initialization
	void Start ()
    {
        gameObject.tag = "Booth";
	}
	
	// Update is called once per frame
	void Update ()
    {
		// booth animations
	}

    /* booth types
     * - "safe" no transfer of infection happens here
     *  --door, bathroom, food court, cold sore clinic.
     * 
     * - infected source, all people who go to this booth become infected
     * -- church, kissing booth, ballpit, water fountain
     *  
     * - trading spot, if a person at the booth is infected they infect N other people
     * -- CPR course, bottle spin,
     * 
     */
    private void Contract()
    {
        for (int iii = currTime; iii < infectedTimeLine.Length; iii++)
        {
            infectedTimeLine[iii] = true;
        }
    }

    private void CalcInfection()
    {
   
        npcs.Sort(CompareName);
        switch (boothType)
        {
            case BoothType.safe:
                // do nothing
                break;
            case BoothType.source:
                if(npcs.Any(s => s.Infected[currTime]))
                {
                    Contract();
                }
                if (infectedTimeLine[currTime])
                {
                    foreach (Npc npc in npcs)
                    {
                        npc.Contract(false);
                    }
                }
                /* if the booth is infected infect each npc there */
                break;
            case BoothType.trade:
                for (int i = 0; i< npcs.Count-1; i+=2)
                {
                   if( npcs[i].Infected[currTime] || npcs[i + 1].Infected[currTime])
                    {
                        npcs[i].Contract(false);
                        npcs[i + 1].Contract(false);
                    }
                }
                // pair up NPCs, 

                // check for infection 
                // spread infection

                //do fancy things
                break;
            default:
                break;

        }
        npcs.Clear();
    }

    public void BoothStepForward() {
       Invoke("CalcInfection", 1);
    }

    public void BoothStepBack() {
        Invoke("CalcInfection", 1);
    }


}
