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

[CreateAssetMenu(fileName = "Palette", menuName = "Arugula/SurfaceSpawner Palette")]
public class SurfaceSpawnerPalette : ScriptableObject
{
    [System.Serializable]
    public class Spawnable
    {
        public string name;
        public GameObject prefab;
        public float minOffset;
        public float maxOffset;
        public Vector3 minRotation;
        public Vector3 maxRotation;
        public float minScale = 1;
        public float maxScale = 1;
        [TextArea]
        public string description;

        public string Name
        {
            get
            {
                if (name == null || name == "")
                {
                    if (prefab != null)
                        return prefab.name;

                    return "Unknown";
                }


                return name;

            }
        }


        public GameObject Spawn(Vector3 position, Vector3 normal)
        {
            if (prefab == null || normal.sqrMagnitude == 0)
                return null;

            Vector3 pos = position + (normal * Random.Range(minOffset, maxOffset));
            Quaternion rot = Quaternion.LookRotation(normal);
            rot = Quaternion.LookRotation(rot * Vector3.up, normal);

            rot = rot * Quaternion.Euler(
                Random.Range(minRotation.x, maxRotation.x),
                Random.Range(minRotation.y, maxRotation.y),
                Random.Range(minRotation.z, maxRotation.z)
                );

            var go = Instantiate(prefab, pos, rot);
            go.transform.localScale *= Random.Range(minScale, maxScale);

            return go;
        }
    }

    public Spawnable[] spawnables;

    public Spawnable this[int i]
    {
        get
        {
            if (i < 0 || i >= spawnables.Length)
                return null;

            return spawnables[i];
        }
    }
}

