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

public class Multiply : Switcher {


	public List<ActivableObject> Targets;

	Material CurMat;
	public List<Switcher> Inputs;
	List<Material> Boutons;

	// Use this for initialization
	void Start () {
		MeshRenderer MR = GetComponent<MeshRenderer>();
		CurMat = MR.material;
		SetState(IsOn);
		Boutons = new List<Material>();
		Transform Bout = transform.FindChild("Bouton");
		MeshRenderer BouMR = Bout.GetComponent<MeshRenderer>();
		Boutons.Add(BouMR.material);
		for(int i=1; i<Inputs.Count; ++i) {			
			GameObject GO = GameObject.Instantiate(Bout.gameObject, Bout.parent);
			Vector3 BasePos = GO.transform.localPosition;
			GO.transform.localPosition = BasePos + Vector3.forward * (i * 0.2f);
			MeshRenderer GOMR = GO.GetComponent<MeshRenderer>();
			Boutons.Add(GOMR.material);
		}
	}

	void Update() {
		bool NewIsOn = false;
		bool SlotMissing = false;
		for(int i=0; i<Inputs.Count; ++i) {
			Switcher Input = Inputs[i];
			Material Mat = Boutons[i];
			if(Input) {
				if(!Input.IsOn) {
					SlotMissing = true;
				}
			}
			Mat.color = (Input && Input.IsOn) ? Color.green * 10.0f : Color.red;
		}

		NewIsOn = !SlotMissing;

		if(NewIsOn != IsOn) {
			IsOn = NewIsOn;
			SetState(NewIsOn);
		}
	}

	public override void SetState(bool NewOn) {
		base.SetState(NewOn);
		CurMat.color = IsOn ? Color.green * 10.0f : Color.red;
		foreach(var Target in Targets) {
			if(Target) Target.SetState(this, NewOn);
		}
	}
}
