package org.a0z.mpd;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

public class Album extends Item implements Parcelable {
    public static String singleTrackFormat = "%1 Track (%2)";
    public static String multipleTracksFormat = "%1 Tracks (%2)";

    private final String name;
    private long songCount;
    private long duration;
    private long year;
    private Artist artist;

    public Album(String name, long songCount, long duration, long year, Artist artist) {
        this.name = name;
        this.songCount = songCount;
        this.duration = duration;
        this.year = year;
        this.artist = artist;
    }

    public Album(String name, Artist artist) {
        this(name, 0, 0, 0, artist);
    }

    public Album(Album a) {
        this(a.name, a.songCount, a.duration, a.year, new Artist(a.artist.getName()));
    }

    protected Album(Parcel in) {
        this.name = in.readString();
        this.songCount = in.readLong();
        this.duration = in.readLong();
        this.year = in.readLong();
        this.artist = new Artist(in.readString());
    }

    public String getName() {
        return name;
    }

    public long getSongCount() {
        return songCount;
    }

    public void setSongCount(long sc) {
        songCount = sc;
    }

    public long getYear() {
        return year;
    }

    public void setYear(long y) {
        year = y;
    }

    public long getDuration() {
        return duration;
    }

    public void setDuration(long d) {
        duration = d;
    }

    @Override
    public String mainText() {
        return name;
    }

    public Artist getArtist() {
        return artist;
    }

    public void setArtist(Artist artist) {
        this.artist = artist;
    }

    @Override
    public String subText() {
        String construct = null;
        if (MPD.sortAlbumsByYear() && 0 != year) {
            construct = Long.toString(year);
        }
        if (0 != songCount) {
            if (construct != null)
                construct += " - ";
            construct += String.format(1 == songCount ? singleTrackFormat : multipleTracksFormat, songCount, Music.timeToString(duration));
        }
        return construct;
    }

    @Override
    public int compareTo(Item o) {
        if (o instanceof Album) {
            Album oa = (Album) o;
            if (MPD.sortAlbumsByYear()) {
                if (year != oa.year) {
                    return year < oa.year ? -1 : 1;
                }
            }
            int comp = super.compareTo(o);
            if (comp == 0) { // same album name, check artist
                comp = artist.compareTo(oa.artist);
            }
        }
        return super.compareTo(o);
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof Album) {
            Album a = (Album) o;
            return (year == a.year && duration == a.duration &&
                    songCount == a.songCount &&
                    name.equals(a.name) && artist.equals(a.artist));
        }
        return false;
    }

    public boolean isSameOnList(Item o) {
        if (null == o) {
            return false;
        }
        Album a = (Album)o;
        return (name.equals(a.getName()) &&
                artist.isSameOnList(a.getArtist()));
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.name);
        dest.writeLong(this.songCount);
        dest.writeLong(this.duration);
        dest.writeLong(this.year);
        dest.writeString(this.artist.getName());
    }

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

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

    public AlbumInfo getAlbumInfo() {
        return new AlbumInfo(getArtist().getName(), getName());
    }

}

@@ -387,24 +387,49 @@
  long year;%0A
+    private String path;%0A
     private 
@@ -525,32 +525,217 @@
 rtist artist) %7B%0A
+        this(name, songCount, duration, year, artist, %22%22);%0A    %7D%0A%0A    public Album(String name, long songCount, long duration, long year,%0A                 Artist artist, String path) %7B%0A
         this.nam
@@ -862,32 +862,58 @@
 rtist = artist;%0A
+        this.path = path;%0A
     %7D%0A%0A    publi
@@ -980,24 +980,138 @@
 0, 0, artist
+, %22%22);%0A    %7D%0A%0A    public Album(String name, Artist artist, String path) %7B%0A        this(name, 0, 0, 0, artist, path
 );%0A    %7D%0A%0A  
@@ -1209,27 +1209,25 @@
 a.artist
-.getName())
+), a.path
 );%0A    %7D
@@ -1404,32 +1404,69 @@
  in.readLong();%0A
+        this.path = in.readString();%0A
         this.art
@@ -1496,24 +1496,89 @@
 dString());%0A
+        this.artist.setIsAlbumArtist(in.readInt()%3E0?true:false);%0A
     %7D%0A%0A    p
@@ -2005,32 +2005,150 @@
 ion = d;%0A    %7D%0A%0A
+    public String getPath() %7B%0A        return path;%0A    %7D%0A    public void setPath(String p) %7B%0A        path = p;%0A    %7D%0A%0A
     @Override%0A  
@@ -3545,24 +3545,67 @@
 ongCount &&%0A
+                    path.equals(a.path) &&%0A
             
@@ -4227,24 +4227,61 @@
 this.year);%0A
+        dest.writeString(this.path);%0A
         dest
@@ -4313,24 +4313,80 @@
 getName());%0A
+        dest.writeInt(this.artist.isAlbumArtist()?1:0);%0A
     %7D%0A%0A    p
@@ -4831,24 +4831,187 @@
 ), getName()
+, getPath(), %22%22);%0A    %7D%0A%0A    public String info() %7B%0A        return getArtist().info() + %22 // %22 + getName() +%0A            (%22%22.equals(path) ? %22%22 : %22 (%22+getPath()+%22)%22
 );%0A    %7D%0A%0A%7D%0A

add path to Album.java