<commit_before>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Emby.Naming.Common;
using Emby.Naming.TV;

namespace Emby.Naming.AudioBook
{
    public class AudioBookFilePathParser
    {
        private readonly NamingOptions _options;
        private readonly IRegexProvider _iRegexProvider;

        public AudioBookFilePathParser(NamingOptions options, IRegexProvider iRegexProvider)
        {
            _options = options;
            _iRegexProvider = iRegexProvider;
        }

        public AudioBookFilePathParserResult Parse(string path, bool IsDirectory)
        {
            AudioBookFilePathParserResult result = Parse(path);
            return !result.Success ? new AudioBookFilePathParserResult() : result;
        }

        private AudioBookFilePathParserResult Parse(string path)
        {
            var result = new AudioBookFilePathParserResult();
            var fileName = Path.GetFileNameWithoutExtension(path);
            foreach (var expression in _options.AudioBookPartsExpressions)
            {
                var match = _iRegexProvider.GetRegex(expression, RegexOptions.IgnoreCase).Match(fileName);
                if (match.Success)
                {
                    if (!result.ChapterNumber.HasValue)
                    {
                        var value = match.Groups["chapter"];
                        if (value.Success)
                            result.ChapterNumber = int.Parse(value.Value);
                    }
                    if (!result.PartNumber.HasValue)
                    {
                        var value = match.Groups["part"];
                        if (value.Success)
                            result.PartNumber = int.Parse(value.Value);
                    }
                }
            }
            
            /*var matches = _iRegexProvider.GetRegex("\\d+", RegexOptions.IgnoreCase).Matches(fileName);
            if (matches.Count > 0)
            {
                if (!result.ChapterNumber.HasValue)
                {<commit_msg>use int.tryparse<commit_after>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Emby.Naming.Common;
using Emby.Naming.TV;

namespace Emby.Naming.AudioBook
{
    public class AudioBookFilePathParser
    {
        private readonly NamingOptions _options;
        private readonly IRegexProvider _iRegexProvider;

        public AudioBookFilePathParser(NamingOptions options, IRegexProvider iRegexProvider)
        {
            _options = options;
            _iRegexProvider = iRegexProvider;
        }

        public AudioBookFilePathParserResult Parse(string path, bool IsDirectory)
        {
            AudioBookFilePathParserResult result = Parse(path);
            return !result.Success ? new AudioBookFilePathParserResult() : result;
        }

        private AudioBookFilePathParserResult Parse(string path)
        {
            var result = new AudioBookFilePathParserResult();
            var fileName = Path.GetFileNameWithoutExtension(path);
            foreach (var expression in _options.AudioBookPartsExpressions)
            {
                var match = _iRegexProvider.GetRegex(expression, RegexOptions.IgnoreCase).Match(fileName);
                if (match.Success)
                {
                    if (!result.ChapterNumber.HasValue)
                    {
                        var value = match.Groups["chapter"];
                        if (value.Success)
                        {
                            int intValue;
                            if (int.TryParse(value.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out intValue))
                            {
                                result.ChapterNumber = intValue;
                            }
                        }
                    }
                    if (!result.PartNumber.HasValue)
                    {
                        var value = match.Groups["part"];
                        if (value.Success)
                        {
                            int intValue;
                            if (int.TryParse(value.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out intValue))
                            {
                                result.ChapterNumber = intValue;
                            }
                        }
                    }
                }
            }

            /*var matches = _iRegexProvider.GetRegex("\\d+", RegexOptions.IgnoreCase).Matches(fileName);
            if (matches.Count > 0)
            {
                if (!result.ChapterNumber.HasValue)
                {
{'fraction_non_alphanumeric': 0.07317578772802653, 'fraction_numerical': 0.00041459369817578774, 'mean_word_length': 1.4935400516795865, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2643800', 'n_tokens_mistral': 1131, 'n_tokens_neox': 1082, 'n_words': 223}