using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using XYY.Common.Standard;
using XYY.Data.Standard.Channel;
using XYY.Model.Standard.Channel;

namespace XYY.Service.Standard.Channel
{
    public interface IChannelService
    {
        Task AddPublicChannelsAsDto(Logistics_PublicChannelCategoryDto dto);
        Task<IEnumerable<Logistics_Public>> CacheGetAllPublic();
        Task<IEnumerable<Logistics_Channel>> CacheGetAllChannel();
        Task EditPublicChannelsAsDto(Logistics_PublicChannelCategoryDto dto);
        Task<IEnumerable<Logistics_PublicChannelCategoryDto>> GetDtos(QueryModel queryModel);

    }

    public class ChannelService : IChannelService
    {
        const string PublicChannelCacheKey = "@@PublicChannelCacheKey";
        const string ChannelCacheKey = "@@ChannelCacheKey";
        const string Token = "A2DGA5DGAD2DGC6CEB59F383A6615GE2";
        const string ApiHost = "http://wms.xingyunyi.cn";
        IDistributedCache _distributedCache;

        readonly ILogistics_PublicRepository _publicRepository;
        readonly ILogistics_PublicChannelCategoryRepository _publicChannelCategoryRepository;
        readonly ILogistics_ChannelRepository _channelRepository;
        readonly ILogistics_BoxRepository _boxRepository;
        public ChannelService(IDistributedCache distributedCache, ILogistics_PublicRepository publicRepository,
            ILogistics_PublicChannelCategoryRepository publicChannelCategoryRepository, ILogistics_ChannelRepository channelRepository, ILogistics_BoxRepository boxRepository)
        {
            _distributedCache = distributedCache;
            _publicRepository = publicRepository;
            _publicChannelCategoryRepository = publicChannelCategoryRepository;
            _channelRepository = channelRepository;
            _boxRepository = boxRepository;
        }



        public async Task<IEnumerable<Logistics_Public>> CacheGetAllPublic()
        {
            return await ChannelClientService.CacheGetAllPublic();
        }

        public async Task<IEnumerable<Logistics_Channel>> CacheGetAllChannel()
        {
             return await ChannelClientService.CacheGetAllChannel();
        }

        public async Task AddPublicChannels(Logistics_PublicChannelCategory model)
        {
            if (await _publicChannelCategoryRepository.IsExistsAsync(x => x.RootId == model.RootId && x.CountryCode == model.CountryCode))
            {
                throw new Exception("不能配置相同公布渠道与国家");
            }
            else
            {
                await _publicChannelCategoryRepository.InsertAsync(model);
            }
        }

        public async Task EditPublicChannels(Logistics_PublicChannelCategory model)
        {
            if (await _publicChannelCategoryRepository.IsExistsAsync(x =>
            x.RootId == model.RootId
            && x.CountryCode == model.CountryCode
            && x.Id != model.Id))
            {
                throw new Exception("不能配置相同公布渠道与国家");
            }
            else
            {
                await _publicChannelCategoryRepository.UpdateAsync(model);
            }
        }

        public async Task AddPublicChannelsAsDto(Logistics_PublicChannelCategoryDto dto)
        {
            foreach (var item in dto.CountryCodes.Split(','))
            {
                await AddPublicChannels(
                    new Logistics_PublicChannelCategory
                    {
                        RootId = dto.RootId,
                        PublicId = dto.PublicId,
                        CountryCode = item
                    });
            }
        }

        public async Task EditPublicChannelsAsDto(Logistics_PublicChannelCategoryDto dto)
        {
            await _publicChannelCategoryRepository.DeleteAsync(x => x.RootId == dto.RootId && x.PublicId == dto.PublicId);
            await AddPublicChannelsAsDto(dto);
        }


        public async Task<IEnumerable<Logistics_PublicChannelCategoryDto>> GetDtos(QueryModel queryModel)
        {
            return await _publicChannelCategoryRepository.GetDtos(queryModel);
        }
    }
}
