using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System;
using System.Composition;
using System.Configuration;
using System.Drawing;
using TagLib;

namespace songrequests_blazor.Data
{
    public class Song
    {
        public int Id { get; set; }

        public string Uri { get; set; } = null!;

        // Tag-Parameter
        public string Title { get; set; } = null!;
        public string? Artist { get; set; }
        public string? Album { get; set; }
        public uint? Year { get; set; }
        public string? Genre { get; set; }
        public long Length { get; set; }
        public byte[]? Cover { get; set; }


        // Statistics
        public int RequestCount { get; set; }

        public int Library_ID { get; set; }
        public virtual Library Library { get; set; } = null!;

        public virtual List<QueuedSong>? Queued { get; set; }

        public static Song FromFile(string filename)
        {
            var mp3file = TagLib.File.Create(filename);
            Song song = new();
            song.Uri = filename;
            song.Title = mp3file.Tag.Title;
            song.Artist = mp3file.Tag.FirstPerformer;
            song.Album = mp3file.Tag.Album;
            if (mp3file.Tag.Year > 0) song.Year = mp3file.Tag.Year;
            song.Genre = mp3file.Tag.FirstGenre;
            song.Length = mp3file.Length;
            song.Cover = mp3file.Tag.Pictures.Where(p => p.Type == PictureType.FrontCover).FirstOrDefault()?.Data.ToArray();
            return song;
        }

    }
}
