using System;
using System.Net.Http;
using System.Windows;
using System.Windows.Input;
using Crysome.Server.Data;

namespace Crysome.Server.ViewModel;

public class SettingsViewModel : ViewModelBase
{
	private readonly MainViewModel _mainVm;

	private readonly AppDataStore _store;

	private bool _telegramEnabled;

	private string _telegramToken;

	private string _telegramChatId;

	public ICommand GoBackCommand { get; }

	public ICommand SaveCommand { get; }

	public string Port { get; set; }

	public string QuicPort { get; set; }

	public string InfoPollInterval { get; set; }

	public string MaxEndpoints { get; set; }

	public string MaxSendFileSizeMB { get; set; }

	public bool NotifyConnect { get; set; }

	public bool NotifyDisconnect { get; set; }

	public bool LogPaused { get; set; }

	public bool LogToFile { get; set; }

	public bool TelegramEnabled
	{
		get
		{
			return _telegramEnabled;
		}
		set
		{
			_telegramEnabled = value;
			OnPropertyChanged("TelegramEnabled");
		}
	}

	public string TelegramToken
	{
		get
		{
			return _telegramToken;
		}
		set
		{
			_telegramToken = value;
			OnPropertyChanged("TelegramToken");
		}
	}

	public string TelegramChatId
	{
		get
		{
			return _telegramChatId;
		}
		set
		{
			_telegramChatId = value;
			OnPropertyChanged("TelegramChatId");
		}
	}

	public ICommand TestTelegramCommand { get; }

	public SettingsViewModel(MainViewModel mainVm, AppDataStore store)
	{
		_mainVm = mainVm;
		_store = store;
		GoBackCommand = new RelayCommand<object>(delegate
		{
			_mainVm.GoToClients();
		});
		SaveCommand = new RelayCommand<object>(delegate
		{
			DoSave();
		});
		TestTelegramCommand = new RelayCommand<object>(delegate
		{
			DoTestTelegram();
		});
		Port = store.Settings.Port.ToString();
		QuicPort = store.Settings.QuicPort.ToString();
		InfoPollInterval = store.Settings.InfoPollInterval.ToString();
		MaxEndpoints = store.Settings.MaxEndpoints.ToString();
		MaxSendFileSizeMB = store.Settings.MaxSendFileSizeMB.ToString();
		NotifyConnect = store.Settings.NotifyConnect;
		NotifyDisconnect = store.Settings.NotifyDisconnect;
		LogPaused = store.Settings.LogPaused;
		LogToFile = store.Settings.LogToFile;
		TelegramEnabled = store.Settings.TelegramEnabled;
		TelegramToken = store.Settings.TelegramToken;
		TelegramChatId = store.Settings.TelegramChatId;
	}

	private async void DoTestTelegram()
	{
		if (string.IsNullOrEmpty(TelegramToken) || string.IsNullOrEmpty(TelegramChatId))
		{
			MessageBox.Show("Please enter Token and Chat ID first.");
			return;
		}
		try
		{
			using HttpClient client = new HttpClient();
			string requestUri = $"https://api.telegram.org/bot{TelegramToken}/sendMessage?chat_id={TelegramChatId}&text=Crysome Server: Test Message";
			await client.GetAsync(requestUri);
			MessageBox.Show("Test message sent! Check your Telegram.");
		}
		catch (Exception ex)
		{
			MessageBox.Show("Telegram Error: " + ex.Message);
		}
	}

	private void DoSave()
	{
		if (int.TryParse(Port, out var result) && result >= 0 && result <= 65535)
		{
			_store.Settings.Port = result;
		}
		if (int.TryParse(QuicPort, out result) && result >= 0 && result <= 65535)
		{
			_store.Settings.QuicPort = result;
		}
		if (int.TryParse(InfoPollInterval, out result) && result >= 500)
		{
			_store.Settings.InfoPollInterval = result;
		}
		if (int.TryParse(MaxEndpoints, out result) && result >= 0)
		{
			_store.Settings.MaxEndpoints = result;
		}
		if (int.TryParse(MaxSendFileSizeMB, out result) && result > 0)
		{
			_store.Settings.MaxSendFileSizeMB = result;
		}
		_store.Settings.NotifyConnect = NotifyConnect;
		_store.Settings.NotifyDisconnect = NotifyDisconnect;
		_store.Settings.LogPaused = LogPaused;
		_store.Settings.LogToFile = LogToFile;
		_store.Settings.TelegramEnabled = TelegramEnabled;
		_store.Settings.TelegramToken = TelegramToken;
		_store.Settings.TelegramChatId = TelegramChatId;
		_store.SaveSettings();
		_mainVm.ClientsVM?.EnqueueStatus("Settings saved. Restart server for port changes.");
	}
}
