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

namespace GxPress.Repository.Implement.Attach
{
    public class AttachRepository : IAttachRepository
    {
        private readonly Repository<Entity.tede2.Attach.Attach> _repository;
        private readonly IMapper _mapper;
        private readonly string _connectionString;
        private readonly string _databaseTypestr;
        public AttachRepository(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.Attach.Attach>(database);
            _mapper = mapper;
        }

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

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

        public async Task<PagedList<Entity.tede2.Attach.Attach>> GetAllAsync(AttachRequest request)
        {
            var result = new PagedList<Entity.tede2.Attach.Attach>();
            var query = Q.NewQuery();
            query.Where(nameof(Entity.tede2.Attach.Attach.AttachType), request.TypeId);
            if (!string.IsNullOrEmpty(request.KeyWord))
                query.WhereLike(nameof(Entity.tede2.Attach.Attach.Name), $"%{request.KeyWord}%");
            if (request.CategoryId > 0)
                query.Where(nameof(Entity.tede2.Attach.Attach.CategoryId), request.CategoryId);
            if (request.AdminId > 0)
                query.Where(nameof(Entity.tede2.Attach.Attach.CategoryId), request.AdminId);
            if (request.WithdrawType > 0)
                query.Where(nameof(Entity.tede2.Attach.Attach.IsWithdraw), request.WithdrawType == 2);
            result.Items = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
            result.Total = await _repository.CountAsync(query);
            return result;
        }
        public async Task<IEnumerable<Entity.tede2.Attach.Attach>> GetSearchAllAsync(AttachRequest request)
        {
            var query = Q.NewQuery();
            query.Where(nameof(Entity.tede2.Attach.Attach.AttachType), request.TypeId);
            if (!string.IsNullOrEmpty(request.KeyWord))
                query.WhereLike(nameof(Entity.tede2.Attach.Attach.Name), $"%{request.KeyWord}%");
            if (request.CategoryId > 0)
                query.Where(nameof(Entity.tede2.Attach.Attach.CategoryId), request.CategoryId);
            if (request.AdminId > 0)
                query.Where(nameof(Entity.tede2.Attach.Attach.CategoryId), request.AdminId);
            if (request.WithdrawType > 0)
                query.Where(nameof(Entity.tede2.Attach.Attach.IsWithdraw), request.WithdrawType == 2);
            var result = await _repository.GetAllAsync(query);
            return result;
        }

        public async Task<Entity.tede2.Attach.Attach> GetAsync(int id)
        {
            return await _repository.GetAsync(id);
        }

        public async Task<int> InsertAsync(Entity.tede2.Attach.Attach note)
        {
            var categoryId = "";
            var categoryIds = StringUtils.StringCollectionToStringList(note.CategoryId);
            foreach (var item in categoryIds)
            {
                categoryId += item.Split(",")[item.Split(",").Length - 1] + ",";
            }
            categoryId = categoryId.Remove(categoryId.Length - 1, 1);
            note.FullCategoryId = note.CategoryId;
            note.CategoryId = categoryId;
            return await _repository.InsertAsync(note);
        }

        public async Task<bool> UpdateAsync(Entity.tede2.Attach.Attach note)
        {
            var categoryId = "";
            var categoryIds = StringUtils.StringCollectionToStringList(note.CategoryId);
            foreach (var item in categoryIds)
            {
                categoryId += item.Split(",")[item.Split(",").Length - 1] + ",";
            }
            categoryId = categoryId.Remove(categoryId.Length - 1, 1);
            note.FullCategoryId = note.CategoryId;
            note.CategoryId = categoryId;
            return await _repository.UpdateAsync(note);
        }

    }
}