using UnityEngine;
using System.Collections.Generic;

namespace Assets._game.scripts
{
    public class GameStatistics
    {
        public List<GamePlayerStatistics> players;
        private int currentRank;
        private float startTime;

        public GameStatistics(Game game)
        {
            players = new List<GamePlayerStatistics>();

            foreach(Player player in game.Players)
            {
                Debug.Log("player.playerID" + player.PlayerId);

                player.PlayerHit += PlayerHit;
                player.PlayerKilled += PlayerKilled;
                player.PlayerFire += PlayerFire;

                players.Add(new GamePlayerStatistics(player.PlayerId, player.Data));
            }

            currentRank = game.Players.Count;

            startTime = game.StartTime;
        }

        public void OnGameOver(Player winner)
        {
            GamePlayerStatistics winnerPlayerStats = GetPlayerStats(winner != null ? winner.PlayerId : 0);
            Debug.Log("Setting WINNER RANK:" + currentRank + "(" + winnerPlayerStats.playerId + ")");
            winnerPlayerStats.rank = currentRank;
            winnerPlayerStats.timeAlive = Time.timeSinceLevelLoad - startTime;
        }

        private void PlayerFire(Player firePlayer)
        {
            GamePlayerStatistics firePlayerStats = GetPlayerStats(firePlayer.PlayerId);
            firePlayerStats.projectilesFired ++;
        }

        private void PlayerKilled(Player deadPlayer, GameEntity killer)
        {
            GamePlayerStatistics deadPlayerStats = GetPlayerStats(deadPlayer.PlayerId);

            Debug.Log("Setting KILLED RANK:" + currentRank + "(" + deadPlayerStats.playerId + ")");

            deadPlayerStats.rank = currentRank;
            currentRank--;

            deadPlayerStats.timeAlive = deadPlayer.TimeKilled - startTime;
        
            PlayerController killerController = killer as PlayerController;
            if(killerController)
            {
                GamePlayerStatistics killerPlayerStats = GetPlayerStats(killerController.PlayerId);

                killerPlayerStats.kills++;

                Debug.LogFormat("Player {0} has been killed by {1}", deadPlayer.PlayerId, killerController.PlayerId);
            }
            else 
            {
                Debug.LogFormat("Player {0} has been killed by area {1}", deadPlayer.PlayerId, killer.Name);
            }
        }

        private void PlayerHit(Player hitPlayer, int amount, GameEntity source)
        {
            GamePlayerStatistics hitPlayerStats = GetPlayerStats(hitPlayer.PlayerId);
            hitPlayerStats.damageTaken += amount;

            PlayerController sourcePlayerController = source as PlayerController;
            if(sourcePlayerController)
            {
                GamePlayerStatistics sourcePlayerStats = GetPlayerStats(sourcePlayerController.PlayerId);
                sourcePlayerStats.damageGiven += amount;
                sourcePlayerStats.projectilesHits++;
            }

            Debug.LogFormat("Player {0} has been hit for {1} damage by {3}. Remaining: {2}", hitPlayer.PlayerId, amount, hitPlayer.HitPoints, source.Name);
        }

        private GamePlayerStatistics GetPlayerStats(int playerId)
        {
            foreach(GamePlayerStatistics player in players)
            {
                if(player.playerId == playerId)
                    return player;
            }
            return null;
        }
    }

    public class GamePlayerStatistics
    {
        public GamePlayerStatistics(int playerId, PlayerData data)
        {
            this.playerData = data;
            this.playerId = playerId;
        }

        public PlayerData playerData;
        public int playerId;

        public int rank;

        public int projectilesFired;
        public int projectilesHits;

        public float damageTaken;
        public float damageGiven;

        public int kills; 

        public float timeAlive;
    }
}
