/* 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 CommercialFrame : ID3v2Frame
    {
        public CommercialFrame()
            : base("COMR", 0, 0, null)
        { }
        public CommercialFrame(int size, ushort flags, byte[] data)
            : base("COMR", size, flags, data)
        { }
        public override string Name
        {
            get { return "Commercial frame"; }
        }

        Encoding encoding = ID3v2EncodingWrapper.ISO_8859_1;

        string price = "";
        string validUntil = "";
        string contactURL = "";
        byte receivedAs = 0;
        string nameOfSeller = "";
        string description = "";
        string pictureMIMEtype = "";
        byte[] sellerLogoData;
        string currency = "usd";
        void DetectEncoding(string val)
        {
            if (encoding.CodePage == ID3v2EncodingWrapper.ISO_8859_1.CodePage)
                encoding = ID3v2EncodingWrapper.GetBestEncoding(val);
        }
        public string Price
        { get { return price; } set { price = value; } }
        public string Currency
        {
            get
            {
                foreach (string cur in Consts.Currency)
                {
                    if (cur.Substring(cur.Length - 5, 3).ToLower() == currency.ToLower())
                        return cur;
                }
                return currency;
            }
            set
            {
                currency = value.Substring(value.Length - 5, 3).ToUpper();
            }
        }
        public string ValidUntil
        { get { return validUntil; } set { validUntil = value; } }
        public string ContactURL
        { get { return contactURL; } set { contactURL = value; } }
        public string NameOfSeller
        { get { return nameOfSeller; } set { nameOfSeller = value; DetectEncoding(value); } }
        public string Description
        { get { return description; } set { description = value; DetectEncoding(value); } }
        public string PictureMIMEType
        { get { return pictureMIMEtype; } set { pictureMIMEtype = value; } }
        public byte[] SellerLogoData
        { get { return sellerLogoData; } set { sellerLogoData = value; } }
        public string ReceivedAs
        {
            get
            {
                return Consts.CommercialReceivedAs[receivedAs];
            }
            set
            {
                for (byte i = 0; i < Consts.CommercialReceivedAs.Length; i++)
                {
                    if (Consts.CommercialReceivedAs[i] == value)
                    { receivedAs = i; break; }
                }
            }
        }

        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);
            int index = 1;
            string[] list = code.Split(new char[] { '\0' });
            if (list[0].Length >= 3)
            {
                currency = list[0].Substring(0, 3);
                price = list[0].Substring(3, list[0].Length - 3);
            }
            index += ID3v2EncodingWrapper.ISO_8859_1.GetByteCount(list[0]);
            validUntil = code.Substring(index, 8);
            index += 8;
            list = code.Substring(index).Split(new char[] { '\0' });
            contactURL = list[0];
            index += contactURL.Length + 2;
            receivedAs = Data[index]; index++;

            code = encoding.GetString(Data, index, Data.Length - index);
            list = code.Split(new char[] { '\0' });
            nameOfSeller = list[0];
            index += encoding.GetByteCount(nameOfSeller + "\0");

            description = list[1];
            index += encoding.GetByteCount(description + "\0");

            code = ID3v2EncodingWrapper.ISO_8859_1.GetString(Data, index, Data.Length - index);
            list = code.Split(new char[] { '\0' });
            pictureMIMEtype = list[0];
            index += ID3v2EncodingWrapper.ISO_8859_1.GetByteCount(pictureMIMEtype + "\0");

            sellerLogoData = new byte[Data.Length - index];
            for (int i = index; i < Data.Length - index; i++)
            {
                sellerLogoData[i - index] = Data[i];
            }
        }

        public override int GetSize()
        {
            return 1 + ID3v2EncodingWrapper.ISO_8859_1.GetByteCount(currency + price + "\0") +
 ID3v2EncodingWrapper.ISO_8859_1.GetByteCount(validUntil) + ID3v2EncodingWrapper.ISO_8859_1.GetByteCount(contactURL + "\0") +
 1 + encoding.GetByteCount(nameOfSeller + "\0") + encoding.GetByteCount(description + "\0") + ID3v2EncodingWrapper.ISO_8859_1.GetByteCount(pictureMIMEtype + "\0")
 + ((sellerLogoData != null) ? sellerLogoData.Length : 0);
        }
        public override void Save()
        {
            List<byte> data = new List<byte>();
            //encoding
            data.Add(ID3v2EncodingWrapper.ToByte(encoding));
            //price
            data.AddRange(ID3v2EncodingWrapper.ISO_8859_1.GetBytes(currency + price + "\0"));
            //valid until
            data.AddRange(ID3v2EncodingWrapper.ISO_8859_1.GetBytes(validUntil));
            //url
            data.AddRange(ID3v2EncodingWrapper.ISO_8859_1.GetBytes(contactURL + "\0"));
            //Received as
            data.Add(receivedAs);
            //Name Of Seller
            data.AddRange(encoding.GetBytes(nameOfSeller + "\0"));
            //Description
            data.AddRange(encoding.GetBytes(description + "\0"));
            //Picture MIME type 
            data.AddRange(ID3v2EncodingWrapper.ISO_8859_1.GetBytes(pictureMIMEtype + "\0"));
            //picture data
            data.AddRange(sellerLogoData);
            //save
            this.Data = data.ToArray();
        }
    }
}
