/* 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 AttachedPictureFrame : ID3v2Frame
    {
        public AttachedPictureFrame()
            : base("APIC", 0, 0, null)
        { }
        public AttachedPictureFrame(int size, ushort flags, byte[] data)
            : base("APIC", size, flags, data)
        { }
        public override string Name
        {
            get { return "Attached picture"; }
        }
        string mime = "";
        int type = 0;
        string desc = "";
        byte[] picData = new byte[0];
        Encoding encoding = ID3v2EncodingWrapper.ISO_8859_1;
        void DetectEncoding(string val)
        {
            if (encoding.CodePage == ID3v2EncodingWrapper.ISO_8859_1.CodePage)
                encoding = ID3v2EncodingWrapper.GetBestEncoding(val);
        }
        public virtual Encoding Encoding
        { get { return encoding; } set { encoding = value; } }
        public string MIME
        {
            get { return mime; }
            set { mime = value; }
        }
        public string Description
        {
            get { return desc; }
            set
            {
                desc = value;
                DetectEncoding(value);
            }
        }

        public string PictureType
        {
            get { return Consts.PictureTypes[type]; }
            set
            {
                for (int i = 0; i < Consts.PictureTypes.Length; i++)
                {
                    if (Consts.PictureTypes[i] == value)
                    {
                        type = i; break;
                    }
                }
            }
        }
        public byte[] PictureData
        { get { return picData; } set { picData = value; } }
        public override void Load()
        {
            encoding = ID3v2EncodingWrapper.FromByte(Data[0]);
            //read mime
            string code = ID3v2EncodingWrapper.ISO_8859_1.GetString(Data, 1, Data.Length - 1);
            string[] Ltext = code.Split(new char[] { '\0' });
            int index = 1;
            mime = Ltext[0]; index += mime.Length + 1;
            //type
            type = Data[index]; index++;
            //description (according to encoding)
            Ltext = encoding.GetString(Data, index, Data.Length - index).Split(new char[] { '\0' });

            desc = Ltext[0]; index += encoding.GetByteCount(desc + "\0");
            //picture data
            picData = new byte[Data.Length - index];
            for (int i = index; i < Data.Length - index; i++)
            {
                picData[i - index] = Data[i];
            }
        }
        public override int GetSize()
        {
            return 1 + ID3v2EncodingWrapper.ISO_8859_1.GetByteCount(mime + 1) + 1 + encoding.GetByteCount(desc + "\0")
                + picData.Length;
        }
        public override void Save()
        {
            List<byte> data = new List<byte>();
            //encoding
            data.Add(ID3v2EncodingWrapper.ToByte(encoding));
            //mime
            data.AddRange(ID3v2EncodingWrapper.ISO_8859_1.GetBytes(mime + "\0"));
            //type
            data.Add((byte)type);
            //desc
            data.AddRange(encoding.GetBytes(desc + "\0"));
            //pic data
            data.AddRange(picData);
            //save
            this.Data = data.ToArray();
        }

        public override string ToString()
        {
            return desc + " [" + PictureType + "]";
        }
    }
}
