using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using GxPress.Common.AppOptions;
using GxPress.Common.Tools;
using GxPress.Entity;
using GxPress.Repository.Interface;
using Microsoft.Extensions.Options;
using Datory;
using GxPress.Request.Slide;
using GxPress.Common.Page;

namespace GxPress.Repository.Implement
{
    public class SlideRepository : ISlideRepository
    {
        private readonly Repository<Slide> _repository;
        private readonly IMapper _mapper;

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

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

        public async Task<bool> ExistsAsync(int id)
        {
            return await _repository.ExistsAsync(id);
        }

        public async Task<Slide> GetAsync(int id)
        {
            var slide = await _repository.GetAsync(id);
            slide.ImageUrl = StringUtils.AddDomain(slide.ImageUrl);
            return slide;
        }

        public async Task<PagedList<Slide>> GetListAsync(SlideSearchRequest request)
        {
            var result = new PagedList<Slide>();
            var query = Q.NewQuery();
            if (!string.IsNullOrEmpty(request.KeyWord))
                query.WhereLike(nameof(Slide.Title), $"%{request.KeyWord}%");
            if (request.TypeId > 0)
                query.Where(nameof(Slide.TypeId), request.TypeId);
            result.Total = await _repository.CountAsync(query);
            var slides = await _repository.GetAllAsync(query.OrderBy(nameof(Slide.Sort)).ForPage(request.Page, request.PerPage));
            foreach (var slide in slides)
                slide.ImageUrl = StringUtils.AddDomain(slide.ImageUrl);
            result.Items = slides;
            return result;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<Slide> AddAsync(Slide request)
        {
            var sort = await _repository.MaxAsync(nameof(Slide.Sort)) ?? 0;
            request.Sort = sort + 1;
            request.ImageUrl = StringUtils.RemoveDomain(request.ImageUrl);
            request.Id = await _repository.InsertAsync(request);
            return request;
        }

        public async Task<Slide> EditAsync(Slide request)
        {
            request.ImageUrl = StringUtils.RemoveDomain(request.ImageUrl);
            await _repository.UpdateAsync(request);
            return request;
        }

        public async Task<bool> DeleteAsync(int id)
        {
            return await _repository.DeleteAsync(id);
        }
    }
}