using System;
using System.IO;

namespace Mp4BoxViewer.Model.Boxes
{
    public class TrackHeaderBox : Box
    {
        public byte Version { get; set; }

        public byte[] Flags { get; set; }

        public UInt64 CreationTime { get; set; }

        public UInt64 ModificationTime { get; set; }

        public UInt32 TrackId { get; set; }

        public UInt64 Duration { get; set; }

        public Int16 Layer { get; set; }

        public Int16 AlternateGroup { get; set; }

        public Int16 Volume { get; set; }

        public Int32[] Matrix { get; set; }

        public UInt32 Width { get; set; }

        public UInt32 Height { get; set; }

        public TrackHeaderBox(Box parent) : base(parent) { }

        public override void StructUniqueParameters(FileStream fs)
        {
            base.StructUniqueParameters(fs);

            Version = (byte)fs.ReadByte();
            Flags = new byte[3];
            fs.Read(Flags, 0, Flags.Length);
            if (Version == 1)
            {
                CreationTime = Mp4Util.getUint64Number(fs);
                ModificationTime = Mp4Util.getUint64Number(fs);
                TrackId = Mp4Util.getUint32Number(fs);
                fs.Seek(4, SeekOrigin.Current);
                Duration = Mp4Util.getUint64Number(fs);
            }
            else
            {
                CreationTime = Mp4Util.getUint32Number(fs);
                ModificationTime = Mp4Util.getUint32Number(fs);
                TrackId = Mp4Util.getUint32Number(fs);
                fs.Seek(4, SeekOrigin.Current);
                Duration = Mp4Util.getUint32Number(fs);
            }
            fs.Seek(8, SeekOrigin.Current);
            Layer = Mp4Util.getInt16Number(fs);
            AlternateGroup = Mp4Util.getInt16Number(fs);
            Volume = Mp4Util.getInt16Number(fs);
            fs.Seek(2, SeekOrigin.Current);
            Matrix = new Int32[9];
            for (int i = 0; i < Matrix.Length; i++)
            {
                Matrix[i] = Mp4Util.getInt32Number(fs);
            }
            Width = Mp4Util.getUint32Number(fs);
            Height = Mp4Util.getUint32Number(fs);

            Parameters.Add("Version", Version.ToString());
            Parameters.Add("Flags", Flags[0].ToString("X2") + Flags[1].ToString("X2") + Flags[2].ToString("X2"));
            Parameters.Add("CreationTime", Mp4Util.getUtcTime(CreationTime).ToString());
            Parameters.Add("ModificationTime", Mp4Util.getUtcTime(ModificationTime).ToString());
            Parameters.Add("TrackId", TrackId.ToString());
            Parameters.Add("Duration", Duration.ToString());
            Parameters.Add("Layer", Layer.ToString());
            Parameters.Add("AlternateGroup", AlternateGroup.ToString());
            Parameters.Add("Volume", Volume.ToString());
            Parameters.Add("Matrix", Mp4Util.getArrayString(Matrix));
            Parameters.Add("Width", Width.ToString());
            Parameters.Add("Height", Height.ToString());
        }
    }
}
