using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ID3TagLib;

namespace MP3Tag
{
    public partial class Form1 : Form
    {
        #region Version und Copyright
        // Version und Copyright
        string programName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //Den Programmnamen auslesen
        string programVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
        static object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
        string copyright = GenerateCopyright();
        string icon = Application.StartupPath + "\\Info.bmp";
        //Assembly Datum und Zeit
        static DateTime value = AssemblyDateTime();
        string date = value.ToShortDateString();
        string time = value.ToLongTimeString();
        private static DateTime AssemblyDateTime()
        {
            var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            var buildDateTime = new DateTime(2000, 1, 1).Add(new TimeSpan(TimeSpan.TicksPerDay * version.Build + TimeSpan.TicksPerSecond * 2 * version.Revision));
            //Tage seit dem 1. Januar 2000 und Sekunden seit Mitternacht, (multiplizier mit 2 ergibt das Original)
            return buildDateTime;
        }

        private static string CurrentYear()
        {
            DateTime dtn = DateTime.Now;
            return dtn.Year.ToString();
        }

        private static string StartYear()
        {
            //string startYear = ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            //return startYear.Remove(0, startYear.LastIndexOf(" ") + 1);
            string startYear = "";
            if (((AssemblyCopyrightAttribute)attributes[0]).Copyright.Contains("Copyright © "))
            {
                startYear = ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace("Copyright © ", "");
                while (startYear.StartsWith(" "))
                {
                    startYear = startYear.Remove(0, 1);
                }
                startYear = startYear.Remove(startYear.IndexOf(" "));
                return startYear;
            }
            else
            {
                DateTime dtn = DateTime.Now;
                return dtn.Year.ToString();
            }
        }

        private static string GenerateCopyright()
        {
            string startYear = StartYear();
            string currentYear = CurrentYear();

            if (startYear == currentYear)
            {
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
            else
            {
                //return String.Concat(((AssemblyCopyrightAttribute)attributes[0]).Copyright, "-", currentYear);
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace(startYear, String.Concat(startYear, "-", currentYear));
            }
        }
        #endregion

        #region Programm-Info
        /// <summary>
        /// Contains the name of the program file
        /// </summary>
        public static string ProgramName { set; get; }

        /// <summary>
        /// Contains the version of the program file
        /// </summary>
        public static string ProgramVersion { set; get; }

        /// <summary>
        /// Contains the copyright notice
        /// </summary>
        public static string Copyright { set; get; }

        private void SetProgramInfo()
        {
            //Name, Version und Copyright setzen
            ProgramName = programName;
            ProgramVersion = programVersion;
            Copyright = copyright;
        }
        #endregion

        public Form1()
        {
            SetProgramInfo(); //Name, Version und Copyright setzen

            InitializeComponent();
        }
		
		const string projectDataPath = @"C:\CAD\Git-WorkRepository\VisualStudio\_PROJEKTE\MP3Tag.git\Daten\";

        private void buttonStart_Click(object sender, EventArgs e)
        {
            Start();
        }

        private void Start()
        {
            //Tags aus der Datei laden
            ID3File file = new ID3File(projectDataPath + "01 Palo Pa Rumba.mp2");

            //Werte auslesen
            ReadTag();

            //Werte schreiben
            WriteTag();

            //Werte auslesen
            ReadTag();
        }

        private void WriteTag()
        {
            //Werte schreiben
            ID3File file = new ID3File(projectDataPath + "01 Palo Pa Rumba.mp2");

            ID3v1Tag v1tag = file.ID3v1Tag;

            if (v1tag == null)
            {
                v1tag = new ID3v1Tag();
                file.ID3v1Tag = v1tag;

                v1tag.Album = "Mein Album";
                v1tag.Artist = "Spanischer Sänger";
                v1tag.Title = "01 Palo Pa Rumba";
                v1tag.TrackNumber = "1";
                v1tag.Genre = ID3v1Genre.Samba;
                v1tag.Year = "2018";
                v1tag.Comment = "Test";

                file.Save(projectDataPath + "01 Palo Pa Rumba.mp2");
            }
        }

        private void ReadTag()
        {
            string album = "";
            string artist = "";
            string title = "";
            string trackNumber = "";
            string genre = null;
            string year = "";
            string comment = "";

            //Tags aus der Datei laden
            ID3File file = new ID3File(projectDataPath + "01 Palo Pa Rumba.mp2");

            //Werte auslesen
            ID3v1Tag v1tag = file.ID3v1Tag;
            if (v1tag != null)
            {
                album = v1tag.Album;
                artist = v1tag.Artist;
                title = v1tag.Title;
                trackNumber = v1tag.TrackNumber;
                genre = v1tag.Genre.ToString();
                year = v1tag.Year;
                comment = v1tag.Comment;
            }

            //ID3v2Tag v2tag = file.ID3v2Tag;
            //if (v2tag != null)
            //{
            //    PictureFrame pf = FrameFactory.GetFrame(FrameFactory.AttachedPictureFrameId) as PictureFrame;
            //    v2tag.Frames.Add(pf);
            //}

            ShowTag(album, artist, title, trackNumber, genre, year, comment);
        }

        private void ShowTag(string album, string artist, string title, string trackNumber, string genre, string year, string comment)
        {
            labelTag.Text = String.Concat("ID1Tag:\nAlbum:\nArtist:\nTitle:\nTrack No:\nGenre:\nYear:\nComment:");
            labelTagValue.Text = String.Concat("\n", album, "\n", artist, "\n", title, "\n", trackNumber, "\n", genre, "\n", year, "\n", comment);
        }
    }
}
