using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Aurora.Core.Domain.Entities;
using Aurora.Core.EntityFramework;
using Aurora.Core.IService;
using Aurora.Core.IService.Dto;
using Aurora.Infrastructure.Response;
using AutoMapper;
using Microsoft.EntityFrameworkCore;

namespace Aurora.Core.Service
{
    public class RocketService : IRocketService
    {
        private readonly ApplicationDbContext _dbContext;
        private readonly ApplicationReadonlyDbContext _readonlyDbContext;
        private readonly IMapper _mapper;

        public RocketService(ApplicationDbContext dbContext,
            ApplicationReadonlyDbContext readonlyDbContext,
            IMapper mapper)
        {
            _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));

            _dbContext = dbContext;
            _readonlyDbContext = readonlyDbContext;

        }
        public async Task<ResponseModel<List<RocketWordDto>>> GetList()
        {
            var list = await _readonlyDbContext.Categories.ToListAsync();
            var dtoList = _mapper.Map<List<RocketWordDto>>(list);

            var res = new ResponseModel<List<RocketWordDto>>(dtoList);

            return res;
        }

        public async Task<ResponseModel<RocketWordDto>> GetById(int id)
        {
            var entity = await _readonlyDbContext.Categories.FirstOrDefaultAsync(m => m.ID == id);
            var dto = _mapper.Map<RocketWordDto>(entity);

            var res = new ResponseModel<RocketWordDto>(dto);

            return res;
        }

        public async Task<ResponseModel<RocketWordDto>> Rocket(RocketWordDto model)
        {
            #region 保存到数据库

            var entity = _mapper.Map<RocketWord>(model);

            _dbContext.RocketWords.Add(entity);

            var num = await _dbContext.SaveChangesAsync();

            #endregion

            #region 发送到移动端

            // 按照用户发送
            // 广播发送

            #endregion

            var dto = _mapper.Map<RocketWordDto>(entity);

            var res = new ResponseModel<RocketWordDto>(dto);

            return res;
        }
    }
}
