using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SolarPower.Models;
using SolarPower.Models.PowerStation;
using SolarPower.Models.Role;
using SolarPower.Repository.Interface;
using SolarPower.Services.Implement;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace SolarPower.Controllers
{
    public class MapOverviewController : MyBaseController<MapOverviewController>
    {
        private readonly IOverviewRepository overviewRepository;
        private readonly IPowerStationRepository powerStationRepository;

        public MapOverviewController(
            IOverviewRepository overviewRepository,
            IPowerStationRepository powerStationRepository) : base()
        {
            this.overviewRepository = overviewRepository;
            this.powerStationRepository = powerStationRepository;
        }


        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public async Task<ApiResult<MapOverview>> GetMapOverview()
        {
            ApiResult<MapOverview> apiResult = new ApiResult<MapOverview>();

            MapOverview mapOverview = new MapOverview();
            try
            {
                var myPowerStations = myPowerStationService.GetMyPowerStationsGroupByCity(myUser);

                List<int> powerStationIds = myPowerStations.SelectMany(x => x.MyPowerStations.Select(y => y.Id)).ToList();

                var powerStations = myPowerStations.SelectMany(x => x.MyPowerStations).ToList();
                var powerStationSummary = myPowerStationService.GetMyPowerStationsSummary(powerStations);

                //var overview = await overviewRepository.GetOverviewByPowerStationIds(powerStationIds);
                mapOverview.Today_kwh = powerStationSummary.Today_kwh;
                mapOverview.Total_kwh = powerStationSummary.Total_kwh;
                mapOverview.Today_irradiance = powerStationSummary.Today_irradiance;
                mapOverview.Avg_irradiance = powerStationSummary.Avg_irradiance;
                mapOverview.Today_PR = powerStationSummary.Today_PR;
                mapOverview.Avg_PR = powerStationSummary.Avg_PR;
                mapOverview.Today_kwhkwp = powerStationSummary.Today_kwhkwp;
                mapOverview.Avg_kwhkwp = powerStationSummary.Avg_kwhkwp;
                mapOverview.Today_carbon = powerStationSummary.Today_carbon;
                mapOverview.Total_carbon = powerStationSummary.Total_carbon;

                mapOverview.CapacityDataTables = await overviewRepository.GetCapacityDataTableByPowerStationIds(powerStationIds);

                var totalPowerStationCount = 0;
                var totalCapacity = 0.0;
                
                foreach(var capacity in mapOverview.CapacityDataTables)
                {
                    totalPowerStationCount += capacity.SubPowerStationCount;
                    totalCapacity += capacity.SubTotalCapacity;
                }

                mapOverview.TotalPowerStationCount = totalPowerStationCount;
                mapOverview.TotalCapacity = totalCapacity / 1000; // kWp -> MWp

                //mapOverview.PowerStations = await overviewRepository.GetListPowerStationByPowerStationIds(powerStationIds);
                mapOverview.PowerStations = powerStations;

                mapOverview.UpdatedAt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");


                apiResult.Code = "0000";
                apiResult.Data = mapOverview;

            }
            catch (Exception exception)
            {
                apiResult.Code = "9999";
                Logger.LogError("【" + controllerName + "/" + actionName + "】");
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
            }

            apiResult.Msg = errorCode.GetString(apiResult.Code);
            return apiResult;
        }
    }
}
