package scal.io.liger.model;

import android.media.MediaMetadataRetriever;
import android.os.Parcel;
import android.os.Parcelable;

import scal.io.liger.view.Util;

/**
 * @author Matt Bogner
 * @author Josh Steiner
 */
public class FullMetadata implements Parcelable {

    // class used for export, not serialized

    private int startTime;
    private int stopTime;
    private int duration; // duration in millisecond
    private int volume;
    private String effect;
    private String type;
    private String medium;
    private String filePath;

    public FullMetadata (ClipMetadata cm, MediaFile mf) {
        this.startTime = cm.getStartTime();
        this.stopTime = cm.getStopTime();
        this.volume = cm.getVolume();
        this.effect = cm.getEffect();
        this.type = cm.getType();
        this.medium = mf.getMedium();
        this.filePath = mf.getPath();

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(this.filePath);
        String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        long timeMs = 0;
        if (time != null) {
            timeMs = Long.parseLong(time);
        }
        this.duration = Util.safeLongToInt(timeMs);
    }

    public FullMetadata(Parcel in) {
        String[] data = new String[7];

        in.readStringArray(data);

        this.startTime = Integer.parseInt(data[0]);
        this.stopTime = Integer.parseInt(data[1]);
        this.volume = Integer.parseInt(data[2]);
        this.effect = data[3];
        this.type = data[4];
        this.medium = data[5];
        this.filePath = data[6];

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(this.filePath);
        String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        long timeInmillisec = Long.parseLong( time );
        this.duration = Util.safeLongToInt(timeInmillisec);
    }

    public static final Parcelable.Creator<FullMetadata> CREATOR
            = new Parcelable.Creator<FullMetadata>() {
        public FullMetadata createFromParcel(Parcel in) {
            return new FullMetadata(in);
        }

        public FullMetadata[] newArray(int size) {
            return new FullMetadata[size];
        }
    };


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        String[] data = new String[7];
        out.writeStringArray(new String[]{
                "" + this.startTime,
                "" + this.stopTime,
                "" + this.volume,
                this.effect,
                this.type,
                this.medium,
                this.filePath
        });
    }

    public int getStartTime() {
        return startTime;
    }

    public void setStartTime(int startTime) {
        this.startTime = startTime;
    }

    public int getStopTime() {
        return stopTime;
    }

    public void setStopTime(int stopTime) {
        this.stopTime = stopTime;
    }

    public int getVolume() {
        return volume;
    }

    public void setVolume(int volume) {
        this.volume = volume;
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int volume) {
        this.duration = duration;
    }

    public String getEffect() {
        return effect;
    }

    public void setEffect(String effect) {
        this.effect = effect;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getMedium() {
        return medium;
    }

    public void setMedium(String medium) {
        this.medium = medium;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }
}

@@ -124,16 +124,73 @@
 elable;%0A
+import android.util.Log;%0A%0Aimport scal.io.liger.Constants;
 %0Aimport 
@@ -319,16 +319,70 @@
 elable %7B
+%0A    private static final String TAG = %22FullMetadata%22;
 %0A%0A    //
@@ -987,24 +987,108 @@
 getPath();%0A%0A
+        if (this.medium == Constants.VIDEO %7C%7C this.medium == Constants.AUDIO) %7B%0A    
         Medi
@@ -1148,32 +1148,48 @@
 ever();%0A        
+    Log.d(TAG, %22
 retriever.setDat
@@ -1188,32 +1188,36 @@
 r.setDataSource(
+%22 + 
 this.filePath);%0A
@@ -1205,35 +1205,98 @@
  + this.filePath
-);%0A
+ + %22);%22);%0A            retriever.setDataSource(this.filePath);%0A    
         String t
@@ -1374,32 +1374,36 @@
 ATION);%0A        
+    
 long timeMs = 0;
@@ -1399,24 +1399,28 @@
 timeMs = 0;%0A
+    
         if (
@@ -1447,16 +1447,20 @@
         
+  
+  
 timeMs =
@@ -1486,24 +1486,28 @@
 e);%0A        
+    
 %7D%0A        th
@@ -1496,32 +1496,36 @@
       %7D%0A        
+ 
+   
 this.duration = 
@@ -1552,16 +1552,74 @@
 imeMs);%0A
+        %7D else %7B%0A            this.duration = 0;%0A        %7D%0A
     %7D%0A%0A 
@@ -2002,24 +2002,108 @@
 = data%5B6%5D;%0A%0A
+        if (this.medium == Constants.VIDEO %7C%7C this.medium == Constants.AUDIO) %7B%0A    
         Medi
@@ -2163,32 +2163,36 @@
 ever();%0A        
+    
 retriever.setDat
@@ -2215,32 +2215,36 @@
 ePath);%0A        
+  
+  
 String time = re
@@ -2306,32 +2306,36 @@
 _KEY_DURATION);%0A
+    
         long tim
@@ -2372,32 +2372,36 @@
 time );%0A        
+    
 this.duration = 
@@ -2436,16 +2436,74 @@
 lisec);%0A
+        %7D else %7B%0A            this.duration = 0;%0A        %7D%0A
     %7D%0A%0A 

fix crash publishing photo stories