using RothenburgAR.Common;
using RothenburgAR.PointOfInterest;
using RothenburgAR.UI;
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace RothenburgAR.Exhibition
{
    public class ExhibitTitleBehaviour : MonoBehaviour
    {
        public GameObject Background;
        public GameObject PoiSpacer;
        public ExhibitBehaviour Exhibit;

        private PoiBehaviour _descriptionPoiBehaviour;
        private PoiData _descriptionPoiData;

        private TextMeshPro GuiText
        {
            get { return this.GetComponentInChildren<TextMeshPro>(); }
        }

        private RectTransform GuiRect
        {
            get { return this.GetComponentInChildren<RectTransform>(); }
        }

        public float FontSize
        {
            get { return this.GuiText.fontSize; }
            set { this.GuiText.fontSize = value; }
        }

        public string DisplayText
        {
            get { return this.GuiText.text; }
            set
            {
                this.GuiText.text = value;
                this.RecalculateSize();
            }
        }

        public TextElement DataText
        {
            get; set;
        }

        void OnEnable()
        {
            this.RecalculateSize();
        }

        private void RecalculateSize()
        {
            GuiText.GetComponent<LayoutElement>().enabled = false;
            GuiText.GetComponent<LayoutElement>().enabled = true;
        }

        public void SetDescription(TextElement newDescription)
        {
            if (_descriptionPoiBehaviour == null)
            {
                // Create the POI
                var poiElementPrefab = Resources.Load("Prefabs/POI", typeof(PoiBehaviour));
                _descriptionPoiBehaviour = Instantiate(poiElementPrefab) as PoiBehaviour;

                if (_descriptionPoiBehaviour == null) return;

                _descriptionPoiBehaviour.IsTitlePoi = true;
                var spriteRendererArray = _descriptionPoiBehaviour.gameObject.GetComponentsInChildren<SpriteRenderer>();
                foreach (var spriteRender in spriteRendererArray)
                {
                    spriteRender.material = UIColorSchemeManager.Instance.GetUIMaterial(UIMaterial.BaseOpaque);
                    spriteRender.sortingOrder = 1;
                }

                // Set data reference
                _descriptionPoiBehaviour.Exhibit = Exhibit;
                _descriptionPoiBehaviour.ReferencedID = "TITLE:" + Exhibit.ID;

                // Set position of the POI
                _descriptionPoiBehaviour.transform.localScale = new Vector3(0.85f, 0.85f, 0.85f);
                _descriptionPoiBehaviour.transform.SetParent(PoiSpacer.transform);
                _descriptionPoiBehaviour.transform.localPosition = Vector3.zero;

                // Set POI Rotation
                var transformLocalRotation = _descriptionPoiBehaviour.transform.localRotation;
                transformLocalRotation.x = 0f;
                _descriptionPoiBehaviour.transform.localRotation = transformLocalRotation;
            }

            if (_descriptionPoiData == null)
            {
                _descriptionPoiData = new PoiData { ID = "TITLE:" + Exhibit.ID };
                PoiDataManager.Instance.AddPoiData(_descriptionPoiData);

            }
            _descriptionPoiData.Description = newDescription;
        }
    }
}