/* 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
{
    public class TextFrame : ID3v2Frame
    {
        public TextFrame(string name)
            : base("????", 0, 0, null)
        { }
        public TextFrame(string id, int size, ushort flags, byte[] data)
            : base(id, size, flags, data)
        { }

        string text = "";
        Encoding encoding = ID3v2EncodingWrapper.ISO_8859_1;
        void DetectEncoding(string val)
        {
            if (encoding.CodePage == ID3v2EncodingWrapper.ISO_8859_1.CodePage)
                encoding = ID3v2EncodingWrapper.GetBestEncoding(val);
        }
        public override string Name
        {
            get { return ""; }
        }
        public virtual string Text
        {
            get { return text; }
            set { text = value; DetectEncoding(value); }
        }
        public virtual Encoding Encoding
        { get { return encoding; } set { encoding = value; } }
        public override int GetSize()
        {
            return 1 + encoding.GetByteCount(text);
        }
        public override void Load()
        {
            encoding = ID3v2EncodingWrapper.FromByte(Data[0]);
            text = encoding.GetString(Data, 1, Data.Length - 1);
        }
        public override void Save()
        {
            List<byte> data = new List<byte>();
            //encoding
            data.Add(ID3v2EncodingWrapper.ToByte(encoding));
            //data
            data.AddRange(encoding.GetBytes(text));
            //save
            this.Data = data.ToArray();
        }
        public virtual TextFrameType FrameType
        { get { return TextFrameType.Default; } }
    }
    public enum TextFrameType
    {
        /// <summary>
        /// This frame is normal string
        /// </summary>
        Default,
        /// <summary>
        /// Number represented as string
        /// </summary>
        NumericalString,
        /// <summary>
        /// numeric string in the DDMM format containing the date. 
        /// </summary>
        DDMM,
        /// <summary>
        /// This is a frame with known types like genre, language... should be edited with editable text combobox
        /// </summary>
        TypeOfTypesEditable,
        /// <summary>
        /// This is a frame with known types like file types... should be edited with none-editable text combobox
        /// </summary>
        TypeOfTypesNotEditable,
        /// <summary>
        /// numeric string in the HHMM format containing the time. 
        /// </summary>
        HHMM,
        /// <summary>
        /// numeric string in the year format containing the year. 
        /// </summary>
        Year,
        /// <summary>
        /// The value may be extended with a "/" character and a numeric string containing the 
        /// total number of parts in the set. E.g. "1/2".
        /// </summary>
        PositionOfSet
    }
}
