using Microsoft.AspNetCore.Hosting;
using System.IO.Compression;

namespace Tiobon.Core.Services;

/// <summary>
/// 课件 (服务)
/// </summary>
public class Ghre_CourseWareServices : BaseServices<Ghre_CourseWare, Ghre_CourseWareDto, InsertGhre_CourseWareInput, EditGhre_CourseWareInput>, IGhre_CourseWareServices
{
    private readonly IBaseRepository<Ghre_CourseWare> _dal;
    private IGhre_CourseServices _ghre_CourseServices;
    private readonly IWebHostEnvironment _hostingEnvironment;
    private IGhre_CourseWareAttachmentServices _ghre_CourseWareAttachmentServices;

    public Ghre_CourseWareServices(ICaching caching,
        IGhre_CourseServices ghre_CourseServices,
        IWebHostEnvironment hostingEnvironment,
        IGhre_CourseWareAttachmentServices ghre_CourseWareAttachmentServices,
        IBaseRepository<Ghre_CourseWare> dal)
    {
        this._dal = dal;
        base.BaseDal = dal;
        base._caching = caching;
        _ghre_CourseServices = ghre_CourseServices;
        _ghre_CourseWareAttachmentServices = ghre_CourseWareAttachmentServices;
        _hostingEnvironment = hostingEnvironment;
    }

    public override async Task<long> Add(InsertGhre_CourseWareInput entity)
    {
        if (await base.AnyAsync(x => x.CourseWareName == entity.CourseWareName && x.VersionNo == entity.VersionNo))
            throw new Exception("已存在相同课名称和版本号数据！");
        entity.CourseIds = null;

        var result = await base.Add(entity);
        entity.Attachments.ForEach(x =>
        {
            x.CourseWareId = result;
            x.RelativePath = x.RelativePath.Replace("/Advanced", null);
        });
        if (entity.Source == "Attachments")
            await _ghre_CourseWareAttachmentServices.Add(entity.Attachments);
        else if (entity.Links != null && entity.Links.Any())
        {
            var attachments = entity.Links.Select(x => new InsertGhre_CourseWareAttachmentInput()
            {
                CourseWareId = result,
                AttachmentName = x.LinkName,
                RelativePath = x.LinkAddress,
                AttachFileExtension = "http",
                LearnDuration = x.LearnDuration
            }).ToList();
            await _ghre_CourseWareAttachmentServices.Add(attachments);
        }

        return result;
    }

    public override async Task<bool> Update(long Id, EditGhre_CourseWareInput editModel)
    {
        if (await base.AnyAsync(x => x.CourseWareName == editModel.CourseWareName && x.VersionNo == editModel.VersionNo && x.Id != Id))
            throw new Exception("已存在相同课名称和版本号数据！");

        #region 判断对应课程是否在学习中
        var courseIds = await Db.Queryable<Ghre_Course>().Where(x => x.CourseWareId == Id).Select(x => x.Id).ToListAsync();
        var courseSceneIds = await Db.Queryable<Ghre_Course>().Where(x => x.CourseWareId == Id).Select(x => x.CourseSceneId).ToListAsync();
        if (await Db.Queryable<Ghre_StudyRecord>().AnyAsync(x =>
        ((x.CourseId != null && courseIds.Contains(x.CourseId.Value)) || (x.CourseSceneId != null && courseSceneIds.Contains(x.CourseSceneId.Value))) &&
        x.StudyStatus != Consts.DIC_STUDY_RECORD_STUDY_STATUS.HAS_FINISH && x.CourseEndTime != null &&
        x.CourseEndTime.Value.Date >= DateTime.Now.Date))
            throw new Exception($"关联课程/场景有学员正在学习中，暂不可修改附件");
        #endregion

        if (editModel.CourseIds2.Any())
        {
            editModel.CourseIds = JsonConvert.SerializeObject(editModel.CourseIds2);
            var courses = await _ghre_CourseServices.Query(x => editModel.CourseIds2.Contains(x.Id));
            editModel.CourseNames = string.Join("、", courses.Select(x => x.CourseNo + " " + x.CourseName));
        }

        await _ghre_CourseWareAttachmentServices.Delete(x => x.CourseWareId == Id);

        if (editModel.Source == "Attachments")
        {

            editModel.Attachments.ForEach(x =>
            {
                x.CourseWareId = Id;
                x.RelativePath = x.RelativePath.Replace("/Advanced", null);
            });
            await _ghre_CourseWareAttachmentServices.Add(editModel.Attachments);
        }
        else
        {
            if (editModel.Links != null && editModel.Links.Any())
            {
                var attachments = editModel.Links.Select(x => new InsertGhre_CourseWareAttachmentInput()
                {
                    CourseWareId = Id,
                    AttachmentName = x.LinkName,
                    RelativePath = x.LinkAddress,
                    AttachFileExtension = "http",
                    LearnDuration = x.LearnDuration
                }).ToList();
                await _ghre_CourseWareAttachmentServices.Add(attachments);
            }
        }
        return await base.Update(Id, editModel);
    }

    public override async Task<ServiceFormResult<Ghre_CourseWareDto>> QueryForm(QueryForm body)
    {
        var result = await base.QueryForm(body);
        string courseIds = result.result.DT_TableDataT1[0].CourseIds;
        if (courseIds.IsNotEmptyOrNull())
            result.result.DT_TableDataT1[0].CourseIds2 = JsonConvert.DeserializeObject<List<long>>(courseIds);
        else result.result.DT_TableDataT1[0].CourseIds2 = new List<long>();

        var attachments = await _ghre_CourseWareAttachmentServices.Query(x => x.CourseWareId == body.id);

        result.result.DT_TableDataT1[0].Attachments = attachments.OrderBy(x => x.SortNo).Where(x => x.AttachFileExtension != "http").ToList();
        if (result.result.DT_TableDataT1[0].Attachments.Any())
        {
            result.result.DT_TableDataT1[0].Attachments.ForEach(x => x.RelativePath = "/Advanced" + x.RelativePath);
        }
        result.result.DT_TableDataT1[0].Links = attachments.Where(x => x.AttachFileExtension == "http").Select(x => new Ghre_CourseWareLink()
        {
            LinkName = x.AttachmentName,
            LinkAddress = x.RelativePath,
            LearnDuration = x.LearnDuration
        }).ToList();

        var IDS = result.result.DT_TableDataT1[0].CourseIds2;
        result.result.DT_TableDataT1[0].Courses = await _ghre_CourseServices.Query(x => IDS.Contains(x.Id));

        if (body.doType == "Copy")
        {
            result.result.DT_TableDataT1[0].CourseWareNo = null;
            result.result.DT_TableDataT1[0].CourseWareName = null;
        }
        return result;

    }

    public override async Task<ServicePageResult<Ghre_CourseWareDto>> QueryFilterPage(QueryBody filter)
    {
        bool? IsAllowDownload = null;
        if (filter.jsonParam != null)
            foreach (JProperty jProperty in filter.jsonParam.Properties())
            {
                var name = jProperty.Name;
                var value = jProperty.Value.ToString();
                if (name != "IsAllowDownload")
                    continue;
                if (!string.IsNullOrWhiteSpace(value))
                {
                    var jsonParam = JsonConvert.DeserializeObject<JsonParam>(value);

                    switch (name)
                    {
                        case "IsAllowDownload":
                            IsAllowDownload = Convert.ToBoolean(jsonParam.columnValue);
                            break;
                        default:
                            break;
                    }
                }
            }
        string condition = string.Empty;
        if (IsAllowDownload != null)
        {
            string sql = @$"SELECT DISTINCT B.Id
                                FROM Ghre_CourseWareAttachment A
                                     JOIN Ghre_CourseWare B ON A.CourseWareId = B.Id AND B.IsEnable = 1
                                WHERE A.IsEnable = 1 AND A.AttachFileExtension != 'http' AND A.IsAllowDownload = '{IsAllowDownload}'";
            var entitys = await Db.Ado.SqlQueryAsync<long>(sql);
            if (entitys.Any())
                condition = "Id IN (" + string.Join(",", entitys.Select(x => x)) + ")";
        }
        var result = await base.QueryFilterPage(filter, condition);
        var data = result.result.DT_TableDataT1;
        var attachmentIds = data.Select(x => x.Id).ToList();

        var courseIds = new List<long>();
        var attachments = await _ghre_CourseWareAttachmentServices.Query(x => x.CourseWareId != null && attachmentIds.Contains(x.CourseWareId.Value));
        data.ForEach(async x =>
        {
            if (!string.IsNullOrWhiteSpace(x.CourseIds))
            {
                x.CourseIds2 = JsonConvert.DeserializeObject<List<long>>(x.CourseIds);
                courseIds.AddRange(x.CourseIds2);
            }

            x.StudyDuration = $"{x.Hours}小时{x.Minutes}分钟";
            x.SourceLabel = await GetParaLabel("CourseWareSource", x.Source);
            x.Attachments = attachments.Where(a => a.CourseWareId == x.Id && a.AttachFileExtension != "http").OrderBy(x => x.SortNo).ToList();
            x.Links = attachments.Where(a => a.CourseWareId == x.Id && a.AttachFileExtension == "http").OrderBy(x => x.SortNo)
            .Select(x => new Ghre_CourseWareLink()
            {
                LinkName = x.AttachmentName,
                LinkAddress = x.RelativePath,
            }).ToList();
        });
        courseIds = courseIds.Distinct().ToList();

        var courses = await _ghre_CourseServices.Query(x => courseIds.Contains(x.Id));

        data.ForEach(x =>
            {
                if (!string.IsNullOrWhiteSpace(x.CourseIds))
                    x.CourseNames = string.Join("、", courses.Where(o => x.CourseIds2.Contains(o.Id)).Select(o => o.CourseNo + " " + o.CourseName));
            });
        result.result.DT_TableDataT1 = data;
        return result;

    }

    #region 打包下载
    public async Task<ServiceResult<string>> DownZip(long id)
    {
        var result = await base.QueryById(id);
        if (result is null)
            return ServiceResult<string>.OprateFailed("无效的课件ID！");
        var attachments = await _ghre_CourseWareAttachmentServices.Query(x => x.CourseWareId == id && x.AttachFileExtension != "http");
        var webRootPath = _hostingEnvironment.WebRootPath;
        var outPath = $"/files/upload/{result.CourseWareName}_{result.VersionNo}_{DateTime.Now.ToString("yyyyMMddHHmmss")}.zip";
        if (attachments.Any())
        {
            var newFilePath = $"{webRootPath}/files/upload/{SnowFlakeSingle.Instance.NextId()}";

            Directory.CreateDirectory(newFilePath);

            attachments.ForEach(x =>
            {
                if (File.Exists($"{webRootPath}{"\\" + x.RelativePath}"))
                    FileHelper.FileCoppy($"{webRootPath}{"\\" + x.RelativePath}", $"{newFilePath}/{x.AttachmentName}");
            });

            var files = attachments.Select(x => $"{newFilePath}{"\\" + x.AttachmentName}").ToArray();
            if (files.Length > 1)
                CreateZip($"{webRootPath}{outPath}", files);
            else
                outPath = newFilePath + "/" + attachments[0].AttachmentName;
            return ServiceResult<string>.OprateSuccess(null, "/Advanced" + outPath);
        }
        else
            return ServiceResult<string>.OprateFailed("该课件无附件！");

    }

    public static void CreateZip(string zipPath, string[] filesToZip)
    {
        if (File.Exists(zipPath))
        {
            throw new IOException("File already exists.");
        }

        using (FileStream zipFile = new FileStream(zipPath, FileMode.Create))
        {
            using (ZipArchive zipArchive = new ZipArchive(zipFile, ZipArchiveMode.Create))
            {
                foreach (string file in filesToZip)
                {
                    if (File.Exists(file))
                    {
                        ZipArchiveEntry entry = zipArchive.CreateEntryFromFile(file, Path.GetFileName(file));
                    }
                }
            }
        }
    }
    #endregion

    #region 删除
    // <summary>
    /// 删除指定ID的数据
    /// </summary>
    /// <param name="id">主键ID</param>
    /// <returns></returns>
    public override async Task<bool> DeleteById1(object id)
    {

        if (await Db.Queryable<Ghre_Course>().AnyAsync(x => x.CourseWareId == (long)id))
            throw new Exception($"该课件已与课程关联,暂不可删除！");

        return await base.DeleteById1(id);
    }

    /// <summary>
    /// 删除指定ID集合的数据(批量删除)
    /// </summary>
    /// <param name="ids">主键ID集合</param>
    /// <returns></returns>
    public override async Task<bool> DeleteByIds1(long[] ids)
    {

        foreach (var id in ids)
        {
            if (await Db.Queryable<Ghre_Course>().AnyAsync(x => x.CourseWareId == (long)id))
                throw new Exception($"课件已与课程关联,暂不可删除！");
        }

        return await base.DeleteByIds1(ids);
    }
    #endregion
}