using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App.Response;
using OpenAuth.App.ServiceApp.FmFireSiteManage;
using OpenAuth.App.ServiceApp.FmInterphoneApp;
using OpenAuth.App.ServiceApp.FmInterphoneApp.Request;
using OpenAuth.App.ServiceApp.FmInterphoneApp.Response;
using OpenAuth.Repository.Domain.FireManagement;

namespace OpenAuth.WebApi.Controllers.ServiceControllers.FireManagement
{
    /// <summary>
    /// 对讲机管理模块
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FmInterPhoneController : ControllerBase
    {
        private readonly FmInterPhoneApp _app;

        public FmInterPhoneController(FmInterPhoneApp app)
        {
            _app = app;
        }

        /// <summary>
        /// 获取对讲机信息
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="account">唯一编码</param>
        /// <param name="isbinding">是否绑定</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<PageInfo<List<InterphoneInfoResp>>>> GetInterphoneInfo(int pageIndex, int pageSize, string account, int isbinding)
        {
            Response<PageInfo<List<InterphoneInfoResp>>> response = new Response<PageInfo<List<InterphoneInfoResp>>>();
            try
            {
                return await _app.GetInterphoneInfo(pageIndex, pageSize, account,isbinding);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 添加或者更新对讲机
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<Response<bool>> AddOrUpdateInterphoneInfo(FmInterphonePoint info)
        {
            Response<bool> response = new();
            try
            {
                return await _app.AddOrUpdateInterphoneInfo(info);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 删除对讲机
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<Response<bool>> DeleteInterphoneInfo(List<string> ids)
        {
            Response<bool> response = new();
            try
            {
                return await _app.DeleteInterphoneInfo(ids);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 导入数据
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public Response<bool> ExportInterphoneInfo(AddOrUpdateBatchExportUsersReq req)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response.Message = _app.ExportInterphoneInfo(req);
                response.Result = true;
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 导出数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public IActionResult ExportInfo()
        {
            var res = new Response();
            var excelRes = _app.ExportInfo();
            if (excelRes.Code == 200)
            {
                return File(excelRes.Result.ToArray(),
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                    "对讲机数据" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
            }
            else
            {
                res.Code = excelRes.Code;
                res.Message = "导出失败";
            }
            return Ok(res);
        }
    }
}
