<commit_before>﻿using Cofoundry.Core;
using Cofoundry.Core.ErrorLogging;
using Cofoundry.Domain.CQS;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cofoundry.Domain
{
    /// <summary>
    /// Route library for image assets
    /// </summary>
    public class ImageAssetRouteLibrary : IImageAssetRouteLibrary
    {
        #region constructor

        private readonly IQueryExecutor _queryExecutor;

        public ImageAssetRouteLibrary(
            IQueryExecutor queryExecutor
            )
        {
            _queryExecutor = queryExecutor;
        }

        #endregion

        #region public methods

        /// <summary>
        /// Simple but less efficient way of getting an image url if you only know 
        /// the id. Use the overload accepting an IImageAssetRenderable if possible to save a 
        /// potential db query if the route isn't cached.
        /// </summary>
        /// <param name="imageAssetId">Id of the image asset to get the url for</param>
        /// <param name="settings">Optional resizing settings for the image</param>
        public async Task<string> ImageAssetAsync(int? imageAssetId, IImageResizeSettings settings = null)
        {
            if (!imageAssetId.HasValue) return string.Empty;

            var asset = await _queryExecutor.GetByIdAsync<ImageAssetRenderDetails>(imageAssetId.Value);

            return ImageAsset(asset, settings);
        }

        /// <summary>
        /// Gets the url for an image asset, with optional resizing parameters
        /// </summary>
        /// <param name="asset">asset to get the url for</param>
        /// <param name="settings">Optional resizing settings for the image</param>
        public string ImageAsset(IImageAssetRenderable asset, IImageResizeSettings settings = null)
        {
            if (asset == null) return string.Empty;
            SetDefaultCrop(asset, settings);

            return ImageAsset(asset.ImageAssetId, asset.FileName, asset.Extension, settings);
        }

        /// <summary>
        /// Gets the url for an image asset, with optional resizing parameters
        /// </summary>
        /// <param name="asset">asset to get the url for</param>
        /// <param name="width">width to resize the image to</param>
        /// <param name="height">height to resize the image to</param>
        public string ImageAsset(IImageAssetRenderable asset, int? width, int? height = null)
        {
            if (asset == null) return string.Empty;

            var settings = new ImageResizeSettings();

            if (width.HasValue)
            {
                settings.Width = width.Value;
            }

            if (height.HasValue)
            {
                settings.Height = height.Value;
            }

            SetDefaultCrop(asset, settings);

            return ImageAsset(asset.ImageAssetId, asset.FileName, asset.Extension, settings);
        }

        #endregion

        #region private helpers

        private static string ImageAsset(int assetId, string fileName, string extension, IImageResizeSettings settings = null)
        {
            var pathFileName = Path.ChangeExtension(assetId + "_" + SlugFormatter.ToSlug(fileName), extension);
            var url = "/assets/images/" + pathFileName;
            if (settings != null)
            {
                url += settings.ToQueryString();
            }
            return url;
        }

        private static void SetDefaultCrop(IImageAssetRenderable asset, IImageResizeSettings settings)
        {
            if (settings == null) return;

            if (isDefinedAndChanged(settings.Width, asset.Width) || isDefinedAndChanged(settings.Height, asset.Height))
            {
                if (settings.Mode == ImageFitMode.Default)
                {
                    settings.Mode = ImageFitMode.Crop;
                }

                if (asset.DefaultAnchorLocation.HasValue)<commit_msg>Removed forcing the Crop setting from the image resizing url because it should now be considered as the default.<commit_after>﻿using Cofoundry.Core;
using Cofoundry.Core.ErrorLogging;
using Cofoundry.Domain.CQS;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cofoundry.Domain
{
    /// <summary>
    /// Route library for image assets
    /// </summary>
    public class ImageAssetRouteLibrary : IImageAssetRouteLibrary
    {
        #region constructor

        private readonly IQueryExecutor _queryExecutor;

        public ImageAssetRouteLibrary(
            IQueryExecutor queryExecutor
            )
        {
            _queryExecutor = queryExecutor;
        }

        #endregion

        #region public methods

        /// <summary>
        /// Simple but less efficient way of getting an image url if you only know 
        /// the id. Use the overload accepting an IImageAssetRenderable if possible to save a 
        /// potential db query if the route isn't cached.
        /// </summary>
        /// <param name="imageAssetId">Id of the image asset to get the url for</param>
        /// <param name="settings">Optional resizing settings for the image</param>
        public async Task<string> ImageAssetAsync(int? imageAssetId, IImageResizeSettings settings = null)
        {
            if (!imageAssetId.HasValue) return string.Empty;

            var asset = await _queryExecutor.GetByIdAsync<ImageAssetRenderDetails>(imageAssetId.Value);

            return ImageAsset(asset, settings);
        }

        /// <summary>
        /// Gets the url for an image asset, with optional resizing parameters
        /// </summary>
        /// <param name="asset">asset to get the url for</param>
        /// <param name="settings">Optional resizing settings for the image</param>
        public string ImageAsset(IImageAssetRenderable asset, IImageResizeSettings settings = null)
        {
            if (asset == null) return string.Empty;
            SetDefaultCrop(asset, settings);

            return ImageAsset(asset.ImageAssetId, asset.FileName, asset.Extension, settings);
        }

        /// <summary>
        /// Gets the url for an image asset, with optional resizing parameters
        /// </summary>
        /// <param name="asset">asset to get the url for</param>
        /// <param name="width">width to resize the image to</param>
        /// <param name="height">height to resize the image to</param>
        public string ImageAsset(IImageAssetRenderable asset, int? width, int? height = null)
        {
            if (asset == null) return string.Empty;

            var settings = new ImageResizeSettings();

            if (width.HasValue)
            {
                settings.Width = width.Value;
            }

            if (height.HasValue)
            {
                settings.Height = height.Value;
            }

            SetDefaultCrop(asset, settings);

            return ImageAsset(asset.ImageAssetId, asset.FileName, asset.Extension, settings);
        }

        #endregion

        #region private helpers

        private static string ImageAsset(int assetId, string fileName, string extension, IImageResizeSettings settings = null)
        {
            var pathFileName = Path.ChangeExtension(assetId + "_" + SlugFormatter.ToSlug(fileName), extension);
            var url = "/assets/images/" + pathFileName;
            if (settings != null)
            {
                url += settings.ToQueryString();
            }
            return url;
        }

        private static void SetDefaultCrop(IImageAssetRenderable asset, IImageResizeSettings settings)
        {
            if (settings == null) return;

            if (isDefinedAndChanged(settings.Width, asset.Width) || isDefinedAndChanged(settings.Height, asset.Height))
            {
                if (asset.DefaultAnchorLocation.HasValue)
{'fraction_non_alphanumeric': 0.09112559538731511, 'fraction_numerical': 0.0, 'mean_word_length': 2.097437888198758, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, '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': '831182', 'n_tokens_mistral': 1990, 'n_tokens_neox': 1935, 'n_words': 590}