using SolarPower.Models;
using SolarPower.Models.PowerStation;
using SolarPower.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SolarPower.Services.Implement
{
    public class MyPowerStationService
    {
        private readonly IPowerStationRepository powerStationRepository;

        public MyPowerStationService(IPowerStationRepository powerStationRepository)
        {
            this.powerStationRepository = powerStationRepository;
        }

        public List<MyCity> GetMyCities(MyUser myUser, byte showEnable = 1)
        {
            List<MyCity> myCities = new List<MyCity>();

            myCities = powerStationRepository.GetMyCities(myUser, showEnable);

            return myCities;
        }

        public List<PowerStation> GetMyPowerStations(MyUser myUser, byte showEnable = 1, List<int> cityIds = null, List<string> wheres = null, Dictionary<string, object> where_entities = null, List<string> orderBy = null)
        {
            List<PowerStation> powerStations = new List<PowerStation>();
            powerStations = powerStationRepository.GetMyPowerStationList(myUser, showEnable, cityIds, wheres, where_entities, orderBy);

            return powerStations;
        }

        public List<MyPowerStationGroupByCity> GetMyPowerStationsGroupByCity(MyUser myUser, List<int> cityIds = null, List<string> wheres = null, Dictionary<string, object> where_entities = null, List<string> orderBy = null)
        {
            List<PowerStation> powerStations = new List<PowerStation>();

            powerStations = powerStationRepository.GetMyPowerStationList(myUser,1, cityIds, wheres, where_entities, orderBy);

            var myPowerStations_group = powerStations.GroupBy(x => x.CityId);

            List<MyPowerStationGroupByCity> myPowerStationGroupByCities = new List<MyPowerStationGroupByCity>();
            foreach (var powerStations_group_item in myPowerStations_group)
            {
                MyPowerStationGroupByCity myPowerStationGroupByCity = new MyPowerStationGroupByCity();
                myPowerStationGroupByCity.CityId = powerStations_group_item.Key;
                myPowerStationGroupByCity.CityName = powerStations_group_item.First().CityName;
                myPowerStationGroupByCity.Amount = powerStations_group_item.Count();
                myPowerStationGroupByCity.MyPowerStations = powerStations_group_item.ToList();

                myPowerStationGroupByCities.Add(myPowerStationGroupByCity);
            }

            return myPowerStationGroupByCities;
        }

        public PowerStationsSummary GetMyPowerStationsSummary(List<PowerStation> powerStations)
        {
            PowerStationsSummary powerStationsSummary = new PowerStationsSummary();

            if(powerStations.Count() <= 0)
            {
                return powerStationsSummary;
            }

            var sum_Today_kwh = 0.0;
            var sum_Total_kwh = 0.0;
            var sum_Today_irradiance = 0.0;
            var sum_Avg_irradiance = 0.0;
            var sum_Today_PR = 0.0;
            var sum_Avg_PR = 0.0;
            var sum_Today_kwhkwp = 0.0;
            var sum_Avg_kwhkwp = 0.0;
            var sum_Today_money = 0.0;
            var sum_Total_money = 0.0;
            var sum_Today_carbon = 0.0;
            var sum_Total_carbon = 0.0;

            var count_powerStation = powerStations.Count();

            foreach (var powerStation in powerStations)
            {
                sum_Today_kwh += powerStation.Today_kWh;
                sum_Total_kwh += powerStation.Total_kWh;
                sum_Today_irradiance += powerStation.Today_irradiance;
                sum_Avg_irradiance += powerStation.Avg_irradiance;
                sum_Today_PR += powerStation.Today_PR;
                sum_Avg_PR += powerStation.Avg_PR;
                sum_Today_kwhkwp += powerStation.Today_kwhkwp;
                sum_Avg_kwhkwp += powerStation.Avg_kwhkwp;
                sum_Today_money += powerStation.Today_Money;
                sum_Total_money += powerStation.Total_Money;
                sum_Today_carbon += powerStation.Today_Carbon;
                sum_Total_carbon += powerStation.Total_Carbon;
            }

            powerStationsSummary.Today_kwh = sum_Today_kwh;
            powerStationsSummary.Total_kwh = sum_Total_kwh;
            powerStationsSummary.Today_irradiance = sum_Today_irradiance / count_powerStation;
            powerStationsSummary.Avg_irradiance = sum_Avg_irradiance / count_powerStation;
            powerStationsSummary.Today_PR = sum_Today_PR / count_powerStation;
            powerStationsSummary.Avg_PR = sum_Avg_PR / count_powerStation;
            powerStationsSummary.Today_kwhkwp = sum_Today_kwhkwp / count_powerStation;
            powerStationsSummary.Avg_kwhkwp = sum_Avg_kwhkwp / count_powerStation;
            powerStationsSummary.Today_money = sum_Today_money;
            powerStationsSummary.Total_money = sum_Total_money;
            powerStationsSummary.Today_carbon = sum_Today_carbon;
            powerStationsSummary.Total_carbon = sum_Total_carbon;

            return powerStationsSummary;
        }
}
}
