package com.leff.midi.event.meta;

import java.io.IOException;
import java.io.OutputStream;

import com.leff.midi.event.MidiEvent;
import com.leff.midi.util.VariableLengthInt;

public class GenericMetaEvent extends MetaEvent
{
    private byte[] mData;

    protected GenericMetaEvent(long tick, long delta, MetaEventData info)
    {
        super(tick, delta, info.type, info.length);

        mData = info.data;

        System.out.println("Warning: GenericMetaEvent used because type (" + info.type + ") wasn't recognized or unexpected data length (" + info.length.getValue() + ") for type.");
    }
    
    private GenericMetaEvent(long tick, long delta, byte[] data, int type, VariableLengthInt length)
    {
        super(tick, delta, type, length);

        mData = data;
    }
    
    public MidiEvent clone(){
    	return new GenericMetaEvent(mTick, mDelta.getValue(), mData, mType, mLength);
    }
    
    @Override
    protected int getEventSize()
    {
        return 1 + 1 + mLength.getByteCount() + mLength.getValue();
    }

    @Override
    protected void writeToFile(OutputStream out) throws IOException
    {
        super.writeToFile(out);
        out.write(mLength.getBytes());
        out.write(mData);
    }

    @Override
    public int compareTo(MidiEvent other)
    {
        if(mTick != other.getTick())
        {
            return mTick < other.getTick() ? -1 : 1;
        }
        if(mDelta.getValue() != other.getDelta())
        {
            return mDelta.getValue() < other.getDelta() ? 1 : -1;
        }

        return 1;
    }

}
