using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using OpenAuth.App.Response;
using OpenAuth.App.ServiceApp.FireManagement;
using OpenAuth.App.ServiceApp.FireManagement.Request;
using OpenAuth.App.ServiceApp.FireManagement.Response;
using OpenAuth.Repository.Domain.FireManagement;

namespace OpenAuth.WebApi.Controllers.ServiceControllers.FireManagement
{
    /// <summary>
    /// 网格
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FireGridController : ControllerBase
    {
        private readonly FireGridApp _app;

        public FireGridController(FireGridApp app)
        {
            _app = app;
        }
        /// <summary>
        /// 添加打卡点
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> AddCheckPoint(CheckPointReq info)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                return await _app.AddCheckPoint(info);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 获取打卡点列表
        /// </summary>
        /// <param name="pointName"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<PageInfo<List<CheckPointInfo>>>> GetCheckPointList(long areaid, string pointName, int page, int limit)
        {
            Response<PageInfo<List<CheckPointInfo>>> response = new Response<PageInfo<List<CheckPointInfo>>>();
            try
            {
                return await _app.GetCheckPointList(areaid, pointName, page, limit);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 删除打卡点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> DeleteCheckPoint(long id)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                return await _app.DeleteCheckPoint(id);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 编辑打卡点
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> EditCheckPoint(CheckPointReq info)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                return await _app.EditCheckPoint(info);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 获取单个打卡点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<CheckPointReq>> GetSingleCheckPoint(long id)
        {
            Response<CheckPointReq> response = new Response<CheckPointReq>();
            try
            {
                return await _app.GetCheckPointById(id);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 打卡
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> AddClockOnInfo(FmCheckClockOn info)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                return await _app.AddClockOnInfo(info);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 获取打卡记录
        /// </summary>
        /// <param name="id"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<dynamic>>>> GetCheckInfoByPointId(long id, int page = 1, int limit = 10)
        {
            Response<PageInfo<List<dynamic>>> response = new Response<PageInfo<List<dynamic>>>();
            try
            {
                return await _app.GetCheckPointById(id, page, limit);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 打卡信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<dynamic>>>> GetCheckInfoByUserId(long userid, int page = 1, int limit = 10)
        {
            Response<PageInfo<List<dynamic>>> response = new Response<PageInfo<List<dynamic>>>();
            try
            {
                return await _app.GetCheckInfoByUserId(userid, page, limit);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 获取打卡记录列表
        /// </summary>
        /// <param name="userId">人员ID</param>
        /// <param name="beginTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<dynamic>>>> GetClockOnList(long userId, DateTime beginTime, DateTime endTime, int page, int limit)
        {
            Response<PageInfo<List<dynamic>>> response = new Response<PageInfo<List<dynamic>>>();
            try
            {
                return await _app.GetClockOnList(userId, beginTime, endTime, page, limit);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// app根据登录用户获取打卡点
        /// </summary>
        /// <param name="userid">用户id</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<PageInfo<List<dynamic>>>> GetCheckPointByUserId(string userid)
        {
            return await _app.GetCheckPointByUserId(userid);
        }
        /// <summary>
        /// 数据总览
        /// </summary>
        /// <param name="areaid"></param>
        /// <param name="begintime"></param>
        /// <param name="endtime"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<JObject>> GetStatisticsCount(long areaid, DateTime begintime, DateTime endtime)
        {
            Response<JObject> response = new Response<JObject>();
            try
            {
                return await _app.GetStatisticsCount(areaid, begintime, endtime);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 数据列表
        /// </summary>
        /// <param name="areaid"></param>
        /// <param name="begintime"></param>
        /// <param name="endtime"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<PointstatisticsResp>>>> GetStatistics(long areaid, DateTime begintime, DateTime endtime, int page, int limit)
        {
            Response<PageInfo<List<PointstatisticsResp>>> response = new Response<PageInfo<List<PointstatisticsResp>>>();
            try
            {
                return await _app.Getstatistics(areaid, begintime, endtime, page, limit);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

    }
}
