using Microsoft.Data.SqlClient;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Concurrent;
namespace kmCustomReportsNET.ClassObj;
public class ClsConstants
{
  public static string KDrive = @"\\salem\k";
  /// 
  /// T = Dot-Net (C#) version of the reports
  /// 
  public enum enuReportIDs : byte
  {
    /// 
    /// Dobbs Monthly Email Contests
    /// 
    T1439 = 101
  }
  static ConcurrentDictionary dctScheduler = new(StringComparer.OrdinalIgnoreCase);
  internal static string GetCustomerRegistryItem(string KeyName)
  {
    string rv = string.Empty;
    try
    {
      using (var cn = new SqlConnection(kmCommonLibsCore.Constants.GetCnString(kmCommonLibsCore.Constants.enuServer.RDB)))
      {
        var Sql = "Select [Value] From crpt.[Registry] Where [Key]=@Key And [Enabled]=1";
        var parms = new { Key = KeyName };
        var theValue = cn.ExecuteScalar(Sql, parms);
        if (theValue != null && !string.IsNullOrWhiteSpace(theValue.ToString()))
          rv = theValue?.ToString() ?? string.Empty;
      }
    }
    catch (Exception ex)
    {
      kmCommonLibsCore.ClsErrorReporting.ErrorEncountered(ex);
      rv = string.Empty;
    }
    return rv;
  }
  internal static void SetCustomerRegistryItem(string KeyName, string Value)
  {
    using (var cn = new SqlConnection(kmCommonLibsCore.Constants.GetCnString(kmCommonLibsCore.Constants.enuServer.RDB)))
    {
      var Sql = "Select Count(*) As Cnt From crpt.[Registry] Where [Key]=@Key And [Enabled]=1";
      var parms = new { Key = KeyName };
      int cnt = cn.ExecuteScalar(Sql, parms);
      if (cnt < 1)
        Sql = "Insert Into crpt.[Registry] ([Key], [Value], [Enabled]) Values (@Key, @Value, 1)";
      else
        Sql = "Update crpt.[Registry] Set [Value]=@Value, [DateChanged]=SysDateTimeOffset() Where [Key]=@Key And [Enabled]=1";
      cn.Execute(Sql, parms);
    }
  }
  internal static bool NeedToRun(string TaskName)
  {
    bool rv = false;
    if (!dctScheduler.ContainsKey(TaskName) || dctScheduler[TaskName] < DateTimeOffset.Now)
      rv = true;
    else
      rv = false;
    return rv;
  }
  internal static void SetNextRun(string TaskName, DateTimeOffset theTime)
  {
    dctScheduler[TaskName] = theTime;
  }
}