using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using Datory;
using GxPress.Common.AppOptions;
using GxPress.Common.Tools;
using GxPress.Repository.Interface.Media;
using GxPress.Result.Media;
using Microsoft.Extensions.Options;
using System.Transactions;
using GxPress.Common.Exceptions;
using GxPress.Common.Page;
using GxPress.Request.Media;
using System;

namespace GxPress.Repository.Implement.Media
{
    public class MediaRepository : IMediaRepository
    {
        private readonly Repository<Entity.tede2.Media.Media> _repository;
        private readonly Repository<Entity.tede2.Media.MediaLibrary> mediaLibraryRepository;
        private readonly Repository<Entity.tede2.Media.MediaLable> mediaLableRepository;
        private readonly IMapper _mapper;
        private readonly string _connectionString;
        private readonly string _databaseTypestr;
        public MediaRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
        {
            _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
            _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
            var databaseType =
                StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
            var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
            _repository = new Repository<Entity.tede2.Media.Media>(database);
            mediaLibraryRepository = new Repository<Entity.tede2.Media.MediaLibrary>(database);
            mediaLableRepository = new Repository<Entity.tede2.Media.MediaLable>(database);
            _mapper = mapper;
        }

        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;

        public async Task<MediaResult> GetAsync(int id)
        {
            var result = new MediaResult();
            //获取媒体
            var media = await _repository.GetAsync(id);
            result = _mapper.Map<MediaResult>(media);
            //获取媒体库资源
            var mediaLibrarys = await mediaLibraryRepository.GetAllAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), id));
            result.MediaLibraryResults = mediaLibrarys.ToList().Select(n => _mapper.Map<MediaLibraryResult>(n)).ToList();
            //获取媒体标签
            var mediaLables = await mediaLableRepository.GetAllAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), id));
            result.MediaLableResults = mediaLables.Select(n => _mapper.Map<MediaLableResult>(n)).ToList();
            return result;
        }
        /// <summary>
        /// 删除媒体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int id)
        {
            try
            {
                using (var transactionScope = new TransactionScope())
                {
                    // //删除媒体库
                    // await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), id));
                    // //删除媒体标签
                    // await mediaLableRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), id));

                    await _repository.UpdateAsync(Q.Where(nameof(Entity.tede2.Media.Media.Id), id).Set(nameof(Entity.tede2.Media.Media.IsDelete), true));
                    transactionScope.Complete();
                }
            }
            catch (System.Exception ex)
            {
                throw new BusinessException(ex.Message);
            }
            return true;
        }
        /// <summary>
        /// 添加媒体
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public async Task<bool> InsertAsync(MediaResult result)
        {
            var model = _mapper.Map<Entity.tede2.Media.Media>(result);
            try
            {
                using (var transactionScope = new TransactionScope())
                {
                    model.AutoNumber = DateTime.Now.Ticks.ToString();
                    var categoryId = "";
                    foreach (var item in result.CategoryId)
                    {
                        categoryId += item.Split(",")[item.Split(",").Length - 1] + ",";
                    }
                    categoryId = categoryId.Remove(categoryId.Length - 1, 1);
                    model.CategoryId = categoryId;
                    model.FullCategoryId = StringUtils.ObjectCollectionToString(result.CategoryId);
                    var categoryName = "";
                    foreach (var item in result.CategoryName)
                    {
                        categoryName += item.Split(",")[item.Split(",").Length - 1] + ",";
                    }
                    categoryName = categoryName.Remove(categoryName.Length - 1, 1);
                    model.CategoryName = categoryName;
                    model.IsLibrary = result.MediaLibraryResults.Count > 0;
                    var id = await _repository.InsertAsync(model);
                    if (id > 0)
                    {
                        foreach (var item in result.MediaLibraryResults)
                        {
                            var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
                            await mediaLibraryRepository.InsertAsync(mediaLibrary);
                        }
                    }
                    //添加标签
                    foreach (var item in result.MediaLableResults)
                    {
                        var mediaLable = _mapper.Map<Entity.tede2.Media.MediaLable>(item);
                        await mediaLableRepository.InsertAsync(mediaLable);
                    }
                    if (result.Id == 0 && result.TeacherId == 0 && !string.IsNullOrEmpty(result.Author))
                    {
                        //新增名师

                    }
                    //提交事务
                    transactionScope.Complete();
                }
            }
            catch (System.Exception ex)
            {
                throw new Common.Exceptions.BusinessException(ex.Message);
            }

            return true;
        }

        public async Task<bool> UpdateAsync(Entity.tede2.Media.Media note)
        {
            return await _repository.UpdateAsync(note);
        }
        public async Task<bool> UpdateAsync(SqlKata.Query query)
        {
            return await _repository.UpdateAsync(query) > 0;
        }

        public async Task<PagedList<Entity.tede2.Media.Media>> GetAllAsync(MediaSearchRequest request)
        {
            var result = new PagedList<Entity.tede2.Media.Media>();
            var query = Q.NewQuery();
            if(!string.IsNullOrEmpty(request.KeyWord))
               query.WhereLike(nameof(Entity.tede2.Media.Media.Title),$"%{request.KeyWord}%");
            if (request.IsDelete > 0)
                query.Where(nameof(Entity.tede2.Media.Media.IsDelete), request.IsDelete);
            if (request.MediaType > 0)
                query.Where(nameof(Entity.tede2.Media.Media.MediaType), request.MediaType);
            if (request.LibraryType > 0)
                query.Where(nameof(Entity.tede2.Media.Media.IsLibrary), request.LibraryType == 1);
            result.Total = await _repository.CountAsync(query);
            var item = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage).OrderByDesc(nameof(Entity.tede2.Media.Media.Sort)));
            result.Items = item;
            return result;
        }

        public async Task<bool> UpdateAsync(MediaResult result)
        {
            var model = _mapper.Map<Entity.tede2.Media.Media>(result);
            try
            {
                using (var transactionScope = new TransactionScope())
                {
                    var categoryId = "";
                    foreach (var item in result.CategoryId)
                    {
                        categoryId += item.Split(",")[item.Split(",").Length - 1] + ",";
                    }
                    categoryId = categoryId.Remove(categoryId.Length - 1, 1);
                    model.CategoryId = categoryId;
                    model.FullCategoryId = StringUtils.ObjectCollectionToString(result.CategoryId);
                    var categoryName = "";
                    foreach (var item in result.CategoryName)
                    {
                        categoryName += item.Split(",")[item.Split(",").Length - 1] + ",";
                    }
                    categoryName = categoryName.Remove(categoryName.Length - 1, 1);
                    model.CategoryName = categoryName;
                    model.IsLibrary = result.MediaLibraryResults.Count > 0;
                    await _repository.UpdateAsync(model);
                    //删除媒体库
                    await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), result.Id));
                    //删除媒体标签
                    await mediaLableRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), result.Id));
                    foreach (var item in result.MediaLibraryResults)
                    {
                        var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
                        await mediaLibraryRepository.InsertAsync(mediaLibrary);
                    }
                    //添加标签
                    foreach (var item in result.MediaLableResults)
                    {
                        var mediaLable = _mapper.Map<Entity.tede2.Media.MediaLable>(item);
                        await mediaLableRepository.InsertAsync(mediaLable);
                    }
                    //提交事务
                    transactionScope.Complete();
                }
            }
            catch (System.Exception ex)
            {
                throw new Common.Exceptions.BusinessException(ex.Message);
            }

            return true;
        }
    }
}
