using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Networking.Server;

public abstract class GameMode : ScriptableObject
{

    [SerializeField]
    [Tooltip("Scene to load which contains Gamemode UI")]
    private string GameModeScene;

    [SerializeField]
    [Tooltip("Round Times are acessed from the top down")]
    private List<float> RoundTimes;

    public Action GameStartEvent;
    public Action InputStartEvent;

    public Action RoundStartEvent;
    public Action AllPlayersMovedEvent;
    public Action RoundEndEvent;
    public Action OnGameOverEvent;

    private List<float> gameTimes;

    /// <summary>
    /// Called once before any players have spawned
    /// </summary>
    protected virtual void OnPreGameStart() { }

    /// <summary>
    /// Called once after players have spawned
    /// </summary>
    /// <param name="allPlayers">All players in game</param>
    protected virtual void OnGameStart(PlayerData[] allPlayers) { }

    /// <summary>
    /// Called every time players are moved to the input scene
    /// </summary>
    /// <param name="allPlayers">All players in game</param>
    protected virtual void OnInputStart(PlayerData[] allPlayers) { }

    /// <summary>
    /// Called for each player when we recieve their input
    /// </summary>
    /// <param name="player">Player who's input we recieved</param>
    protected virtual void OnRecievedPlayerInput(PlayerData player) { }

    /// <summary>
    /// Called after we have recieved all player input
    /// </summary>
    /// <param name="allPlayers">All players in game</param>
    protected virtual void OnInputEnd(PlayerData[] allPlayers) { }

    /// <summary>
    /// Called at the beginning of a new Round
    /// </summary>
    protected virtual void OnRoundStart(PlayerData[] allPlayers) { }

    /// <summary>
    /// Called for each player after they finished one move. Even if they didn't move!
    /// </summary>
    /// <param name="character">Character which moved</param>
    /// <param name="client">Client which controls character</param>
    /// <param name="currentBlock">Block which the character finished on</param>
    protected virtual void OnPlayerMoved(Character character, ClientData client, Block currentBlock) { }


    protected virtual void OnPlayerKilled(Character character, ClientData client) { }

    /// <summary>
    /// Called once after every player has finished one move
    /// </summary>
    /// <param name="allCharacters">List of all player Info</param>
    protected virtual void OnAllPlayersMoved(PlayerData[] allPlayers) { }

    /// <summary>
    /// Called after all environmentblocks have taken their turn
    /// </summary>
    /// <param name="allPlayers">All players in scene</param>
    protected virtual void OnEnvironmentTurn(PlayerData[] allPlayers) { }

    /// <summary>
    /// Called after one round has finished
    /// </summary>
    /// <param name="allPlayers">all players in game</param>
    protected virtual void OnRoundEnd(PlayerData[] allPlayers) { }

    /// <summary>
    /// Called once at end of game
    /// </summary>
    /// <param name="allPlayers">All players in game</param>
    protected virtual void OnGameOver(PlayerData[] allPlayers) { }

    /// <summary>
    /// Called to check if victory condition has been met
    /// </summary>
    /// <param name="allPlayers">All players in game</param>
    /// <returns>returns true if victory condition has been met</returns>
    public abstract bool isGameOver(PlayerData[] allPlayers);

    
    public float GetRoundTime()
    {
        float retVal = gameTimes[0];
        if (gameTimes.Count > 1)
            gameTimes.RemoveAt(0);
        return retVal;
    }

    #region Public functions
    public void PreGameStart()
    {
        OnPreGameStart();
    }

    public void GameStart(PlayerData[] allPlayers)
    {
        gameTimes = new List<float>(RoundTimes);

        if (!String.IsNullOrEmpty(GameModeScene))
            UnityEngine.SceneManagement.SceneManager.LoadScene(GameModeScene, UnityEngine.SceneManagement.LoadSceneMode.Additive);

        for (int i = 0; i < allPlayers.Length; i++)
        {
            allPlayers[i].client.Lives = 3;
        }
        OnGameStart(allPlayers);
        GameStartEvent?.Invoke();
    }

    public void InputStart(PlayerData[] AllPlayers)
    {
        OnInputStart(AllPlayers);
        InputStartEvent?.Invoke();
    }

    public void RecievedPlayerInput(PlayerData player)
    {
        OnRecievedPlayerInput(player);
    }

    public void InputEnd(PlayerData[] AllPlayers)
    {

    }

    public abstract PlayerData[] getPlayerOrder(PlayerData[] AllPlayers);

    public void RoundStart(PlayerData[] allPlayers)
    {
        OnRoundStart(allPlayers);
        RoundStartEvent?.Invoke();
    }

    public void PlayerMoved(PlayerData player)
    {
        OnPlayerMoved(player.character, player.client, player.character.CurrentBlock);
    }

    public void PlayerKilled(PlayerData player)
    {
        OnPlayerKilled(player.character, player.client);
    }

    public void AllPlayersMoved(PlayerData[] allPlayers)
    {
        OnAllPlayersMoved(allPlayers);
        AllPlayersMovedEvent?.Invoke();
    }

    public void EnvironmentTurn(PlayerData[] allPlayers)
    {
        OnEnvironmentTurn(allPlayers);
    }

    public void RoundEnd(PlayerData[] allPlayers)
    {
        OnRoundEnd(allPlayers);
        RoundEndEvent?.Invoke();
    }

    public void GameEnd(PlayerData[] allPlayers)
    {
        OnGameOver(allPlayers);
        OnGameOverEvent?.Invoke();
    }
} 
#endregion Public Functions



/* --- Calls we need ---
 * 
 * PreGameSetup(); - //Called once before the game actually starts
 * GameStart(PlayerData[] AllPlayers); //Called once after players have Spawned
 * 
 * OnInputStart(PlayerData[] AllPlayers) //Called when players enter input mode
 * OnRecievedPlayerInput(PlayerData Player) //Called when a specific player has sent their input
 * OnInputEnd(PlayerData[] AllPlayers) //Called when player input mode has finished
 * 
 * OnRoundStart(PlayerData[] AllPlayers) //Just before Players start moving
 * 
 *  ------- Start Round Loop -------
 * 
 * OnPlayerMoved(PlayerData player) //Called after each player moves
 * IsGameOver(PlayerData[] AllPlayers) //Called after each player moves
 * 
 * AllPlayersMoved(PlayerData[] AllPlayers) //Called after all players have moved
 * EnvironmentTurn() //Also called after all players have moved but should be kept specialised to environment stuff
 * IsGameOver(PlayerData[] AllPlayers) //We need to check if the environment terminated the game
 * 
 * --------- End Game Loop ---------
 * 
 * OnRoundEnd(PlayerData[] AllPlayers) //Called after movements are made
 * IsGameOver(PlayerData[] AllPlayers) //This needs to be checked again
 * 
 * GameEnd(PlayerData[] AllPlayers)
 * 
 * 
 * 
 * 
 * 
 */
