using Hotline.Caching.Interfaces;
using Hotline.Settings;
using Hotline.Share.Attributes;
using Hotline.Share.Dtos.Snapshot;
using Hotline.Snapshot.Interfaces;
using Microsoft.AspNetCore.Http;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using XF.Domain.Dependency;

namespace Hotline.Application.Snapshot;
public class SnapshotBulletinApplication : ISnapshotBulletinApplication, IScopeDependency
{

    private readonly ISystemSettingCacheManager _systemSettingCacheManager;
    private readonly ISnapshotBulletinRepository _bulletinRepository;

    public SnapshotBulletinApplication(ISystemSettingCacheManager systemSettingCacheManager, ISnapshotBulletinRepository bulletinRepository)
    {
        _systemSettingCacheManager = systemSettingCacheManager;
        _bulletinRepository = bulletinRepository;
    }

    /// <summary>
    /// 处理通知公告图片附件路径
    /// </summary>
    /// <param name="sHtmlText"></param>
    /// <returns></returns>
    public string GetSiteUrls(string sHtmlText)
    {
        sHtmlText = sHtmlText.Replace("&lt;", "<").Replace("&gt;", ">");

        //临时内容
        System.Text.StringBuilder sb = new StringBuilder();
        sb.Append(sHtmlText);
        // 定义正则表达式用来匹配 img 标签
        Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
        // 搜索匹配的字符串
        MatchCollection matches = regImg.Matches(sHtmlText);

        // 定义正则表达式用来匹配 video 标签
        Regex regvideo = new Regex(@"<video\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
        // 搜索匹配的字符串
        MatchCollection matchesvideo = regvideo.Matches(sHtmlText);

        var strSiteUrl = _systemSettingCacheManager.GetSetting(SettingConstants.OldFilesUrls)?.SettingValue[0];

        // 取得匹配项列表 视频
        foreach (Match match in matchesvideo)
        {
            if (-1 == match.Groups["imgUrl"].Value.IndexOf("http"))
            {
                sb.Replace(match.Groups["imgUrl"].Value, strSiteUrl + match.Groups["imgUrl"].Value);
                sb.Replace(strSiteUrl + strSiteUrl + match.Groups["imgUrl"].Value, strSiteUrl + match.Groups["imgUrl"].Value);
            }
        }

        // 取得匹配项列表
        foreach (Match match in matches)
        {
            if (-1 == match.Groups["imgUrl"].Value.IndexOf("http"))
            {
                sb.Replace(match.Groups["imgUrl"].Value, strSiteUrl + match.Groups["imgUrl"].Value);
                sb.Replace(strSiteUrl + strSiteUrl + match.Groups["imgUrl"].Value, strSiteUrl + match.Groups["imgUrl"].Value);
            }
        }

        return sb.ToString();
    }

    [ExportExcel("随手拍公告")]
    public ISugarQueryable<SnapshotBulletinItemsOutDto> QueryBulletinItems(SnapshotBulletinItemsInDto dto)
    {
        var query = _bulletinRepository.Queryable()
            .Includes(x => x.ExaminMan)
            .WhereIF(!string.IsNullOrEmpty(dto.SnapshotBulletinTypeName), d => d.SnapshotBulletinTypeName.Contains(dto.SnapshotBulletinTypeName))
            .WhereIF(!string.IsNullOrEmpty(dto.Title), d => d.Title.Contains(dto.Title))
            .WhereIF(dto.BeginCreationTime.HasValue, d => d.BulletinTime >= dto.BeginCreationTime)
            .WhereIF(dto.EndCreationTime.HasValue, d => d.BulletinTime <= dto.EndCreationTime)
            .WhereIF(dto.State.HasValue, d => d.BulletinState == dto.State)
            .OrderByDescending(d => d.CreationTime)
            .Select<SnapshotBulletinItemsOutDto>();
        return query;
    }
}
