using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using GxPress.Common.Tools;
using GxPress.Entity;
using GxPress.Repository.Interface.AppReport;
using GxPress.Request.App.AppReport;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GxPress.Api.AdminControllers
{
    /// <summary>
    /// App数据报表
    /// </summary>
    [Route("api/admin/app-report")]
    [ApiController]
    [Authorize]
    public class AdminAppReportController : ControllerBase
    {
        private  readonly IAppReportRepository _appReportRepository;
        private readonly IMapper _mapper;
        public AdminAppReportController(IAppReportRepository appReportRepository,IMapper mapper)
        {
            _appReportRepository = appReportRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("add")]
        public async Task<int> Insert(AppReportRequest request)
        {
            request.IocUrl = StringUtils.RemoveDomain(request.IocUrl);
            var appReport = _mapper.Map<AppReport>(request);
            return await _appReportRepository.InsertAsync(appReport);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("update")]
        public async Task<bool> Update(AppReportRequest request)
        {
            request.IocUrl = StringUtils.RemoveDomain(request.IocUrl);
            var appReport = _mapper.Map<AppReport>(request);
            return await _appReportRepository.UpdateAsync(appReport);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        [HttpPost("list")]
        public async Task<IEnumerable<Entity.AppReport>> GetAppReportList()
        {
            var result= await _appReportRepository.GetAppReportListAsync();
            var appReportList = result as AppReport[] ?? result.ToArray();
            foreach (var appReport in appReportList)
            {
                appReport.IocUrl = StringUtils.AddDomain(appReport.IocUrl);
            }
            
            return appReportList;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<bool> Delete(int id)
        {
            return await _appReportRepository.DeleteAsyncTask(id);
        }

        /// <summary>
        /// 根据type查询数据
        /// </summary>
        /// <param name="reportType"></param>
        /// <returns></returns>
        [HttpGet("{reportType}")]
        public async Task<Entity.AppReport> GetAppReport(int reportType)
        {
            var appReport = await _appReportRepository.GetAppReport(reportType);
            appReport.IocUrl = StringUtils.AddDomain(appReport.IocUrl);
            return appReport;
        }
    }
}