using System;
using System.Collections.Generic;
using System.Diagnostics;
using Pixiview.Resources;
using Pixiview.UI.Theme;
using Pixiview.Utils;
using Xamarin.Forms;

namespace Pixiview
{
    public class App : Application
    {
        // public properties
        public static Theme CurrentTheme { get; private set; }
        public static PlatformCulture CurrentCulture { get; private set; }
        public static Dictionary<string, object> ExtraResources { get; private set; }

        private void InitResources(IEnvironmentService service)
        {
            var p = service.GetEnvironment();

            ExtraResources = new Dictionary<string, object>
            {
                { ThemeBase.IconLightFontFamily, p.IconLightFontFamily },
                { ThemeBase.IconRegularFontFamily, p.IconRegularFontFamily },
                { ThemeBase.IconSolidFontFamily, p.IconSolidFontFamily },

                { ThemeBase.IconLeft, p.IconLeft }
            };

            var theme = service.GetApplicationTheme();
            SetTheme(theme, true);
        }

        private void InitLanguage(IEnvironmentService service)
        {
            var ci = service.GetCurrentCultureInfo();
            service.SetCultureInfo(ci);
            CurrentCulture = new PlatformCulture(ci.Name.ToLower());
        }

        private void SetTheme(Theme theme, bool force = false)
        {
            if (force || theme != CurrentTheme)
            {
                CurrentTheme = theme;
            }
            else
            {
                return;
            }
            DebugPrint($"application theme: {theme}");
            ThemeBase themeInstance;
            if (theme == Theme.Dark)
            {
                themeInstance = DarkTheme.Instance;
            }
            else
            {
                themeInstance = LightTheme.Instance;
            }
            //DependencyService.Get<IEnvironmentService>().SetStatusBarColor
            var style = (StatusBarStyles)themeInstance[ThemeBase.StatusBarStyle];
            DependencyService.Get<IEnvironmentService>().SetStatusBarStyle(style);
            Resources = themeInstance;
        }

        protected override void OnStart()
        {
            var service = DependencyService.Get<IEnvironmentService>();
            InitLanguage(service);
            MainPage = new AppShell();

            InitResources(service);
        }

        protected override void OnSleep()
        {
        }

        protected override void OnResume()
        {
            var theme = DependencyService.Get<IEnvironmentService>().GetApplicationTheme();
            SetTheme(theme);
        }

        public static void DebugPrint(string message)
        {
            Debug.WriteLine("[{0:HH:mm:ss.ffff}] - {1}", DateTime.Now, message);
        }

        public static void DebugError(string category, string message)
        {
            Debug.Fail(string.Format("[{0:HH:mm:ss.ffff}] - {1} - {2}", DateTime.Now, category, message));
        }
    }
}
