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

namespace RothenburgAR.Exhibition
{
    public class ExhibitTitleBehaviour : MonoBehaviour
    {
        public MeshRenderer BackgroundMeshRenderer;
        public GameObject BackgroundGo;
        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 float BoxWidth
        {
            get { return this.GuiRect.rect.width; }
            set
            {
                UpdateRect(value, BoxHeight);
            }
        }

        public float BoxHeight
        {
            get { return this.GuiRect.rect.height; }
            set
            {
                UpdateRect(BoxWidth, value);
            }
        }

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

        public TextElement DataText
        {
            get; set;
        }

        public void UpdateRect(float width, float height)
        {
            this.GuiRect.sizeDelta = new Vector2(width, height);
            if (_descriptionPoiBehaviour != null)
            {
                BackgroundMeshRenderer.transform.localScale = new Vector3(width, height, 1.0f);
                _descriptionPoiBehaviour.transform.localPosition = new Vector3(-0.4f, 0f, -0.005f);
            }
            else
            {
                BackgroundMeshRenderer.transform.localScale = new Vector3(width, height, 1.0f);
            }
        }

        void Start()
        {
            CreateBorder();
        }

        public void SetDescription(TextElement newDescription)
        {
            if (_descriptionPoiBehaviour == null)
            {
                var push = 0.1f;
                BackgroundMeshRenderer.transform.localScale += new Vector3(push, 0f, 0f);

                // 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);
                }

                _descriptionPoiBehaviour.transform.localScale = new Vector3(0.85f, 0.85f, 0.85f);
                _descriptionPoiBehaviour.transform.SetParent(BackgroundMeshRenderer.transform);

                
                // Set margin of the text element
                var guiTextMargin = GuiText.margin;
                guiTextMargin.x = push;
                GuiText.margin = guiTextMargin;

                // Set position of the POI
                _descriptionPoiBehaviour.transform.localPosition = new Vector3(-0.5f + push, 0f, -0.005f);
                _descriptionPoiBehaviour.Exhibit = Exhibit;
                _descriptionPoiBehaviour.ReferencedID = "TITLE:" + Exhibit.ID;

                // 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;
        }

        private void CreateBorder()
        {
            var borderContainer = new GameObject("BorderContainer");
            borderContainer.transform.SetParent(BackgroundGo.transform, true);

            borderContainer.transform.localPosition = Vector3.zero;
            borderContainer.transform.rotation = Quaternion.identity;
            borderContainer.transform.localScale = Vector3.one;

            var outlineRenderer = borderContainer.AddComponent<LineRenderer>();
            outlineRenderer.useWorldSpace = false;
            outlineRenderer.startWidth = 0.01f;
            outlineRenderer.endWidth = 0.01f;

            var size = BackgroundGo.GetComponent<MeshFilter>().mesh.bounds.size;
           
            //produces weird seam at begin/end point, so we'll loop it ourselves by drawing back to topLeft and topRight manually
            outlineRenderer.loop = false;

            Vector3 topLeft = new Vector3(-size.x / 2, 0, -size.y / 2);
            Vector3 topRight = new Vector3(size.x / 2, 0, -size.y / 2);
            Vector3 bottomLeft = new Vector3(-size.x / 2, 0, size.y / 2);
            Vector3 bottomRight = new Vector3(size.x / 2, 0, size.y / 2);
            outlineRenderer.positionCount = 6;
            outlineRenderer.SetPosition(0, topLeft);
            outlineRenderer.SetPosition(1, topRight);
            outlineRenderer.SetPosition(2, bottomRight);
            outlineRenderer.SetPosition(3, bottomLeft);
            outlineRenderer.SetPosition(4, topLeft);
            outlineRenderer.SetPosition(5, topRight);

            outlineRenderer.material = UIColorSchemeManager.Instance.GetUIMaterial(UIMaterial.BaseOpaque);
            outlineRenderer.enabled = false;
        }
    }
}