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

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

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

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

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

        public async Task<int> InsertAsync(Entity.Navigations.Navigation note)
        {
            return await _repository.InsertAsync(note);
        }

        public async Task<bool> UpdateAsync(NavigationInRequest request)
        {
            if (request.Id > 0)
            {
                var model = await GetAsync(request.Id);
                if (request.IsDisable > 0)
                    model.IsDisable = request.IsDisable == 1;
                if (request.Isoperate > 0)
                    model.Isoperate = request.Isoperate == 1;
                if (request.MiddleLableId > 0)
                    model.MiddleLableId = request.MiddleLableId;
                if (!string.IsNullOrEmpty(request.Name))
                    model.Name = request.Name;
                if (request.ParentId > 0)
                    model.ParentId = request.ParentId;
                if (request.Sort > 0)
                    model.Sort = request.Sort;
                if (request.Terminal > 0)
                    model.Terminal = request.Terminal;
                return await _repository.UpdateAsync(model);
            }
            return false;
        }
        public async Task<bool> UpdateAsync(SqlKata.Query query)
        {
            return await _repository.UpdateAsync(query) > 0;
        }
        /// <summary>
        /// 获取 pc 或者移动端
        /// </summary>
        /// <param name="terminal"></param>
        /// <returns></returns>
        public async Task<IEnumerable<Entity.Navigations.Navigation>> GetAllAsync(int terminal)
        {
            var query = Q.NewQuery();
            if (terminal > 0)
                query.Where(nameof(Entity.Navigations.Navigation.Terminal), terminal);
            query.Where(nameof(Entity.Navigations.Navigation.IsDisable), false);
            query.OrderByDesc(nameof(Entity.Navigations.Navigation.Sort));
            return await _repository.GetAllAsync(query);
        }
    }
}