// Copyright (c) Microsoft Corporation. 
// Licensed under the MIT license. 

using Simplygon.Unity;
using SimplygonUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;

namespace SimplygonUI
{
    public class SimplygonMaterialCasterDropdownFactory
    {
        ISimplygonPipelineDropdownCallback Callback;
        public SimplygonMaterialCasterDropdownFactory(ISimplygonPipelineDropdownCallback callback)
        {
            Callback = callback;
        }

        public void BuildItems(GenericMenu menu)
        {           
            foreach (var materialCaster in SimplygonPipelineDatabase.MaterialCasterTemplates)
            {
                menu.AddItem(new GUIContent($"Advanced/{materialCaster.MenuPath}"), false, OnItemSelected, materialCaster);
            }

            if (Callback.IsGameObjectsSelected())
            {
                menu.AddItem(new GUIContent("Automatic"), false, OnItemSelected, null);
            }
            else
            {
                menu.AddDisabledItem(new GUIContent("Automatic"));
            }

            menu.AddSeparator("");
            menu.AddItem(new GUIContent("Remove all", "Remove all added material casters"), false, OnRemoveAllSelected, null);
        }

        void OnItemSelected(object item)
        {
            Callback.NewMaterialCasterSelected(item as SimplygonMaterialCaster);
        }

        void OnRemoveAllSelected(object item)
        {
            Callback.RemoveAllMaterialCasters();
        }
    }
}
