/* 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 InvolvedPeopleListFrame : ID3v2Frame
    {
        public InvolvedPeopleListFrame(string name)
            : base("IPLS", 0, 0, null)
        { }
        public InvolvedPeopleListFrame(string id, int size, ushort flags, byte[] data)
            : base(id, size, flags, data)
        { }
        public override string Name
        {
            get { return "Involved people list"; }
        }
        List<InvolvedPeopleItem> items = new List<InvolvedPeopleItem> ();
        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 List<InvolvedPeopleItem> Items
        { get { return items; } set { items = value; } }

        public override int GetSize()
        {
            int size = 1;
            foreach (InvolvedPeopleItem item in items)
            {
                size += encoding.GetByteCount(item.Involvement + "\0" + item.Involvee + "\0");
            }
            return size;
        }

        public override void Load()
        {
            encoding = ID3v2EncodingWrapper.FromByte(Data[0]);
            items.Clear();
            string code = encoding.GetString(Data, 1, Data.Length - 1);
            string[] list = code.Split(new char[] { '\0' });
            for (int i = 0; i < list.Length; i += 2)
            {
                if (i + 1 < list.Length)
                    items.Add(new InvolvedPeopleItem(list[i], list[i + 1]));
            }
        }

        public override void Save()
        {
            List<byte> data = new List<byte>();
            //encoding
            data.Add(ID3v2EncodingWrapper.ToByte(encoding));
            //data
            foreach (InvolvedPeopleItem item in items)
            {
                DetectEncoding(item.Involvement + item.Involvee);
                data.AddRange(encoding.GetBytes(item.Involvement + "\0" + item.Involvee + "\0"));
            }
            //we should change the encoding to fit the items
            data[0] = ID3v2EncodingWrapper.ToByte(encoding);
            //save
            this.Data = data.ToArray();
        }
    }
}
