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

namespace Jovian.Utilities {

    public class CustomRenderQueueMaterialList : MonoBehaviour {

        [System.Serializable]
        public class MaterialRenderQueue {
            public Material material;
            public int renderQueue;

            public int storedRenderQueue;
        }

        public bool updateMaterialsInEditor;
        [SerializeField]
        private MaterialRenderQueue[] materialRenderQueueList;

        private void Awake() {
#if UNITY_EDITOR
            if(updateMaterialsInEditor) {
                Debug.LogWarning("Updating Materials will cause asset files to change. Please review your change log to ensure only valid changes are submitted.");
            }
            else {
                return;
            }
#endif

            foreach(var materialRenderQueue in materialRenderQueueList) {
                if(materialRenderQueue.material) {
                    materialRenderQueue.storedRenderQueue = materialRenderQueue.material.renderQueue; // store the materials original render queue
                    materialRenderQueue.material.renderQueue = materialRenderQueue.renderQueue; // overwrite the render queue
                }
            }
        }

        private void OnDestroy() {
#if UNITY_EDITOR
            if(updateMaterialsInEditor) {
                Debug.LogWarning("Updating Materials will cause asset files to change. Please review your change log to ensure only valid changes are submitted.");
            }
            else {
                return;
            }
#endif
            foreach(var materialRenderQueue in materialRenderQueueList) {
                if(materialRenderQueue.material) {
                    materialRenderQueue.material.renderQueue = materialRenderQueue.storedRenderQueue;
                }
            }
        }

        public bool DoesMaterialExistInList(Material material) {
            foreach(var materialRenderQueue in materialRenderQueueList) {
                if(materialRenderQueue.material == material) {
                    return true;
                }
            }
            return false;
        }
    }
}