﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mp4BoxViewer.Model.Boxes
{
    public class MovieHeaderBox : Box
    {
        public UInt32 Version { get; set; }

        public UInt64 CreationTime { get; set; }

        public UInt64 ModificationTime { get; set; }

        public UInt32 Timescale { get; set; }

        public UInt64 Duration { get; set; }

        public Int32 Rate { get; set; }

        public Int16 Volume { get; set; }

        public Int32[] Matrix { get; set; }

        public UInt32 NextTrackId { get; set; }

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

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

            Version = Mp4Util.getUint32Number(fs);
            if (Version == 1)
            {
                CreationTime = Mp4Util.getUint64Number(fs);
                ModificationTime = Mp4Util.getUint64Number(fs);
                Timescale = Mp4Util.getUint32Number(fs);
                Duration = Mp4Util.getUint64Number(fs);
            }
            else
            {
                CreationTime = Mp4Util.getUint32Number(fs);
                ModificationTime = Mp4Util.getUint32Number(fs);
                Timescale = Mp4Util.getUint32Number(fs);
                Duration = Mp4Util.getUint32Number(fs);
            }
            Rate = Mp4Util.getInt32Number(fs);
            Volume = Mp4Util.getInt16Number(fs);
            fs.Seek(10, SeekOrigin.Current);
            Matrix = new Int32[9];
            for (int i = 0; i < Matrix.Length; i++)
            {
                Matrix[i] = Mp4Util.getInt32Number(fs);
            }
            fs.Seek(24, SeekOrigin.Current);
            NextTrackId = Mp4Util.getUint32Number(fs);

            Parameters.Add("Version", Version.ToString());
            Parameters.Add("CreationTime", Mp4Util.getUtcTime(CreationTime).ToString());
            Parameters.Add("ModificationTime", Mp4Util.getUtcTime(ModificationTime).ToString());
            Parameters.Add("Timescale", Timescale.ToString());
            Parameters.Add("Duration", Duration.ToString());
            Parameters.Add("Rate", Rate.ToString());
            Parameters.Add("Volume", Volume.ToString());
            Parameters.Add("Matrix", Mp4Util.getArrayString(Matrix));
            Parameters.Add("NextTrackId", NextTrackId.ToString());
        }
    }
}
