using Castle.Core.Internal;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Ropin.Inspection.Api.Common;
using Ropin.Inspection.Api.Controllers;
using Ropin.Inspection.Common.Helper;
using Ropin.Inspection.Model;
using Ropin.Inspection.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace Ropin.Inspection.Api
{

    public class TmtnPushMsgToController : BaseController
    {
        public ILogger<TmtnPushMsgToController> _logger { get; }
        private readonly ITmtnPushMsgToService _TmtnPushMsgToService;
        private readonly IHttpClientFactory _httpClientFactory;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="TmtnPushMsgToService"></param>
        /// <param name="logger"></param>
        /// <param name="httpClientFactory"></param>
        public TmtnPushMsgToController(ITmtnPushMsgToService TmtnPushMsgToService, IHttpClientFactory httpClientFactory, ILogger<TmtnPushMsgToController> logger)
        {
            _TmtnPushMsgToService = TmtnPushMsgToService;
            _logger = logger;
            _httpClientFactory = httpClientFactory;
        }

        /// <summary>
        /// 通过id获取推送消息配置信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetPushMsgToAsync/{id}")]
        public async Task<ApiResult> GetPushMsgToAsync(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var content = await _TmtnPushMsgToService.GetConditionAsync(new TmtnPushMsgToSearchModel { C_ID = id });
                return new ApiResult<TmtnPushMsgToViewModel>(content.FirstOrDefault());
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 获取所有推送消息配置
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetPushMsgTosAsync")]
        public async Task<ApiResult> GetPushMsgTosAsync()
        {
            try
            {
                var contentList = await _TmtnPushMsgToService.GetAllAsync();
                return new ApiResult<IEnumerable<TmtnPushMsgToViewModel>>(contentList);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 通过推送消息配置名称条件查询
        /// </summary>
        /// <param name="searchModel"></param>
        /// <returns></returns>
        [HttpPost("GetPushMsgTosByAsync")]
        public async Task<ApiResult> GetPushMsgTosByAsync(TmtnPushMsgToSearchModel searchModel)
        {
            if (searchModel == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            searchModel.IsPagination = false;
            try
            {
                var contentList = await _TmtnPushMsgToService.GetConditionAsync(searchModel);
                return new ApiResult<PagesModel<TmtnPushMsgToViewModel>>(new PagesModel<TmtnPushMsgToViewModel>(contentList, searchModel));
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        /// <summary>
        /// 通过设备ID获取消息分配人员信息
        /// </summary>
        /// <param name="devStoreCode"></param>
        /// <returns></returns>
        [HttpGet("GetPushMsgToUsersByAsync/{devStoreCode}")]
        public async Task<ApiResult> GetPushMsgToUsersByAsync(string devStoreCode)
        {
            if (string.IsNullOrEmpty(devStoreCode))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var contentList = await _TmtnPushMsgToService.GetPushMsgToUsersByAsync(devStoreCode);
                return new ApiResult<IEnumerable<TmtnPushMsgToUserViewModel>>(contentList);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        /// <summary>
        /// 通过设备ID和人员ID获取消息分配人员信息
        /// </summary>
        /// <param name="devStoreCode"></param>
        /// <param name="userCode"></param>
        /// <returns></returns>
        [HttpGet("GetPushMsgToUsersByAsync/{devStoreCode}/{userCode}")]
        public async Task<ApiResult> GetPushMsgTosByAsync(string devStoreCode, string userCode)
        {
            if (string.IsNullOrEmpty(devStoreCode))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var content = await _TmtnPushMsgToService.GetPushMsgTosByAsync(devStoreCode, userCode);
                return new ApiResult<TmtnUserPushMsgToViewModel>(content);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 创建推送消息配置
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        [HttpPost("CreatePushMsgToAsync")]
        public async Task<ApiResult> CreatePushMsgToAsync(TmtnPushMsgToViewModel content)
        {
            if (content == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _TmtnPushMsgToService.CreateOneAsync(content);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }
        /// <summary>
        /// 创建推送消息数组配置
        /// </summary>
        /// <param name="contentList"></param>
        /// <returns></returns>
        [HttpPost("CreatePushMsgToListAsync")]
        public async Task<ApiResult> CreatePushMsgToListAsync(IEnumerable<TmtnPushMsgToViewModel> contentList)
        {
            if (contentList == null ||string.IsNullOrEmpty(contentList.FirstOrDefault().C_DevStoreCode))
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _TmtnPushMsgToService.CreatePushMsgToListAsync(contentList);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }
        /// <summary>
        /// 单用户消息数组配置
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        [HttpPost("CreatePushMsgToHaveListAsync")]
        public async Task<ApiResult> CreatePushMsgToHaveListAsync(TmtnPushMsgToListCreateModel content)
        {
            if (content == null || string.IsNullOrEmpty(content.C_DevStoreCode) || string.IsNullOrEmpty(content.C_PushPersonCode))
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _TmtnPushMsgToService.CreatePushMsgToHaveListAsync(content);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

        /// <summary>
        /// 删除推送消息配置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("DeletePushMsgToAsync/{id}")]
        public async Task<ApiResult> DeletePushMsgToAsync(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _TmtnPushMsgToService.DeleteAsync(id);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

        /// <summary>
        /// 删除推送消息配置根据【C_DevStoreCode】和【C_PushPersonCode】
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("DeleteByDevPersonIdAsync")]
        public async Task<ApiResult> DeleteByDevPersonIdAsync(TmtnPushMsgToListDelete model)
        {
            if (model==null|| model.pushPersonList==null || model.pushPersonList.Count==0)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
               bool resulu=  await _TmtnPushMsgToService.DeleteByDevPersonIdAsync(model);
                if (resulu)
                {
                    return new ApiResult(ReturnCode.Success);
                }
                else
                {
                    return new ApiResult(ReturnCode.GeneralError);
                }
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        /// <summary>
        /// 更新推送消息配置
        /// </summary>
        /// <param name="id"></param>
        /// <param name="updateModel"></param>
        /// <returns></returns>
        [HttpPut("UpdatePushMsgToAsync/{id}")]
        public async Task<ApiResult> UpdatePushMsgToAsync(string id, TmtnPushMsgToUpdateModel updateModel)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _TmtnPushMsgToService.UpdateAsync(id, updateModel);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

        /// <summary>
        /// 测试推送消息
        /// </summary>
        /// <returns></returns>
        [HttpPost("PushMessageAsync/{userWechatId}/{msg}")]
        [AllowAnonymous]
        public async Task<ApiResult> PushMessageAsync(string userWechatId, string msg)
        {
            try
            {
                var user = await _TmtnPushMsgToService.GetConditionAsync(new TmtnPushMsgToSearchModel { C_DevStoreCode = "",C_PushTypeCode = "" });

                var content = new
                {
                    thing2 = new { value = "niu" ?? "" },
                    time4 = new { value = DateTime.Now.ToString("yyyy-MM-dd hh:mm") },
                    thing5 = new { value = "异常" ?? "" },
                    thing6 = new { value = "大门损坏" ?? "" },
                    thing9 = new { value = msg ?? "" }
                };

                new WeChatHelper(_httpClientFactory).PushMessageToUser(new List<string>() { userWechatId }, content);
                return new ApiResult(ReturnCode.Success);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        /// <summary>
        /// 测试推送消息
        /// </summary>
        /// <returns></returns>
        [HttpGet("PushMsgTestAsync")]
        [AllowAnonymous]
        public ApiResult PushMsgTestAsync()
        {
            try
            {
                //EmailHelper.SendEmail("154817501@qq.com", "环保数字","测试主题", "测试推送消息");
                _TmtnPushMsgToService.PushAlarmMsgAsync(new TpushMsgModel
                {
                    C_DevStoreCode = "f59ce998-6fd7-4f19-99eb-4fe567433d48",
                    C_MsgTypeCode = "MSG_TYPE_001",
                    Msg = "压力表要维修",
                    Subject = "压力表维修通知"
                });
                return new ApiResult(ReturnCode.Success, "chenggong");
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
    }
}