using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class App : MonoBehaviourSingleton<App>
{
	[System.NonSerialized]
	public bool m_appPaused = false;

    [System.NonSerialized]
    public int m_busy = 0;

    protected override void Awake () 
	{
        base.Awake();

        GFRandom.Init();

        Config.instance.OnInit();

        ApplyFpsQuality();

        // to fix storage on iOS
        // otherway getting unity ios Attempting to JIT compile method .ctor () while running with aot only
        if (Application.platform == RuntimePlatform.IPhonePlayer)
		{
			System.Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
		}

//      InputManager.instance.OnInit();

//		Purchaser.instance.Init();
//		Purchaser.instance.Connect();

		AudioManager.instance.OnInit();
//		FXManager.instance.OnInit();

//		UIManager.instance.OnInit();

		Game.instance.OnInit();
	}

    public void ApplyFpsQuality()
    {
        switch (Config.instance.m_data.m_fps)
        {
            case FPSQuality.Low:
                Application.targetFrameRate = 30;
                break;
            case FPSQuality.High:
                Application.targetFrameRate = 60;
                break;
            case FPSQuality.Max:
                Application.targetFrameRate = 1000;
                break;
        }
        QualitySettings.vSyncCount = Config.instance.m_data.m_vsynch ? 1 : 0;
        QualitySettings.antiAliasing = 0;
    }

    void Start()
    {
        Game.instance.OnLateInit();
    }

	void OnApplicationPause(bool paused)
	{
		m_appPaused = paused;

        /* @NOTE: raiders doesn't need save when game paused
		if(paused)
		{
			Config.instance.SaveConfig();
			SaveGame(0);
		}
        */
	}

    void OnApplicationQuit()
    {
        Game.instance.OnDone();

        SceneAccess.Clear();
    }


    void SaveGame(int slot)
	{
		// Storage.Save(gamedata, slot);
	}
	
	void Update()
	{
        if (m_appPaused || m_busy > 0)
			return;

        // InputManager.instance.OnUpdate();

        // UIManager.instance.OnUpdate();

        Game.instance.OnUpdate();

        // blends and applies camera
        // CameraManager.instance.OnUpdate();

        // FXManager.instance.OnUpdate();
        AudioManager.instance.OnUpdate();
    }

    public void Command(string cmd)
	{
        // some low level commands?
	}
/*
	public void OnBeginTouch(GameTouch touch)
	{
		// Pages
//@		bool inputPassThrough = UIManager.instance.OnBeginTouch(touch);

		// Game state
//@		if(inputPassThrough)
//@		{
//@			if(!Fade.instance.IsFaded())
//@			{
//@             // pass to each module
//@				module.OnBeginTouch(touch);
//@			}
//@		}
	}
*/
}
