using UnityEngine;
using RothenburgAR.PointOfInterest;
using System.Collections;

namespace RothenburgAR.UI
{
    public class ARViewBehaviour : BaseViewBehaviour
    {
        public DetailsPanelBehaviour DetailsPanel;
        public ProjectionEffectBehaviour ProjectionEffect;

        public void DisplayPoiDetails(PoiBehaviour poi, bool animated = true)
        {
            // if POI has no Details to display, do nothing
            if (!PoiDataManager.Instance.HasPoiData(poi.ReferencedID)) return;

            // if the POIs Details are already being displayed, do nothing 
            // this can happen when vuforia re-enables the selected POI when the tracker is lost and then found 
            //  again and the user selects the still invisible POI
            if (ProjectionEffect.gameObject.activeSelf && poi.gameObject == ProjectionEffect.SelectedPOI)
                return;

            HidePoiDetails();
            PoiData data = PoiDataManager.Instance.GetPoiData(poi.ReferencedID);

            DetailsPanel.gameObject.SetActive(true);
            DetailsPanel.SetText(data.Description);
            DetailsPanel.SelectedPoi = data;

            DetailsPanel.DisplayModelButton(data);

            if (animated)
            {
                DetailsPanel.UpdateOpeningAnimation(poi.transform.position);
            }

            poi.SetActive(true);

            ProjectionEffect.SelectedPOI = poi.gameObject;
            ProjectionEffect.SetActive(true);
        }
        
        public void HidePoiDetails()
        {
            DetailsPanel.gameObject.SetActive(false);
            ProjectionEffect.SetActive(false);

            if (ProjectionEffect.SelectedPOI != null)
            {
                var poi = ProjectionEffect.SelectedPOI.GetComponent<PoiBehaviour>();
                poi.SetActive(false);
            }
        }

        public void ClosePoiDetails()
        {
            HidePoiDetails();
        }

        public void SendCrosshairRaycast()
        {
            InputManager.Instance.ActivateCrosshairs();
        }
    }
}