using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SolarPower.Models;
using SolarPower.Models.PowerStation;
using SolarPower.Models.Role;
using SolarPower.Repository.Interface;
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
            {

                List<int> powerStationIds = await powerStationRepository.GetPowerStationIdsByUserRole(myUser);
                var overview = await overviewRepository.GetOverviewByPowerStationIds(powerStationIds);
                mapOverview.Today_kwh = overview.Today_kwh;
                mapOverview.Total_kwh = overview.Total_kwh;
                mapOverview.Today_irradiance = overview.Today_irradiance;
                mapOverview.Avg_irradiance = overview.Avg_irradiance;
                mapOverview.Today_PR = overview.Today_PR;
                mapOverview.Avg_PR = overview.Avg_PR;
                mapOverview.Today_kwhkwp = overview.Today_kwhkwp;
                mapOverview.Avg_kwhkwp = overview.Avg_kwhkwp;
                mapOverview.Today_carbon = overview.Today_carbon;
                mapOverview.Total_carbon = overview.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;

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

                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;
        }
    }
}
