using RothenburgAR.Common;
using RothenburgAR.Exhibition;
using RothenburgAR.PointOfInterest;
using RothenburgAR.UI;
using RothenburgAR.Updater;
using UnityEngine;
using Vuforia;

namespace RothenburgAR
{
    public class AppInitializerBehaviour : MonoBehaviour
    {
        public Camera UICamera;
        public Camera UIObjectCamera;

        // Use this for initialization
        void Start()
        {
#if !UNITY_EDITOR
            logFileHandler = new LogFileHandler();
#else
            Debug.Log("Found Unity Editor; Disabled logging into file.");
#endif
            //OBJLoader.defaultShader = Shader.Find("Standard (Specular setup)")
            OBJLoader.defaultShader = Shader.Find("Standard");
            OBJLoader.defaultMaterial = new Material(OBJLoader.defaultShader);

            LanguageManager.Instance.LoadLanguagesFromXml();
            UIColorSchemeManager.Instance.Initialize();

            VuforiaARController.Instance.RegisterVuforiaInitializedCallback(InitializeData);

            UIManager.Instance.Initialize();
            UIManager.Instance.UICamera = UICamera;
            UIManager.Instance.UIObjectCamera = UIObjectCamera;

            StartCoroutine(UIManager.Instance.InitStartView());

            InputManager.Instance.Initialize();
            var initDisplayManagerSingleton = DisplayManager.Instance;
            var initUpdaterSingleton = UpdateManager.Instance;
        }

        void InitializeData()
        {
            PoiDataManager.Instance.AddPoiDataSourcePath(PathHelper.POIPath);
            PoiDataManager.Instance.AddPoiPreloader(new PoiXmlPreloader());

            PoiDataManager.Instance.AddPoiDataSourcePath(PathHelper.ExhibitionPath);
            PoiDataManager.Instance.AddPoiPreloader(new PoiApiDataPreloader());

            PoiDataManager.Instance.LoadEverySourcePath();

            ExhibitionManager.Instance.AddExhibitionSourcePath(PathHelper.ExhibitionPath);
            ExhibitionManager.Instance.AddExhibitionPreloader(new ExhibitionXmlPreloader());
            ExhibitionManager.Instance.AddExhibitionPreloader(new ExhibitionApiDataPreloader());

            ExhibitionManager.Instance.LoadEverySourcePath(
                (int maxPaths, int currentPaths, int subMaxExhibitions, int subCurrentExhibition) =>
                {
                    Debug.Log("<color=green>Done " + subCurrentExhibition + " of " + subMaxExhibitions +
                              " Exhibitions of Path " + currentPaths + " of " + maxPaths + " paths.</color>");
                });
        }
    }
}