using Microsoft.Extensions.DependencyInjection;
using SlnMesnac.Plc;
using SlnMesnac.Rfid;
using SlnMesnac.Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2024 WenJY 保留所有权利。
* CLR版本：4.0.30319.42000
* 机器名称：LAPTOP-E0N2L34V
* 命名空间：SlnMesnac.Business.base
* 唯一标识：b00d95c1-a164-43a3-9f34-2a5d2efb3f34
*
* 创建者：WenJY
* 电子邮箱：wenjy@mesnac.com
* 创建时间：2024-04-12 17:36:19
* 版本：V1.0.0
* 描述：
*
*--------------------------------------------------------------------
* 修改人：
* 时间：
* 修改说明：
*
* 版本：V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.Business.@base
{
    internal class BaseBusiness
    {
        private readonly List<PlcAbsractFactory> _plcFactories;

        private readonly List<RfidAbsractFactory> _rfidFactories;

        private IServiceProvider _serviceProvider;

        public BaseBusiness(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;

            using (var scope = _serviceProvider.CreateScope())
            {
                _plcFactories = scope.ServiceProvider.GetRequiredService<List<PlcAbsractFactory>>(); 
                _rfidFactories = scope.ServiceProvider.GetRequiredService<List<RfidAbsractFactory>>(); 
            }
        }

        /// <summary>
        /// 根据Key获取PLC连接信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        public PlcAbsractFactory GetPlcByKey(string key)
        {
            if(_plcFactories == null)
            {
                throw new ArgumentNullException($"根据Key获取PLC连接信息异常:PLC 连接信息为空");
            }

            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("根据Key获取PLC连接信息异常:设备Key参数为空");
            }

            try
            {
                var info = _plcFactories.Where(x => x.ConfigKey == key).FirstOrDefault();

                return info;
            }catch(Exception ex)
            {
                throw new InvalidOperationException($"根据Key获取PLC连接信息异常:{ex.Message}");
            }
        }


        /// <summary>
        /// 根据Key获取Rfid连接信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        public RfidAbsractFactory GetRfidByKey(string key)
        {
            if (_rfidFactories == null)
            {
                throw new ArgumentNullException($"根据Key获取RFID连接信息异常:PLC 连接信息为空");
            }

            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("根据Key获取RFID连接信息异常:设备Key参数为空");
            }

            try
            {
                var info = _rfidFactories.Where(x => x.ConfigKey == key).FirstOrDefault();

                return info;
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"根据Key获取RFID连接信息异常:{ex.Message}");
            }
        }
    }
}
