/* This file is part of AHD ID3 Tag Editor (AITE)
   A program can create and edit ID3 Tag for mp3 files

   Copyright © Ala I.Hadid 2012

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AHD.ID3Tag
{
    /// <summary>
    /// Class for wrapping the common frames without the need to edit "Frames" property in the ID3v2 class
    /// </summary>
    public class ID3v2QuickWrapper
    {
        ID3v2 id3v2;
        public ID3v2QuickWrapper(ID3v2 id3v2)
        {
            this.id3v2 = id3v2;
        }

        public string Title
        {
            get
            {
                TextFrame Tframe = (TextFrame)id3v2.GetFrameLoaded("TIT2");
                if (Tframe != null)
                    return Tframe.Text;
                else
                    return "";
            }
            set
            {
                if (value.Length > 0)
                {
                    TextFrame Tframe = (TextFrame)id3v2.GetFrameLoaded("TIT2");
                    if (Tframe != null)
                        Tframe.Text = value;
                    else
                    {
                        TextFrames.TIT2 frame = new TextFrames.TIT2();
                        frame.Text = value;
                        id3v2.Frames.Add(frame);
                    }
                }
                else
                {
                    id3v2.RemoveFrame("TIT2");
                }
            }
        }
        public string Artist
        {
            get
            {
                TextFrame Tframe = (TextFrame)id3v2.GetFrameLoaded("TPE1");
                if (Tframe != null)
                    return Tframe.Text;
                else
                    return "";
            }
            set
            {
                if (value.Length > 0)
                {
                    TextFrame Tframe = (TextFrame)id3v2.GetFrameLoaded("TPE1");
                    if (Tframe != null)
                        Tframe.Text = value;
                    else
                    {
                        TextFrames.TPE1 frame = new TextFrames.TPE1();
                        frame.Text = value;
                        id3v2.Frames.Add(frame);
                    }
                }
                else
                {
                    id3v2.RemoveFrame("TPE1");
                }
            }
        }
        public string Album
        {
            get
            {
                TextFrame Tframe = (TextFrame)id3v2.GetFrameLoaded("TALB");
                if (Tframe != null)
                    return Tframe.Text;
                else
                    return "";
            }
            set
            {
                if (value.Length > 0)
                {
                    TextFrame Tframe = (TextFrame)id3v2.GetFrameLoaded("TALB");
                    if (Tframe != null)
                        Tframe.Text = value;
                    else
                    {
                        TextFrames.TALB frame = new TextFrames.TALB();
                        frame.Text = value;
                        id3v2.Frames.Add(frame);
                    }
                }
                else
                {
                    id3v2.RemoveFrame("TALB");
                }
            }
        }
        public string Genre
        {
            get
            {
                TextFrame Tframe = (TextFrame)id3v2.GetFrameLoaded("TCON");
                if (Tframe != null)
                    return Tframe.Text;
                else
                    return "";
            }
            set
            {
                if (value.Length > 0)
                {
                    TextFrame Tframe = (TextFrame)id3v2.GetFrameLoaded("TCON");
                    if (Tframe != null)
                        Tframe.Text = value;
                    else
                    {
                        TextFrames.TCON frame = new TextFrames.TCON();
                        frame.Text = value;
                        id3v2.Frames.Add(frame);
                    }
                }
                else
                {
                    id3v2.RemoveFrame("TCON");
                }
            }
        }
        public string Track
        {
            get
            {
                TextFrame Tframe = (TextFrame)id3v2.GetFrameLoaded("TRCK");
                if (Tframe != null)
                    return Tframe.Text;
                else
                    return "";
            }
            set
            {
                if (value.Length > 0)
                {
                    TextFrame Tframe = (TextFrame)id3v2.GetFrameLoaded("TRCK");
                    if (Tframe != null)
                        Tframe.Text = value;
                    else
                    {
                        TextFrames.TRCK frame = new TextFrames.TRCK();
                        frame.Text = value;
                        id3v2.Frames.Add(frame);
                    }
                }
                else
                {
                    id3v2.RemoveFrame("TRCK");
                }
            }
        }
        public string Year
        {
            get
            {
                TextFrame Tframe = (TextFrame)id3v2.GetFrameLoaded("TYER");
                if (Tframe != null)
                    return Tframe.Text;
                else
                    return "";
            }
            set
            {
                if (value.Length > 0)
                {
                    TextFrame Tframe = (TextFrame)id3v2.GetFrameLoaded("TYER");
                    if (Tframe != null)
                        Tframe.Text = value;
                    else
                    {
                        TextFrames.TYER frame = new TextFrames.TYER();
                        frame.Text = value;
                        id3v2.Frames.Add(frame);
                    }
                }
                else
                {
                    id3v2.RemoveFrame("TYER");
                }
            }
        }
        public string Comment
        {
            get
            {
                CommentsFrame Tframe = (CommentsFrame)id3v2.GetFrameLoaded("COMM");
                if (Tframe != null)
                    return Tframe.Text;
                else
                    return "";
            }
            set
            {
                if (value.Length > 0)
                {
                    CommentsFrame Tframe = (CommentsFrame)id3v2.GetFrameLoaded("COMM");
                    if (Tframe != null)
                        Tframe.Text = value;
                    else
                    {
                        CommentsFrames.COMM frame = new CommentsFrames.COMM();
                        frame.Language = "English [eng]";
                        frame.Text = value;
                        id3v2.Frames.Add(frame);
                    }
                }
                else
                {
                    id3v2.RemoveFrameOnce("COMM");
                }
            }
        }
        public byte Rating
        {
            get
            {
                ID3v2Frame frame = id3v2.GetFrameLoaded("POPM");
                if (frame != null)
                    return ((PopularimeterFrame)frame).Rating;
                else
                    return 0;
            }
            set
            {
                ID3v2Frame frame = id3v2.GetFrameLoaded("POPM");
                if (frame != null)
                    ((PopularimeterFrame)frame).Rating = value;
                else
                {
                    PopularimeterFrames.POPM newFrame = new PopularimeterFrames.POPM();
                    newFrame.Counter = 0;
                    newFrame.Rating = value;
                    id3v2.Frames.Add(newFrame);
                }
            }
        }
        /// <summary>
        /// Clear the fields but not the file
        /// </summary>
        public void Clear()
        {
            Title = Artist = Album = Genre = Track = Year = Comment = "";
            Rating = 0;
        }
    }
}
