using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using c3stream.DataModels;
using LinqToDB.Data;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;

namespace c3stream;

public static class c3stream {
	public const  string DataPath  = "data";
	public const  string DbFile    = "c3stream.sqlite";
	public const  string LogoPath  = "/mnt/nvme-data/c3stream-logos/";
	public const  string LogoUrl   = "https://mirror.c3stream.de/logos/";
	public const  string CachePath = "/mnt/storage/archive/Video/congress/";
	public const  string CacheUrl  = "https://mirror.c3stream.de/";
	public static object Lock      = new();
	public static string DbPath    = Path.Combine(DataPath, DbFile);

	public static readonly List<ConferenceObject> Conferences = new() {
		new ConferenceObject("rc3-2021", true),
		new ConferenceObject("rc3"),
		new ConferenceObject("36c3"),
		new ConferenceObject("camp2019"),
		new ConferenceObject("gpn19"),
		new ConferenceObject("35c3"),
		new ConferenceObject("34c3"),
		new ConferenceObject("33c3"),
		new ConferenceObject("32c3"),
		new ConferenceObject("31c3"),
		new ConferenceObject("30c3")
	};

	public static void Main(string[] args) {
		if (!Directory.Exists(DataPath))
			Directory.CreateDirectory(DataPath);
		if (!File.Exists(DbPath))
			File.Copy(Path.Combine(DataPath, "database.init.sqlite"), DbPath);

		DataConnection.DefaultSettings = new Database.Settings();
		Migrations.RunMigrations();

		foreach (var conference in Conferences)
			UpdateConference(conference);

		if (args.Length != 0) {
			if (args[0] == "logo")
				foreach (var conference in Conferences)
					Console.WriteLine($"wget {conference.LogoUri} -O {Path.Combine(LogoPath, conference.Acronym + ".png")}");
			else if (Conferences.All(p => p.Acronym != args[0]))
				Console.WriteLine("No matching conference found.");
			else
				foreach (var talk in Conferences.First(p => p.Acronym == args[0]).Talks)
					Console.WriteLine($"youtube-dl -f \"best[ext = mp4]\" {talk.FrontendLink} -o \"{Path.Combine(CachePath, args[0], talk.Slug)}.mp4\"");
		}
		else {
			CreateHostBuilder(args).Build().Run();
		}
	}

	//TODO: move this to the database as well
	public static void UpdateConference(ConferenceObject conference) {
		using var httpc = new HttpClient();

		var jsonpath = Path.Combine(DataPath, conference.Acronym + "_index.json");
		var json     = "";
		if (!File.Exists(jsonpath)) {
			json = httpc.GetStringAsync($"https://api.media.ccc.de/public/conferences/{conference.Acronym}").Result;
			File.WriteAllText(jsonpath, json);
		}
		else if (conference.Ongoing) {
			json = httpc.GetStringAsync($"https://api.media.ccc.de/public/conferences/{conference.Acronym}").Result;
		}
		else {
			json = File.ReadAllText(jsonpath);
		}

		var parsed = Conference.FromJson(json);
		lock (Lock) {
			conference.Talks.Clear();
			conference.LogoUri = parsed.LogoUrl.AbsoluteUri;
			conference.Talks.AddRange(parsed.Events);
			conference.Talks.ForEach(p => p.Guid = p.Guid.Trim());
		}
	}

	public static string UpdateCookie(HttpRequest request, HttpResponse response, string redirectUri) {
		var cookie = "";
		//if new bookmark is in uri
		if (request.Query.ContainsKey("bookmark") && Guid.TryParseExact(request.Query["bookmark"], "D", out _)) {
			response.Cookies.Append("bookmark", request.Query["bookmark"], new CookieOptions { Expires = DateTimeOffset.MaxValue });
			cookie = request.Query["bookmark"];
		}
		//if no cookie exists or cookie is invalid
		else if (!request.Cookies.ContainsKey("bookmark") || !Guid.TryParseExact(request.Cookies["bookmark"], "D", out _)) {
			var guid = Guid.NewGuid().ToString();
			response.Cookies.Append("bookmark", guid, new CookieOptions { Expires = DateTimeOffset.MaxValue });
			cookie = guid;
		}
		else {
			cookie = request.Cookies["bookmark"];
		}

		if (request.Query.ContainsKey("bookmark"))
			response.Redirect(redirectUri);

		return cookie;
	}

	public static Event GetEventByGuid(string guid) {
		return Conferences.SelectMany(c => c.Talks.Where(e => e.Guid == guid)).FirstOrDefault();
	}

	public static IEnumerable<Event> GetEventsByGuid(IEnumerable<string> guids) {
		return Conferences.SelectMany(c => c.Talks.Where(e => guids.Contains(e.Guid)));
	}

	public static ConferenceObject GetConferenceByEventGuid(string guid) {
		return Conferences.FirstOrDefault(c => c.Talks.Any(t => t.Guid == guid));
	}

	public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });

	public class ConferenceObject {
		public string      Acronym;
		public string      LogoUri;
		public bool        Ongoing;
		public List<Event> Talks = new();

		public ConferenceObject(string acronym, bool ongoing = false) {
			Acronym = acronym;
			Ongoing = ongoing;
		}
	}
}
