using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Equipment), true)]
public class EquipmentEditor : UnityEditor.Editor {

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        if(GUILayout.Button("Update noise cells"))
        {
            UpdateNoiseCells((Equipment)target);
        }
    }

    public void UpdateNoiseCells(Equipment equipment)
    {
        while(equipment.noiseCells.transform.childCount > 0)
        {
            DestroyImmediate(equipment.noiseCells.transform.GetChild(0).gameObject);
        }

        GameObject noiseCellPrefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Prefabs/NoiseCell.prefab");

        WaveSource waveSource = equipment.GetComponent<WaveSource>();
        foreach (Wave wave in waveSource.waves)
        {
            RelativeZone zone = wave.zone;
            for(int i=0; i<zone.width; i++)
            {
                for(int j=0; j<zone.height; j++)
                {
                    int x = i + zone.x;
                    int y = j + zone.y;
                    GameObject noiseCell = Instantiate(noiseCellPrefab);
                    noiseCell.name += " (" + x + ", " + y + ")";
                    noiseCell.transform.SetParent(equipment.noiseCells.transform, false);
                    noiseCell.transform.localPosition = new Vector3(x, y);
                }
            }
        }
    }

}
