//============= Copyright (c) Ludic GmbH, All rights reserved. ==============
//
// Purpose: Part of the My Behaviour Tree Controller Code
//
//=============================================================================

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using MyBT;

#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(NamedSkybox))]
public class NamedSkyboxInspector : ComponentHandlerInspector {
}
#endif

[System.Serializable]
public class SkyboxItem {
    public string skyboxLabel;
    public Texture2D skyboxTexture;
}

[System.Serializable]
public class NamedSkybox : ComponentHandler {
    public override string TypeLabel () {
        return "Skybox";
    }

    public override string ContentLabel() {
        UpdateComponent();
        return "";
    }

    public override void UpdateComponent() {
        base.UpdateComponent();
    }

    public List<SkyboxItem> skyboxes;
    public Material skyboxMaterial;
    public float rotation;
    public float exposure = 1.0f;

    public override string titleText {
        get {
            return "Set  \"TexName1\" \"TexName2\" , FadeIn/FadeOut between";
        }
    }

    public override string[][] helpText {
        get {
            return new string[][] {
                new string[] {"Set", "Set Panoramic Image", $"BTC.Set(\"{roomId}\", \"{gameObject.name}\", \"TextureName1\", \"TextureName2\")"},
                new string[] {"Show", "Show Texture 1", $"BTC.Show(\"{roomId}\", \"{gameObject.name}\")"},
                new string[] {"Hide", "Show Texture 2", $"BTC.Hide(\"{roomId}\", \"{gameObject.name}\")"},
                new string[] {"FadeIn", "Fade From Texture 1 to 2", $"BTC.FadeIn(\"{roomId}\", \"{gameObject.name}\")"},
                new string[] {"FadeOut", "Fade From Texture 2 to 1", $"BTC.FadeOut(\"{roomId}\", \"{gameObject.name}\")"},
            };
        }
    }

    public override void Start() {
        skyboxMaterial = new Material(Shader.Find("Skybox/Panoramic Blended Full"));
        RenderSettings.skybox = skyboxMaterial;
        skyboxMaterial.SetFloat("_Rotation", rotation);
        skyboxMaterial.SetFloat("_Exposure", exposure);
    }

    public override void Set(NodeState nodeState, string texName1, string texName2) {
        if ((nodeState == NodeState.FirstRun) || (nodeState == NodeState.Running)) {
            Texture2D tex1 = null;
            foreach (SkyboxItem skyboxItem in skyboxes) {
                if (skyboxItem.skyboxLabel == texName1) {
                    tex1 = skyboxItem.skyboxTexture;
                }
            }
            Texture2D tex2 = null;
            foreach (SkyboxItem skyboxItem in skyboxes) {
                if (skyboxItem.skyboxLabel == texName2) {
                    tex2 = skyboxItem.skyboxTexture;
                }
            }
            if ((tex1 != null) && (tex2 != null)) {
                RenderSettings.skybox.SetTexture("_MainTex", tex1);
                RenderSettings.skybox.SetTexture("_SecondaryTex", tex2);
                Task.SetSucceeded();
                return;
            } else {
                Debug.LogError($"NamedSkybox.Set: skybox with name {texName1} or {texName2} not available");
                Task.SetFailed();
            }
        }
    }

    public override void Update() {
        base.Update();
    }

    #region both values setting
    public override void SetAlpha(float alpha) {
        //Debug.Log($"NamedSkybox.SetAlpha {alpha}");
        skyboxMaterial.SetFloat("_Blend", alpha);
        DynamicGI.UpdateEnvironment();
    }

    public override float GetAlpha() {
        return skyboxMaterial.GetFloat("_Blend");
    }
    #endregion
}