using System;
using System.Net;
using Telegram.Bot;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.ReplyMarkups;

namespace Monithor.api.Alerting {
	public class ZulipWebhookTarget : ITarget {
		public ZulipWebhookTarget(string domain, string apikey, IMonitor monitor, string displayName, string stream, string topic = "monithor-alerting") {
			_domain      = domain;
			_apikey      = apikey;
			_stream      = stream;
			_topic       = topic;
			Monitor      = monitor;
			_displayName = displayName;
		}

		private readonly string   _domain;
		private readonly string   _apikey;
		private readonly string   _stream;
		private readonly string   _topic;
		public           IMonitor Monitor { get; set; }
		private readonly string   _displayName;

		public void SendMessage() {
			var message = General.GetMessage(Monitor, _displayName);
			var json    = $"{{ \"text\": \"{message}\" }}".Replace("\n", "\\n");
			try {
				var client = new WebClient();
				client.Headers.Add("Content-Type", "application/json");
				client.UploadString($"https://{_domain}/api/v1/external/slack_incoming?api_key={_apikey}&stream={_stream}&topic={_topic}", json);
			}
			catch (Exception e) {
				Console.WriteLine(e);
			}
		}
	}
}