using RothenburgAR.Common;
using UnityEngine;

namespace RothenburgAR.PointOfInterest
{
    public class PoiData
    {
        public string ID { get; set; }
        public string SourcePath { get; set; }
        public TextElement Description { get; set; }
        // Currently not used, but maybe relevant for future projects
        public TextElement Title { get; set; }
        // The cached model prefab
        private GameObject _modelPrefab;

        // Path to the model. Null if not set.
        public PoiModelDescription ModelDescription { get; set; }

        public bool HasModelDescription
        {
            get { return ModelDescription.ModelPath != null; }
        }

        public GameObject GetModelPrefab()
        {
            if (!HasModelDescription)
                return null;
            if (_modelPrefab != null)
                return _modelPrefab;
            return _modelPrefab = new PoiModelLoader().LoadModel(ModelDescription);
        }

        public GameObject InstantiateModelPrefab()
        {
            if (!HasModelDescription)
                return null;
            var modelPrefab = GetModelPrefab();
            var gameObject = UnityEngine.Object.Instantiate(modelPrefab);
            gameObject.transform.localRotation = Quaternion.identity;
            gameObject.transform.localScale = ModelDescription.Scale;

            var modelGO = gameObject.transform.GetChild(0);
            modelGO.transform.eulerAngles = ModelDescription.Rotation;
            modelGO.gameObject.layer =  LayerMask.NameToLayer("UIModel");

            return gameObject;
        }
    }
}