#define TRACE
#define DEBUG
using System;
using System.Diagnostics;
using System.IO;
using System.Management;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace qk30ic;

public static class KEEPALIVE
{
	public class AutoClosingMessageBox
	{
		private int text_max_length = 200;

		private System.Threading.Timer _timeoutTimer;

		private string _caption;

		private const int WM_CLOSE = 16;

		private AutoClosingMessageBox(string text, string caption, int timeout)
		{
			if (text.Length > text_max_length)
			{
				text = text.Substring(0, text_max_length);
			}
			_caption = caption;
			_timeoutTimer = new System.Threading.Timer(OnTimerElapsed, null, timeout, -1);
			MessageBox.Show(text, caption, MessageBoxButtons.OK, MessageBoxIcon.Hand);
		}

		public static void Show(string text, string caption, int timeout)
		{
			new AutoClosingMessageBox(text, caption, timeout);
		}

		private void OnTimerElapsed(object state)
		{
			IntPtr intPtr = FindWindow(null, _caption);
			if (intPtr != IntPtr.Zero)
			{
				SendMessage(intPtr, 16u, IntPtr.Zero, IntPtr.Zero);
			}
			_timeoutTimer.Dispose();
		}

		[DllImport("user32.dll", SetLastError = true)]
		private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

		[DllImport("user32.dll", CharSet = CharSet.Auto)]
		private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
	}

	public static System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

	public static void TIM_Main()
	{
		timer.Tick += MyTimer;
		timer.Interval = Convert.ToInt32(Form1.str_Confile[11]) * 1000;
		timer.Enabled = true;
	}

	public static void MyTimer_stop()
	{
		timer.Enabled = false;
	}

	public static void MyTimer_start()
	{
		timer.Enabled = true;
	}

	public static void MyTimer(object sender, EventArgs e)
	{
		try
		{
			MyTimer_stop();
			DateTime now = DateTime.Now;
			string text = "";
			string text2 = "";
			string text3 = "";
			string text4 = "";
			string text5 = "";
			Process currentProcess = Process.GetCurrentProcess();
			ManagementClass managementClass = new ManagementClass("Win32_OperatingSystem");
			ManagementObjectCollection instances = managementClass.GetInstances();
			foreach (ManagementObject item in instances)
			{
				text3 = string.Format("{0:#,#}", item["FreePhysicalMemory"]);
				item.Dispose();
			}
			instances.Dispose();
			managementClass.Dispose();
			text2 = $"{currentProcess.WorkingSet64 / 1000:#,#}";
			DriveInfo driveInfo = new DriveInfo("C");
			text4 = $"{driveInfo.TotalFreeSpace / 1024:#,#}";
			text = "使用メモリ:[" + text2 + "]KB   空きメモリ:[" + text3 + "]KB   空きHDD容量:[" + text4 + "]KB";
			if (Form1.str_Confile[15] == "1")
			{
				text5 = "INSERT INTO hardware_check_log (device_id, status, status_comment, created_at) VALUES (" + Form1.KpAlive_devid + ",2,'" + text + "','" + now.ToString() + "');";
				Debug.WriteLine("キープアライブ追記MODE：" + text5);
			}
			else
			{
				text5 = "update hardware_check_log  set status='1',status_comment='" + text + "',updated_at= '" + now.ToString() + "' where device_id = " + Form1.KpAlive_devid + " ORDER BY created_at DESC LIMIT 1;";
				Debug.WriteLine("キープアライブ更新MODE：" + text5);
			}
			Form1.SO_DBKpAlive(text5);
			Random random = new Random();
			timer.Interval = Convert.ToInt32(Form1.str_Confile[11]) * 1000 + random.Next(60) * 1000;
			Form1.SO_LogFileDelCheck();
			MyTimer_start();
		}
		catch (Exception ex)
		{
			Debug.WriteLine("★★_ERROR_29");
			Debug.WriteLine("キープアライブエラー");
			Trace.WriteLine(ex.Message);
			MyTimer_start();
		}
	}
}
