package com.viaccessorca.voplayerdemo.mediamgt;

import android.util.Log;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;


public class StreamingMediaList extends MediaItem implements Serializable {


    ArrayList<StreamingMediaItem> playlist = new ArrayList<StreamingMediaItem>();

    public StreamingMediaList(String mediaId, String displayName) {
        super(mediaId, null, displayName);
        this.mediaType = MediaType.MEDIA_PLAYLIST;
    }

    public void addItemToPlaylist(StreamingMediaItem item){
        playlist.add(item);
    }

    public void setPlaylistUri(){
        this.mediaUri = playlist.get(0).mediaUri;
    }

    public ArrayList<StreamingMediaItem> getStreamingMediaList(){
        return playlist;
    }

    public int getStreamingMediaListLength(){
        return playlist.size();
    }

    public StreamingMediaItem getStreamingMediaItemFromList( int element){
        return playlist.get(element);
    }


    @Override
    public int compareTo(MediaItem another) {
        switch(another.mediaType) {

            case MEDIA_PLAYLIST:
                return this.displayName.compareToIgnoreCase(another.displayName);

            case HTTP_STREAMING_MEDIA_EBD:
                return 1;

            case HTTP_STREAMING_MEDIA:
            case SUBTITLE_FILE:
            case LOCAL_MEDIA:
            case UNKNOWN:
            default:
                return -1;
        }
    }

    /**
     * @return the company name of the stream (VERIMATRIX SPECIFIC)
     */
    public String getCompanyName() {
        return null;
    }


    /**
     * @return the boot address of the stream (VERIMATRIX SPECIFIC)
     */
    public String getBootAddress() {
        return null;
    }


    /**
     * @return the license acquisition URL of the stream (WIDEVINE SPECIFIC)
     */
    public String getDrmLicenseAcquisitionUrl() {
        return null;
    }


    public static StreamingMediaList deserialize(byte[] b){
        try {
            ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(b));
            Object object = in.readObject();
            in.close();
            return (StreamingMediaList)object;
        } catch(ClassNotFoundException cnfe){
            Log.e("StreamingMediaList", "deserialize - class not found error", cnfe);
            return null;
        } catch(IOException ioe){
            Log.e("StreamingMediaList", "deserialize - io error", ioe);
            return null;
        }catch(NullPointerException npe){
            Log.e("StreamingMediaList", "deserialize - null pointer exception error", npe);
            return null;
        }
    }

    public byte[] serialize() {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        try {
            ObjectOutput out = new ObjectOutputStream(bos);
            out.writeObject(this);
            out.close();
            // Get the bytes of the serialized object
            byte[] buf = bos.toByteArray();
            return buf;
        } catch(IOException ioe) {
            Log.e("StreamingMediaList", "serialize - error", ioe);
            return null;
        }
    }

}
