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

namespace NBuilding
{
    /// <summary>
    /// Class <c>BuildingManager</c> contains functions to sum up values from buidlings at star system.
    /// </summary>
    public class BuildingManager
    {
        Dictionary<int, List<IBuilding>> doPlayerBuildings = new Dictionary<int, List<IBuilding>>();

        /// <summary>
        /// Function <c>GetAllValues</c> sums up the running cost of all buildings at star system of player.
        /// <param><c>iId</c>  is the id of a player.</param>
        /// <returns>Returns <c>all values</c> of all buildings at star system of player.</returns>
        /// More efficient because it iterates only once through list instead of 6 times.
        /// </summary>
        public BuildingValues GetAllValues(int iId)
        {
            BuildingValues oValues = new BuildingValues();
            List<IBuilding> loBuildings = doPlayerBuildings[iId];
            foreach (var oBuilding in loBuildings)
            {
                oValues.RunningCost += oBuilding.GetRunningCost();
                oValues.ScienceCapacity += oBuilding.GetScienceCapacity();
                oValues.FoodCapacity += oBuilding.GetFoodCapacity();
                oValues.IndustrialCapacity += oBuilding.GetIndustrialCapacity();
                oValues.MoneyCapacity += oBuilding.GetMoneyCapacity();
                oValues.MilitaryPower += oBuilding.GetMilitaryPower();
            }
            return oValues;
        }

        /// <summary>
        /// Function <c>GetRunningCost</c> sums up the running cost of all buildings at star system of player.
        /// <param><c>iId</c>  is the id of a player.</param>
        /// <returns>Returns <c>running cost</c> of all buildings at star system of player.</returns>
        /// </summary>
        public int GetRunningCost(int iId)
        {
            int iRunningCost = 0;
            List<IBuilding> loBuildings = doPlayerBuildings[iId];
            foreach (var oBuilding in loBuildings)
            {
                iRunningCost += oBuilding.GetRunningCost();
            }
            return iRunningCost;
        }

        /// <summary>
        /// Function <c>GetScienceCapacity</c> sums up the science capacity of all buildings at star system of player.
        /// <param><c>iId</c>  is the id of a player.</param>
        /// <returns>Returns <c>science capacity</c> of all buildings at star system of player.</returns>
        /// </summary>
        public int GetScienceCapacity(int iId)
        {
            int iScienceCapacity = 0;
            List<IBuilding> loBuildings = doPlayerBuildings[iId];
            foreach (var oBuilding in loBuildings)
            {
                iScienceCapacity += oBuilding.GetScienceCapacity();
            }
            return iScienceCapacity;
        }

        /// <summary>
        /// Function <c>GetFoodCapacity</c> sums up the food capacity of all buildings at star system of player.
        /// <param><c>iId</c>  is the id of a player.</param>
        /// <returns>Returns <c>food capacity</c> of all buildings at star system of player.</returns>
        /// </summary>
        public int GetFoodCapacity(int iId)
        {
            int iFoodCapacity = 0;
            List<IBuilding> loBuildings = doPlayerBuildings[iId];
            foreach (var oBuilding in loBuildings)
            {
                iFoodCapacity += oBuilding.GetFoodCapacity();
            }
            return iFoodCapacity;
        }

        /// <summary>
        /// Function <c>GetIndustrialCapacity</c> sums up the industrial capacity of all buildings at star system of player.
        /// <param><c>iId</c>  is the id of a player.</param>
        /// <returns>Returns <c>industrial capacity</c> of all buildings at star system of player.</returns>
        /// </summary>
        public int GetIndustrialCapacity(int iId)
        {
            int iIndustrialCapacity = 0;
            List<IBuilding> loBuildings = doPlayerBuildings[iId];
            foreach (var oBuilding in loBuildings)
            {
                iIndustrialCapacity += oBuilding.GetIndustrialCapacity();
            }
            return iIndustrialCapacity;
        }

        /// <summary>
        /// Function <c>GetMoneyCapacity</c> sums up the money capacity of all buildings at star system of player.
        /// <param><c>iId</c>  is the id of a player.</param>
        /// <returns>Returns <c>money capacity</c> of all buildings at star system of player.</returns>
        /// </summary>
        public int GetMoneyCapacity(int iId)
        {
            int iMoneyCapacity = 0;
            List<IBuilding> loBuildings = doPlayerBuildings[iId];
            foreach (var oBuilding in loBuildings)
            {
                iMoneyCapacity += oBuilding.GetMoneyCapacity();
            }
            return iMoneyCapacity;
        }

        /// <summary>
        /// Function <c>GetMilitaryCapacity</c> sums up the military capacity of all buildings at star system of player.
        /// <param><c>iId</c>  is the id of a player.</param>
        /// <returns>Returns <c>military capacity</c> of all buildings at star system of player.</returns>
        /// </summary>
        public int GetMilitaryPower(int iId)
        {
            int iMilitaryPower = 0;
            List<IBuilding> loBuildings = doPlayerBuildings[iId];
            foreach (var oBuilding in loBuildings)
            {
                iMilitaryPower += oBuilding.GetMilitaryPower();
            }
            return iMilitaryPower;
        }

        /// <summary>
        /// Function <c>BuildNewBuilding</c> checks if building already exists, if not creates a new building and adds it to list.
        /// <param><c>iPlayerId</c>  is the id of a player.</param>
        /// <param><c>eBuildingId</c>  is the id of a building.</param>
        /// </summary>
        public void BuildNewBuilding(int iPlayerId, ABuilding.BuildingID eBuildingId)
        {
            List<IBuilding> loBuildings = doPlayerBuildings[iPlayerId];
            foreach (var oBuilding in loBuildings)
            {
                if (oBuilding.GetId() == eBuildingId)
                {
                    // TODO throw exception
                }
            }
            loBuildings.Add(BuildingBuilder.Build(eBuildingId));
        }

        /// <summary>
        /// Function <c>DestroyBuilding</c> checks if building exists, if it exists, deletes it from list, if not, returns value of Remove(null).
        /// <param><c>iPlayerId</c>  is the id of a player.</param>
        /// <param><c>eBuildingId</c>  is the id of a building.</param>
        /// </summary>
        public void DestroyBuilding(int iPlayerId, ABuilding.BuildingID eBuildingId)
        {
            List<IBuilding> loBuildings = doPlayerBuildings[iPlayerId];
            IBuilding building = null;
            foreach (var oBuilding in loBuildings)
            {
                if (oBuilding.GetId() == eBuildingId)
                {
                    building = oBuilding;
                }
            }
            //TODO what does Remove return? handle
            loBuildings.Remove(building);
        }
    }
}
