//============= 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(NamedTextMeshPro))]
public class NamedTextMeshProInspector : ComponentHandlerInspector {
}

[System.Serializable, RequireComponent(typeof(ComponentController))]
#endif
public class NamedTextMeshPro : ComponentHandler {
#if TMPRO_AVAILABLE
    public override string TypeLabel () {
        return "TextMeshPro";
    }

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

    public override void UpdateComponent() {
        base.UpdateComponent();
        textMeshProUGuiComponent = GetComponent<TMPro.TextMeshProUGUI>();
        textMeshProComponent = GetComponent<TMPro.TextMeshPro>();
    }

    public TMPro.TextMeshProUGUI textMeshProUGuiComponent;
    public TMPro.TextMeshPro textMeshProComponent;

    public string objName = "TmpXY";

    public override string[][] helpText {
        get {
            return new string[][] {
                new string[] {"Show", null, $"BTC.Show(\"{roomId}\", \"{gameObject.name}\")"},
                new string[] {"Hide", null, $"BTC.Hide(\"{roomId}\", \"{gameObject.name}\")"},
                new string[] {"FadeIn", null, $"BTC.FadeIn(\"{roomId}\", \"{gameObject.name}\")"},
                new string[] {"FadeOut", null, $"BTC.FadeOut(\"{roomId}\", \"{gameObject.name}\")"},
                new string[] {"Set Text", "Set Text Content", $"BTC.Set(\"{roomId}\", \"{gameObject.name}\", \"text\", \"Text Content\")"},
            };
        }
    }
    
    public override void Start() {
    }

    public override void Set(NodeState nodeState, string key, string value) {
        if (nodeState == NodeState.FirstRun) {
            if (key == "text") {
                if (textMeshProUGuiComponent!=null)
                    textMeshProUGuiComponent.text = value;
                
                if (textMeshProComponent!=null)
                    textMeshProComponent.text = value;
                Task.SetSucceeded();
                return;
            }
        }
        Task.SetFailed();
    }

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

    #region both values setting
    public override void SetAlpha(float alpha) {
        if (textMeshProUGuiComponent != null) {
            Color col = textMeshProUGuiComponent.color;
            col.a = 1 - alpha;
            textMeshProUGuiComponent.color = col;
        }
        if (textMeshProComponent != null) {
            Color col = textMeshProComponent.color;
            col.a = 1 - alpha;
            textMeshProComponent.color = col;
        }
    }

    public override float GetAlpha() {
        if (textMeshProUGuiComponent != null) {
            return 1-textMeshProUGuiComponent.color.a;
        }
        if (textMeshProComponent != null) {
            return 1-textMeshProComponent.color.a;
        }
        return 0;
    }
    #endregion
#else
        [Header("TextMesh Pro Support disabled: Window->MyBT->PreCompiler Definitions")]
        public string dummy;
#endif
}