using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App.BaseApp.SysGroupManager.Request;
using OpenAuth.App.ServiceApp.InsTaskHallManager;
using OpenAuth.App.ServiceApp.InsTaskHallManager.Response;
using OpenAuth.App.SysDatabaseLink;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers.ServiceControllers
{
    /// <summary>
    /// 任务大厅
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InsTaskHallController : ControllerBase
    {
        private readonly InsTaskHallApp _app;
        public InsTaskHallController(InsTaskHallApp app)
        {
            _app = app;
        }

        /// <summary>
        ///任务区
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<dynamic>>> GetTaskList()
        {
            var result = new Response<List<dynamic>>();
            try
            {
                result.Result = await _app.GetTaskList();
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        ///已完成任务
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<InsTaskRes>>> GetCompleteTaskList()
        {
            var result = new Response<List<InsTaskRes>>();
            try
            {
                result.Result = await _app.GetCompleteTaskList();
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }


        /// <summary>
        /// 最佳牛马排行
        /// </summary>
        /// <param name="flag">标识（日榜：day，月榜：month，周榜：week）</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<dynamic>>> GetRankingList(string flag)
        {
            var result = new Response<List<dynamic>>();
            try
            {
                result.Result = await _app.GetRankingList(flag);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 在办任务
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<dynamic>>> GetTaskingList()
        {
            var result = new Response<List<dynamic>>();
            try
            {
                result.Result = await _app.GetTaskingList();
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 组排名
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<dynamic>>> GetGroupRankingList()
        {
            var result = new Response<List<dynamic>>();
            try
            {
                result.Result = await _app.GetGroupRankingList();
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 根据任务id获取分组任务
        /// </summary>
        /// <param name="id">任务id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<InsTaskgroup>>> GetgrouptaskList(string id)
        {
            var result = new Response<List<InsTaskgroup>>();
            try
            {
                result.Result = await _app.GetgrouptaskList(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 领取任务
        /// </summary>
        /// <param name="groupIds">分组任务ids</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> ReciveTask(List<string> groupIds)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.ReciveTask(groupIds);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
    }
}
