using HuanMeng.DotNetCore.TextCensor.SensitiveWord;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Extensions.DependencyInjection;

namespace HuanMeng.DotNetCore.TextCensor
{
    /// <summary>
    /// 
    /// </summary>
    public static class TextCensorExtend
    {

        /// <summary>
        /// 获取文本检测
        /// </summary>
        /// <param name="_dirPath"></param>
        /// <returns></returns>
        public static ITextCensor GetITextCensor(string _dirPath)
        {
            
            return GetSensitiveWordFilterFrozen(_dirPath)
                //GetSensitiveWordFilter(_dirPath)
                ;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="_dirPath"></param>
        /// <returns></returns>
        public static SensitiveWordFilter GetSensitiveWordFilter(string _dirPath)
        {
            SensitiveWordFilter sensitiveWordFilter = new SensitiveWordFilter();
            var ckPath = Path.GetFullPath(_dirPath);
            var filePath = Directory.EnumerateFiles(ckPath);
            foreach (var item in filePath)
            {
                AddShieldString(item, sensitiveWordFilter);
            }
            return sensitiveWordFilter;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_dirPath"></param>
        /// <returns></returns>
        public static SensitiveWordFilterFrozen GetSensitiveWordFilterFrozen(string _dirPath)
        {
            SensitiveWordFilterFrozen sensitiveWordFilter = new SensitiveWordFilterFrozen();
            var ckPath = Path.GetFullPath(_dirPath);
            var filePath = Directory.EnumerateFiles(ckPath);
            foreach (var item in filePath)
            {
                AddShieldString(item, sensitiveWordFilter);
            }
            return sensitiveWordFilter;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        /// <exception cref="Exception"></exception>
        public static void AddShieldString(string path, SensitiveWordFilterFrozen sensitiveWordFilter)
        {

            if (!File.Exists(path))
            {
                throw new Exception("文件不存在");
            }
            using (StreamReader reader = new StreamReader(path, UnicodeEncoding.UTF8))
            {

                while (reader.Peek() > 0)
                {

                    var tempStr = (reader.ReadLine() ?? "");
                    if (!string.IsNullOrEmpty(tempStr))
                    {
                        sensitiveWordFilter.AddSensitiveWord(tempStr);

                    }
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        /// <exception cref="Exception"></exception>
        public static void AddShieldString(string path, SensitiveWordFilter sensitiveWordFilter)
        {

            if (!File.Exists(path))
            {
                throw new Exception("文件不存在");
            }
            using (StreamReader reader = new StreamReader(path, UnicodeEncoding.UTF8))
            {

                while (reader.Peek() > 0)
                {

                    var tempStr = (reader.ReadLine() ?? "");
                    if (!string.IsNullOrEmpty(tempStr))
                    {
                        sensitiveWordFilter.AddSensitiveWord(tempStr);

                    }
                }
            }
        }
    }
}
