<commit_before>﻿using System;
using System.Threading;
using System.Threading.Tasks;
using BuildIt.Config.Core.Standard.Extensions;
using BuildIt.Config.Core.Standard.Services.Interfaces;

namespace BuildIt.Config.Core.Standard.Services
{
    public static class ConfigurationHelpers
    {
        private static SemaphoreSlim SemaphoreSlim { get; } = new SemaphoreSlim(1);

        public static async Task<bool> CheckMinimumVersion(this IAppConfigurationService configurationService, bool handleLoadConfigValidation = false, bool retrieveCached = false, string minVerKey = Constants.AppConfigurationMinVersionKey)
        {
            if (configurationService == null) return false;

            configurationService.Mapper.EnsurePresence(minVerKey, true);
            var appConfig = await configurationService.LoadAppConfig(handleLoadConfigValidation, retrieveCached);
            if (appConfig == null) return false;

            var minimumVersionMappedValue = appConfig.GetValueForKey<string>(minVerKey);
            var appVerFromPlatform = configurationService.VersionService?.GetVersion();

            if (appVerFromPlatform == null || minimumVersionMappedValue == null)
            {
                return false;
            }

            // Check the minimum required version first
            Version versionToCheck;
            return !Version.TryParse(minimumVersionMappedValue, out versionToCheck) || versionToCheck <= appVerFromPlatform;
        }

        public static async Task NotifyUserWhenNotMetAppMinVer(this IAppConfigurationService configurationService, bool handleLoadConfigValidation = false, bool retrieveCached = false, string minVerKey = Constants.AppConfigurationMinVersionKey)
        {
            var metMinimumAppVer = await CheckMinimumVersion(configurationService, handleLoadConfigValidation, retrieveCached, minVerKey);

            if (!metMinimumAppVer)
            {
                //Block the app from running & alert users
                await configurationService.BlockAppFromRunning(Constants.AppConfigurationMinimumVersionNotMetUserDialogTitle, Constants.AppConfigurationMinimumVersionNotMetUserDialogBody, async () =>
                {
                    await NotifyUserWhenNotMetAppMinVer(configurationService, handleLoadConfigValidation, retrieveCached, minVerKey);
                });
            }
        }

        public static async Task<bool> CheckRecommendedVersion(this IAppConfigurationService configurationService, bool retrieveCached = false, string recommVerKey = Constants.AppConfigurationRecommVersionKey)
        {
            if (configurationService == null) return false;

            configurationService.Mapper.EnsurePresence(recommVerKey);

            var appConfig = await configurationService.LoadAppConfig(retrieveCached);
            var recommVersionMappedValue = appConfig?.GetValueForKey<string>(recommVerKey);
            var appVerFromPlatform = configurationService.VersionService?.GetVersion();

            if (appVerFromPlatform == null || recommVersionMappedValue == null)
            {<commit_msg>Small fix for the refactored code<commit_after>﻿using System;
using System.Threading;
using System.Threading.Tasks;
using BuildIt.Config.Core.Standard.Extensions;
using BuildIt.Config.Core.Standard.Services.Interfaces;

namespace BuildIt.Config.Core.Standard.Services
{
    public static class ConfigurationHelpers
    {
        private static SemaphoreSlim SemaphoreSlim { get; } = new SemaphoreSlim(1);

        public static async Task<bool> CheckMinimumVersion(this IAppConfigurationService configurationService, bool retrieveCached = false, string minVerKey = Constants.AppConfigurationMinVersionKey)
        {
            if (configurationService == null) return false;

            configurationService.Mapper.EnsurePresence(minVerKey, true);
            var appConfig = await configurationService.LoadAppConfig(false, retrieveCached);
            if (appConfig == null) return false;

            var minimumVersionMappedValue = appConfig.GetValueForKey<string>(minVerKey);
            var appVerFromPlatform = configurationService.VersionService?.GetVersion();

            if (appVerFromPlatform == null || minimumVersionMappedValue == null)
            {
                return false;
            }

            // Check the minimum required version first
            Version versionToCheck;
            return !Version.TryParse(minimumVersionMappedValue, out versionToCheck) || versionToCheck <= appVerFromPlatform;
        }

        public static async Task NotifyUserWhenNotMetAppMinVer(this IAppConfigurationService configurationService, bool handleLoadConfigValidation = false, bool retrieveCached = false, string minVerKey = Constants.AppConfigurationMinVersionKey)
        {
            var metMinimumAppVer = await CheckMinimumVersion(configurationService, retrieveCached, minVerKey);
            if (!metMinimumAppVer)
            {
                //Block the app from running & alert users
                await configurationService.BlockAppFromRunning(Constants.AppConfigurationMinimumVersionNotMetUserDialogTitle, Constants.AppConfigurationMinimumVersionNotMetUserDialogBody, async () =>
                {
                    await NotifyUserWhenNotMetAppMinVer(configurationService, handleLoadConfigValidation, retrieveCached, minVerKey);
                });
            }
        }

        public static async Task<bool> CheckRecommendedVersion(this IAppConfigurationService configurationService, bool retrieveCached = false, string recommVerKey = Constants.AppConfigurationRecommVersionKey)
        {
            if (configurationService == null) return false;

            configurationService.Mapper.EnsurePresence(recommVerKey);

            var appConfig = await configurationService.LoadAppConfig(retrieveCached);
            var recommVersionMappedValue = appConfig?.GetValueForKey<string>(recommVerKey);
            var appVerFromPlatform = configurationService.VersionService?.GetVersion();

            if (appVerFromPlatform == null || recommVersionMappedValue == null)
            {
{'fraction_non_alphanumeric': 0.06557645134914146, 'fraction_numerical': 0.00032706459525756336, 'mean_word_length': 3.3009845288326303, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1696076', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1387, 'n_words': 317}