using System.Web;
using Tesses.YouTubeDownloader;
using YoutubeExplode;
using YoutubeExplode.Videos;
string[] mos = new string[]{"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
string[] Mos = new string[]{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
if(args.Length < 3)
{
    Console.WriteLine("USAGE: removed_php_gen removed.txt tytddir phpdir");
    Console.WriteLine("WARNING: wont automaticly edit index.php");
}
else
{
    TYTDPathDirectory tYTDPathDirectory=new TYTDPathDirectory(args[1]);
    DateTime dt=DateTime.Now;
    Directory.CreateDirectory(args[2]);
    /*
    <?php
    include("header-1.php");
?>
<title>Removed as of Oct 31, 2023</title>
<?php
    include("header-2.php");
?>
<h1>Removed as of Oct 31, 2023</h1>
<ul>*/


    
    string filename = Path.Combine(args[2],$"removed-{mos[dt.Month-1]}{dt.Day.ToString("D2")}{dt.Year.ToString("D4")}.php");
    using(var sw = File.CreateText(filename))
    {
        await sw.WriteLineAsync("<?php include(\"header-1.php\"); ?>");
        await sw.WriteLineAsync($"<title>Removed as of {Mos[dt.Month-1]} {dt.Day}, {dt.Year}</title>");
        await sw.WriteLineAsync("<?php include(\"header-2.php\"); ?>");
        await sw.WriteLineAsync($"<h1>Removed as of {Mos[dt.Month-1]} {dt.Day}, {dt.Year}</h1>");
        await sw.WriteLineAsync("<ul>");
        await foreach(var id in File.ReadLinesAsync(args[0]))
        {
            VideoId? v = VideoId.TryParse(id);
            if(v.HasValue)
            {
                if(await tYTDPathDirectory.VideoInfoExistsAsync(v.Value))
                {
                    string title = (await tYTDPathDirectory.GetVideoInfoAsync(v.Value) ?? new SavedVideo{Title=""}).Title;
                    await sw.WriteLineAsync($"<li><a href=\"./watch.php?v={v.Value.Value}\">{HttpUtility.HtmlEncode(title)}</a></li>");
                    Console.WriteLine($"Appended: {title} with Id: {v.Value}");
                }
            }
        }
        await sw.WriteLineAsync("</ul>");
        await sw.WriteLineAsync("<?php include(\"footer.php\"); ?>");
    }
    Console.WriteLine($"Writen to file: {filename}");
    Console.WriteLine();
    Console.WriteLine("Place this within the <ul> tag under <h1>Removed Lists</h1> right before the </ul>");
    Console.WriteLine("<!-- COPY THE TEXT BETWEEN THESE COMMENTS -->");
    Console.WriteLine($"<li><a href=\"removed-{mos[dt.Month-1]}{dt.Day.ToString("D2")}{dt.Year.ToString("D4")}\">{Mos[dt.Month-1]} {dt.Day}, {dt.Year}</a></li>");
    Console.WriteLine("<!-- COPY THE TEXT BETWEEN THESE COMMENTS -->");

}
