using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using GxPress.Common.AppOptions;
using GxPress.Common.Exceptions;
using GxPress.Common.Tools;
using GxPress.Entity;
using GxPress.Repository.Interface;
using GxPress.Request.App.GroupFolder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Options;
using Datory;

namespace GxPress.Repository.Implement
{
    public class GroupFolderRepository:IGroupFolderRepository
    {
        private readonly Repository<GroupFolder> _repository;
        private readonly IMapper _mapper;
        private readonly IWebHostEnvironment _environment;

        public GroupFolderRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper,
            IWebHostEnvironment environment)
        {
            var databaseType =
                StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
            var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
            _repository = new Repository<GroupFolder>(database);
            _environment = environment;
            _mapper = mapper;
        }

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

        /// <summary>
        /// 添加小组文件夹
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<bool> InsertAsync(GroupFolderInRequest request)
        {
            if (string.IsNullOrEmpty(request.Name))
                throw new BusinessException("文件夹名称不能为空");
            if (request.ParentId < 0)
                throw new BusinessException("参数错误");
            if (request.UserId <= 0)
                throw new BusinessException("参数错误");
            var groupFolder = new GroupFolder
            {
                Name = request.Name, ParentId = request.ParentId, UserId = request.UserId
            };
            return await _repository.InsertAsync(groupFolder) > 0;
        }
        /// <summary>
        /// 修改文件夹
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(GroupFolderUpRequest request)
        {
            var groupFolder = await _repository.GetAsync(request.Id);
            if (!string.IsNullOrEmpty(request.Name))
                groupFolder.Name = request.Name;
            return await _repository.UpdateAsync(groupFolder);
        }
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int id)
        {
            return await _repository.DeleteAsync(id);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<IEnumerable<GroupFolder>> FindGroupFoldersAsync(GroupFolderFindRequest request)
        {
            return await _repository.GetAllAsync(Q.Where(nameof(GroupFolder.ParentId), request.GroupFolderId)
                .Where(nameof(GroupFolder.UserId), request.UserId));
        }
    }
}
