using System.Data;
using Dapper;
using MySql.Data;
using MySql.Data.MySqlClient;
using Tesses.YouTubeDownloader;

/*edit this according to your db and TYTD*/
string username = "root";
string database="tytd";
string password="";
string server="127.0.0.1";
int port=6244;
string dir="/mnt/8TBShare/Videos/TYTD/Wii";
bool created=false;


MySql.Data.MySqlClient.MySqlConnection connection=new MySql.Data.MySqlClient.MySqlConnection($"Server={server}; port={port}; database={database}; UID={username}; password={password}");
await connection.OpenAsync();
if(!created){
MySqlCommand command = new MySqlCommand("CREATE TABLE VideoId (VideoId varchar(11), Title varchar(100), Description text, Views bigint, Likes bigint, Dislikes bigint, UploadDate bigint, Keywords text, AddDate bigint, AuthorTitle varchar(100), AuthorId varchar(24), TYTDTag text);", connection);
var reader=await command.ExecuteReaderAsync();
 while(reader.Read())
    {
        string someStringFromColumnZero = reader.GetString(0);
        string someStringFromColumnOne = reader.GetString(1);
        Console.WriteLine(someStringFromColumnZero + "," + someStringFromColumnOne);
    }
}else {
string insertInto = "INSERT INTO SavedVideo (Id,Title,Description,Views,Likes,Dislikes,UploadDate,Keywords,AddDate,AuthorTitle,AuthorId,TYTDTag) values (@Id,@Title,@Description,@Views,@Likes,@Dislikes,@UploadDate,@Keywords,@AddDate,@AuthorTitle,@AuthorId,@TYTDTag);";
async Task AddVideoToTable(SavedVideo video)
{
    //string mysqlCommand = $"INSERT INTO SavedVideo ('{MySqlHelper.EscapeString(video.Id)}','{MySqlHelper.EscapeString(video.Title)}','{MySqlHelper.EscapeString(video.Description)}','{video.Views}','{video.Likes}','{video.Dislikes}','{}','{MySqlHelper.EscapeString()}','{new DateTimeOffset(video.AddDate).ToUnixTimeSeconds()}','{MySqlHelper.EscapeString(video.AuthorTitle)}','{MySqlHelper.EscapeString(video.AuthorChannelId)}','{MySqlHelper.EscapeString(video.TYTDTag)}');";
    await connection.ExecuteAsync(insertInto,new{
        Id=video.Id,
        Title=video.Title,
        Description=video.Description,
        Views = video.Views,
        Likes = video.Likes,
        Dislikes=video.Dislikes,
        UploadDate =new DateTimeOffset(video.UploadDate).ToUnixTimeSeconds(),
        Keywords=string.Join(",",video.Keywords),
        AddDate=new DateTimeOffset(video.AddDate).ToUnixTimeSeconds(),
        AuthorTitle = video.AuthorTitle,
        AuthorId = video.AuthorChannelId,
        TYTDTag = video.TYTDTag
    });
}
TYTDPathDirectory directory=new TYTDPathDirectory(dir);
await foreach (var item in directory.GetVideoIdsAsync())
{   string name = $"https://www.youtube.com/watch?v={item}";
    try{
        
        var savedVideo = await directory.GetVideoInfoAsync(item);
        name = $"{savedVideo.Title} - {savedVideo.Id}";
        await AddVideoToTable(savedVideo);
        Console.WriteLine($"{name} Added");

    }catch(Exception ex)
    {
         Console.WriteLine($"{name} Already Added");
        //Console.WriteLine(ex);
        _=ex;
    }
}

}
await connection.CloseAsync();
