using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GenerateNewGameState : BaseGameState {

    public override void InitState()
    {
        base.InitState();

        var randomMapSize = Random.Range (50, 80);
        var randomObstacleAmount = (int)Random.Range(randomMapSize, randomMapSize * 1.5f);
        var randomDifficulty = Random.Range(0.0f, 1.0f);
        var zombiesAmount = 5 + (int)(randomMapSize/2 * randomDifficulty);

        this.gameController.DataStorage.CurrentGame = new CurrentGameData();
        this.gameController.DataStorage.CurrentGame.MapSize = (uint)randomMapSize;
        this.gameController.DataStorage.CurrentGame.Difficulty = randomDifficulty;
        this.gameController.DataStorage.CurrentGame.Zombies = (uint)zombiesAmount;

        this.gameController.MapController.GenerateMap(randomMapSize, randomObstacleAmount);
        this.gameController.MapController.PlaceGameplayObjects(zombiesAmount);

        var player = GameObject.FindObjectOfType<Player>();
        this.gameController.Player = player;
        player.PrepareCharacter();

        var portal = GameObject.FindObjectOfType<MapPortal>();
        this.gameController.MapPortal = portal;
        portal.PreparePortal(this.gameController);

        var antenna = GameObject.FindObjectOfType<AntennaHandler>();
        this.gameController.Antenna = antenna;
        antenna.Init(this.gameController);

        this.gameController.UIController.GameInfoView.ShowInfo(this.gameController.DataStorage.CurrentGame);
        this.gameController.UIController.GameInfoView.OnInfoClosed += OnInfoClosed;

        var prepareCamera = new PreprareGameCameraState();
        prepareCamera.focusPosition = this.gameController.MapController.MapInstance.Tiles[randomMapSize/2, randomMapSize/2].transform.position;
        this.gameController.CameraController.ChangeState(prepareCamera);

        this.gameController.UIController.BlackedView.HideView();
    }

    public override void DeinitState()
    {
        this.gameController.UIController.GameInfoView.OnInfoClosed -= OnInfoClosed;
        this.gameController.UIController.GameInfoView.HideView();

        base.DeinitState();
    }

    private void OnInfoClosed () {
        this.gameController.ChangeState(new GameLoopState());
    }
}
