using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using solarApp.Service;
using Microsoft.Extensions.Logging;
using System.Configuration;

namespace SolarPower.Services.Implement
{
    public class SensorHourlyService
    {
        ILogger _logger;
        procArchiveSensorHourly sensorSvc;
        procArchiveHourly HourSvc;
        string Connection1 = string.Empty;

        public SensorHourlyService(string Connection_parame = null, ILogger logger = null)
        {
            if (!string.IsNullOrEmpty(Connection_parame))
            {
                Connection1 = Connection_parame;
            }
            else
            {
                Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString;
            }
            sensorSvc = new procArchiveSensorHourly(Connection1);
            HourSvc = new procArchiveHourly(Connection1);
            if (logger != null)
            {
                _logger = logger;
            }
        }

        

        public void ArchiveSensorHourly(string code, string dateTime)
        {
            #region 歸檔 Sensor 
            try
            {
                if (_logger != null)
                {
                    _logger.LogInformation("【SenSorHourlyService】【ArchiveSensorHourly】使用winform，開始執行[{0}]在{1}寫入資料", code, dateTime);
                }
                sensorSvc.insertData(code, dateTime);
                if (_logger != null)
                {
                    _logger.LogInformation("【SenSorHourlyService】【ArchiveSensorHourly】完成執行[{0}]在{1}寫入資料", code, dateTime);
                }
            }
            catch (Exception exception)
            {
                if (_logger != null)
                {
                    _logger.LogError("【SenSorHourlyService】【ArchiveSensorHourly】[Exception] - {0}", exception.Message);
                }
                if (exception.InnerException != null)
                {
                    if (_logger != null)
                    {
                        _logger.LogError("【SenSorHourlyService】【ArchiveSensorHourly】[InnerException] - {0}", exception.InnerException.Message);
                    }
                }
            }
            #endregion
            if (_logger != null)
            {
                _logger.LogInformation("【SenSorHourlyService】【ArchiveSensorHourly】[執行完成] ");
            }
        }

        public Task<bool> ArchiveStationHourly()
        {
            bool result = false;
            #region 歸檔 Station 
            try
            {
                HourSvc.proc_s1_site();
                result = true;
            }
            catch (Exception exception)
            {
                if (_logger != null)
                {
                    _logger.LogError("【SenSorHourlyService】【ArchiveStationHourly】[Exception] - {0}", exception.Message);
                }
                if (exception.InnerException != null)
                {
                    if (_logger != null)
                    {
                        _logger.LogError("【SenSorHourlyService】【ArchiveStationHourly】[InnerException] - {0}", exception.InnerException.Message);
                    }
                }
            }
            #endregion
            if (_logger != null)
            {
                _logger.LogInformation("【SenSorHourlyService】【ArchiveStationHourly】[執行完成] ");
            }
            return Task.FromResult(result);
        }

    }
}
