using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using Datory;
using GxPress.Common.AppOptions;
using GxPress.Common.Tools;
using GxPress.Repository.Interface.Category;
using Microsoft.Extensions.Options;
using SqlKata;

namespace GxPress.Repository.Implement.Category
{
    public class CategoryRepository : ICategoryRepository
    {

        private readonly Repository<Entity.tede2.Category.Category> _repository;
        private readonly IMapper _mapper;
        private readonly string _connectionString;
        private readonly string _databaseTypestr;
        public CategoryRepository(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.Category.Category>(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<IEnumerable<Entity.tede2.Category.Category>> GetAllAsync()
        {
            return await _repository.GetAllAsync();
        }

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

        public async Task<IEnumerable<Entity.tede2.Category.Category>> GetCategoryChildrenAsync(int parentId)
        {
            var result = await _repository.GetAllAsync(Q.Where(nameof(Entity.tede2.Category.Category.ParentId), parentId));
            foreach (var item in result)
            {
                item.IsChildren = await IsChildren(item.Id);
            }
            return result;
        }

        public async Task<int> InsertAsync(Entity.tede2.Category.Category note)
        {

            return await _repository.InsertAsync(note);
        }

        public async Task<bool> UpdateAsync(Entity.tede2.Category.Category note)
        {
            return await _repository.UpdateAsync(note);
        }

        public async Task<bool> UpdateAsync(Query query)
        {
            return await _repository.UpdateAsync(query) > 0;
        }

        public async Task<bool> IsChildren(int parentId)
        {
            var query = Q.NewQuery();
            query.Where(nameof(Entity.tede2.Category.Category.ParentId), parentId);
            return await _repository.CountAsync(query) > 0;
        }

        public async Task<string> GetCategoryParentAsync(int id, string categoryName)
        {
            var result = await _repository.GetAsync(id);
            if (result == null)
                return categoryName;
            categoryName = string.IsNullOrEmpty(categoryName) ? $"{result.Name}" : $"{result.Name}/{categoryName}";
            return await GetCategoryParentAsync(result.ParentId, categoryName);
        }

    }
}
