using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.App.ServiceApp.MiManager.Resquest;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers
{
    /// <summary>
    /// 停车场
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MiParkingController : ControllerBase
    {
        private readonly MiParkingApp _app;
        public MiParkingController(MiParkingApp app)
        {
            _app = app;
        }

        #region 查询

        #region 分页
        /// <summary>
        /// 分页
        /// </summary>
        [HttpGet]
        public async Task<Response<PageInfo<List<MiParking>>>> LoadAllPage([FromQuery] PageReq request)
        {
            return await _app.LoadAllPage(request);
        }

        /// <summary>
        /// app扫码查询停车场信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<MiParking>>> LoadParkinginfo()
        {
            return await _app.LoadParkinginfo();
        }
        #endregion

        #region 实体
        /// <summary>
        /// 实体
        /// </summary>
        [HttpGet]
        public async Task<Response<dynamic>> Get(string id)
        {
            var result = new Response<dynamic>();
            try
            {
                result.Result = await _app.Get(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion
        #endregion

        #region 增删改

        #region 添加
        /// <summary>
        /// 添加
        /// </summary>
        [HttpPost]
        public async Task<Response<bool>> Add(MiParking model)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.Add(model);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 分配用户
        /// </summary>
        [HttpPost]
        public async Task<Response<bool>> AllocateUser(MiParkingUserReq model)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.AllocateUser(model);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改
        /// </summary>
        [HttpPost]
        public async Task<Response<bool>> Update(MiParking model)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.Update(model);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        [HttpPost]
        public async Task<Response<bool>> Delete([FromBody] List<MiParking> models)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.Delete(models);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion
        #endregion
    }
}