//#define GAMECONTROLLER_DEBUG
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameController : StateController<BaseGameState> {

    [SerializeField]
    private DataStorage dataStorage;
    public DataStorage DataStorage { get { return this.dataStorage; } }

    [SerializeField]
    private UIController uiController;
    public UIController UIController { get { return this.uiController; } }

    [SerializeField]
    private CameraController cameraController;
    public CameraController CameraController { get { return this.cameraController; } }

    [SerializeField]
    private MapManager mapController;
    public MapManager MapController { get { return this.mapController; } }

    [SerializeField]
    private Player player;
    public Player Player { get { return this.player; } set { this.player = value; } }

    [SerializeField]
    private AntennaHandler antenna;
    public AntennaHandler Antenna { get { return this.antenna; } set { this.antenna = value; } }


    [SerializeField]
    private MapPortal mapPortal;
    public MapPortal MapPortal { get { return this.mapPortal; } set { this.mapPortal = value; } }


    [SerializeField]
    private GameEvents gameEvents;
    public GameEvents GameEvents { get { return this.gameEvents; } }

    protected override void PrepareStateForInit(BaseGameState state) {
        base.PrepareStateForInit(state);
        state.gameController = this;
    }

    private void Awake () {
        
    }

    private void Start () {
        this.Initialization();
    }

    private void Initialization () {
        
        this.dataStorage.PrepareData();
        this.dataStorage.AssignEvents(this.gameEvents);

        this.uiController.PrepareUI();

        this.cameraController.PrepareCamera(this);

        this.gameEvents.CallOnAppStarted();

        this.ChangeState(new GameMenuState());
    }

    private void OnApplicationFocus(bool hasFocus) {
#if GAMECONTROLLER_DEBUG
        Debug.Log("[Game Controller] Focus status: " + hasFocus + " after " + Time.time + " seconds");
#endif

        this.gameEvents.CallOnAppPaused(!hasFocus);
    }

    private void OnApplicationPause(bool pauseStatus) {
#if GAMECONTROLLER_DEBUG
        Debug.Log("[Game Controller] Pause status: " + pauseStatus + " after " + Time.time + " seconds");
#endif

        this.gameEvents.CallOnAppPaused(pauseStatus);
    }

    private void OnApplicationQuit() {
#if GAMECONTROLLER_DEBUG
        Debug.Log("[Game Controller] Application ending after " + Time.time + " seconds");
#endif

        this.gameEvents.CallOnAppExit();
    }
}
