/*
© 2017 Altavant Technologies Inc.
Web: http://www.altavant.com
*/

namespace Altavant.ItemList
{
	using Android.App;
	using Android.Views;
	using Android.Content.PM;
	using Fusion;
	using Fusion.Core.Data;
	using Fusion.UI;
	using Global;
	using UI.Pages;
	using Wrappers = Fusion.Wrappers;
	using Aero = Fusion.UI.Aero;

	[Activity(
		MainLauncher = true,
		HardwareAccelerated = true,
		LaunchMode = LaunchMode.SingleInstance,
		WindowSoftInputMode = SoftInput.StateHidden | SoftInput.AdjustResize,
		ConfigurationChanges = ConfigChanges.KeyboardHidden | ConfigChanges.Orientation | ConfigChanges.ScreenSize | ConfigChanges.ScreenLayout
	)]

	public class MainActivity : Wrappers.Application
	{
		private bool _active;
		
		protected override void AppPrepare()
		{
			MainContext.Init();

			Window.SetFlags(WindowManagerFlags.HardwareAccelerated, WindowManagerFlags.HardwareAccelerated);

			VolumeControlStream = Android.Media.Stream.Music;
		}

		protected override void Dispose(bool disposing)
		{
			base.Dispose(disposing);
		}

		protected override void AppBuild()
		{
			FusionContext.Registry.AddNumber("StatusBarHeight", new FloatNumber(_statusBarHeight));
			FusionContext.Registry.AddNumber("ActionBarHeight", new FloatNumber(_actionBarHeight));
			FusionContext.Registry.AddNumber("TitleBarHeight", new FloatNumber(Defs.TitleBarHeight));

			AppResources.Build();
		}

		protected override void AppReady(IWindow window)
		{
			_active = true;

			MainContext.Model.Init();

			Aero.PanelLayer layer = new Aero.PanelLayer(new MainPage());

			MainContext.Root.Attach(window);

			MainContext.Root.Content.Push(layer, true);
		}

		protected override void AppActivate()
		{
			ActivateApp();
		}

		protected override void AppDeactivate()
		{
			DeactivateApp();
		}

		protected override void AppForeground()
		{
			ActivateApp();
		}

		protected override void AppBackground()
		{
			DeactivateApp();
		}

		private void ActivateApp()
		{
			if (_active)
				return;

			_active = true;
			
			MainContext.Root.ApplyOrientation();
		}

		private void DeactivateApp()
		{
			if (!_active)
				return;

			_active = false;
		}

		protected override void AppMemoryWarning()
		{
				
		}

		protected override void AppRelease()
		{
			MainContext.Release();
		}
	}
}