using AutoMapper;
using Ropin.Inspection.Common.Accessor.Interface;
using Ropin.Inspection.Common.Helper;
using Ropin.Inspection.Model.Entities;
using Ropin.Inspection.Model.SearchModel.PRD;
using Ropin.Inspection.Model.ViewModel.PRD;
using Ropin.Inspection.Repository.PRD.Interface;
using Ropin.Inspection.Service.PRD.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Ropin.Inspection.Service.PRD
{   
    public class BoxApiService: IBoxApiService
    {
        private readonly IBoxApiRepository _repository;
        private readonly IMapper _mapper;
        private readonly IHttpClientFactory _httpClientFactory;
        public BoxApiService(IBoxApiRepository repository, IMapper mapper,IHttpClientFactory httpClientFactory)
        {
            _repository = repository;
            _mapper = mapper;
            _httpClientFactory = httpClientFactory;
        }
        public async Task<PrivModel> GetUserPriv(string openId,string deviceCode)
        {
            var content = await _repository.GetUserPriv(openId, deviceCode);
            return content;
        }

        public async Task<bool> AddDevData(string C_DeviceCode, string C_ConfigCode, string C_Value)
        {
            TDEV_DevData devData = new TDEV_DevData();
            devData.C_DeviceCode = C_DeviceCode;
            devData.C_ConfigCode = C_ConfigCode;
            devData.C_Value = C_Value;
            devData.D_CreateOn = DateTime.Now;
            var content = await _repository.AddDevData(devData);
            return content;
        }
        public async Task<bool> AddDevAlert(string C_DeviceCode, string C_ConfigCode, string C_Value)
        {
            TDEV_DevAlert devAlert = new TDEV_DevAlert();
            devAlert.C_ID = Guid.NewGuid();
            devAlert.C_DeviceCode = C_DeviceCode;
            devAlert.C_ConfigCode = C_ConfigCode;
            devAlert.C_Value = C_Value;
            devAlert.D_CreateOn = DateTime.Now;
            var content = await _repository.AddDevAlert(devAlert);

            if (content)
            {
                //报警通知  推送小程序
                List<TSYS_User> users = await _repository.GetAllUser();
                List<string> openIds = users.Where(u => u.C_WechatID != null).Select(i => i.C_WechatID).ToList();
                TDEV_DevAlertConfig devAlertConfig = await _repository.GetDevAlertConfig(C_ConfigCode);

                var contentInfo = new
                {
                    thing2 = new { value = "15366278195" ?? "" },
                    time4 = new { value = DateTime.Now.ToString("yyyy-MM-dd hh:mm") },
                    thing5 = new { value = "注意" ?? "" },
                    thing6 = new { value = C_DeviceCode ?? "" },
                    thing9 = new { value = devAlertConfig.C_Name ?? "" }
                };
                new WeChatHelper(_httpClientFactory).PushMessageToUser(openIds, contentInfo);
            }
            return content;
        }
        public async Task<TDEV_Device> GetDeviceInfo(string C_MachineCode)
        {
            var content = await _repository.GetDeviceInfo(C_MachineCode);
            return content;
        }
        public async Task<List<TDEV_Device>> GetAllDeviceInfo()
        {
            var content = await _repository.GetAllDeviceInfo();
            return content;
        }

        public async Task<List<ProductModel>> GetProductInfo(List<epcModel> models)
        {
            var content = await _repository.GetProductInfo(models);
            return content;
        }
        public async Task<List<ProductModel>> GetDevProductInfo(string c_DeviceCode)
        {
            var content = await _repository.GetDevProductInfo(c_DeviceCode);
            return content;
        }

        public async Task<bool> UpOffRackProduct(ProductInfoModel model)
        {
            var content = await _repository.UpOffRackProduct(model);
            return content;
        }

        public async Task<UserModel> GetUserInfo(string openid)
        {
            var content = await _repository.GetUserInfo(openid);
            return content;
        }
    }
}