using HTCoreServiceApp.Common;
using HTCoreServiceApp.Models;
using Microsoft.Extensions.Logging;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace HTCoreServiceApp
{
	public class DriverService
	{
		private readonly ILogger<DriverService> logger;
		public List<DriverThread> DriverThreads = new List<DriverThread>();
		public bool Init { get; set; }
		public DriverService()
		{
			//try
			//{
			//    var drivers = StaticLibrary.DBContext.MetaDrivers.ToList();
			//    foreach(var driver in drivers) 
			//    {
			//        CreateDriverThread(driver);
			//    }


			//}
			//catch (Exception ex)
			//{
			//    //logger.LogError($"获取设备出错", ex);
			//}
			DriverServiceInit();
		}

		public void DriverServiceInit()
		{
			Task.Run(() =>
			{
				DriverServiceAsyncInit();
			});
		}
		public void DriverServiceAsyncInit()
		{
			try
			{
				//string ConnectionStrings = AESKeyEncrypt.DecryptDES(ConfigHelper.ReadAppSettings("ConnectionStrings", "SqlServer"));
				var db = new SqlSugarClient(StaticLibrary.DBConfig);
				var drivers = db.Queryable<MetaDriver>().ToList();  //db.MetaDrivers.ToList();
				foreach (var driver in drivers)
				{
					CreateDriverThread(driver);
				}
				db.Dispose();


			}
			catch (Exception ex)
			{
				Console.WriteLine($"DriverServiceInit错误{ex.Message}");
				LogHelper.LogWrite($"DriverServiceInit错误{ex.Message}");
				//logger.LogError($"获取设备出错", ex);
			}
		}

		public void CreateDriverThread(MetaDriver metaDriver)
		{
			//logger.LogInformation($"CreateDeviceThread 开始:{metaDriver.DriverName}");
			DriverThreads.Add(new DriverThread(metaDriver));
		}
		public void CreateDriverThreads(List<MetaDriver> metaDrivers)
		{
			foreach (MetaDriver driver in metaDrivers)
				CreateDriverThread(driver);
		}

		public void RemoveDriverThread(MetaDriver metaDriver)
		{
			var driverThread = DriverThreads.FirstOrDefault(x => x.MetaDriver.DriverId == metaDriver.DriverId);
			if (driverThread != null)
			{
				driverThread.StopThread();
				driverThread.Dispose();
				DriverThreads.Remove(driverThread);
			}
		}
		public void RemoveDriverThreads(List<MetaDriver> metaDrivers)
		{
			foreach (MetaDriver driver in metaDrivers)
				RemoveDriverThread(driver);
		}

		public void UpdateDriver(MetaDriver metaDriver)
		{
			try
			{
				logger.LogInformation($"UpdateDevice 开始:{metaDriver.DriverName}");
				LogHelper.LogWrite($"UpdateDevice 开始:{metaDriver.DriverName}");
				RemoveDriverThread(metaDriver);
				CreateDriverThread(metaDriver);
				logger.LogInformation($"UpdateDevice 结束:{metaDriver.DriverName}");
				LogHelper.LogWrite($"UpdateDevice 结束:{metaDriver.DriverName}");
			}
			catch (Exception ex)
			{
				logger.LogError($"UpdateDevice 出错:{metaDriver.DriverName}", ex);
			}
		}

		public void Updatedrivers(List<MetaDriver> metaDrivers)
		{
			foreach (var driver in metaDrivers)
				UpdateDriver(driver);
		}
	}
}
