/* 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 TermsOfUseFrame : ID3v2Frame
    {
        public TermsOfUseFrame()
            : base("USER", 0, 0, null)
        { }
        public TermsOfUseFrame(int size, ushort flags, byte[] data)
            : base("USER", size, flags, data)
        { }
        public override string Name
        {
            get { return "Terms of use frame"; }
        }
        string language = "";
        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 string Language
        {
            get
            {
                foreach (string lang in Consts.Languages)
                {
                    if (lang.Substring(lang.Length - 4, 3) == language.ToLower())
                        return lang;
                }
                return language;
            }
            set
            {
                language = value.Substring(value.Length - 4, 3).ToUpper();
            }
        }
        public string Text
        {
            get { return text; }
            set { text = value; DetectEncoding(value); }
        }
        public virtual Encoding Encoding
        { get { return encoding; } set { encoding = value; } }

        public override void Load()
        {
            encoding = ID3v2EncodingWrapper.FromByte(Data[0]);
            string code = ID3v2EncodingWrapper.ISO_8859_1.GetString(Data, 1, Data.Length - 1);
            language = code.Substring(0, 3);

            text = encoding.GetString(Data, 4, Data.Length - 4);
        }

        public override int GetSize()
        {
            return 4 + encoding.GetByteCount(text);
        }
        public override void Save()
        {
            List<byte> data = new List<byte>();
            //encoding
            data.Add(ID3v2EncodingWrapper.ToByte(encoding));
            //language "id"
            data.AddRange(ID3v2EncodingWrapper.ISO_8859_1.GetBytes(language.ToUpper()));
            //text
            data.AddRange(encoding.GetBytes(text));
            //save
            this.Data = data.ToArray();
        }
    }
}
