package org.a0z.mpd;

import android.os.Parcel;
import android.os.Parcelable;

import java.util.Locale;

public class Artist extends Item implements Parcelable {
	public static String singleAlbumFormat="%1 Album";
	public static String multipleAlbumsFormat="%1 Albums";

	private final String name;
	private final String sort;
	//private final boolean isVa;
	private final int albumCount;

	public Artist(String name, int albumCount) {
		this.name=name;
		if (null != name && name.toLowerCase(Locale.getDefault()).startsWith("the ")) {
			sort=name.substring(4);
		} else {
			sort=null;
		}
		this.albumCount=albumCount;
	}

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

    public Artist(Artist a) {
        this.name = a.name;
        this.albumCount = a.albumCount;
        this.sort = a.sort;
    }

	protected Artist(Parcel in) {
		this.name=in.readString();
		this.sort=in.readString();
		this.albumCount=in.readInt();
    }

	public String getName() {
		return name;
	}

	public String sort() {
        return null == sort ? name == null ? "" : name : sort;
    }

	@Override
	public String subText() {
		if (0==albumCount) {
			return null;
		}

		return String.format(1==albumCount ? singleAlbumFormat : multipleAlbumsFormat, albumCount);
	}

	@Override
    public int compareTo(Item o) {
		if (o instanceof Artist) {
			Artist oa=(Artist)o;
			/*
			if (isVa && !oa.isVa) {
				return -1;
			}
			if (!isVa && oa.isVa) {
				return 1;
			}
			*/
			return sort().compareToIgnoreCase(oa.sort());
		}

		return super.compareTo(o);
	}

    @Override
    public boolean equals(Object o) {
    	return (o instanceof Artist) && ((Artist)o).name.equals(name);
    }

    public boolean isSameOnList(Item o) {
        if (null == o) {
            return false;
        }
        return (name.equals(((Artist)o).name));
    }

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

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(this.name);
		dest.writeString(this.sort);
		dest.writeInt(this.albumCount);
	}

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

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

@@ -382,18 +382,56 @@
 mCount;%0A
-%0A%09
+    private boolean isAlbumArtist;%0A%0A    
 public A
@@ -463,24 +463,142 @@
 bumCount) %7B%0A
+        this(name, albumCount, false);%0A    %7D%0A%0A    public Artist(String name, int albumCount, boolean isAlbumArtist) %7B%0A
 %09%09this.name=
@@ -775,67 +775,231 @@
 nt;%0A
-%09%7D%0A%0A    public Artist(String name) %7B%0A        this(name, 0);
+                this.isAlbumArtist = isAlbumArtist;%0A    %7D%0A%0A    public Artist(String name) %7B%0A        this(name, 0, false);%0A    %7D%0A%0A    public Artist(String name, boolean isAlbumArtist) %7B%0A        this(name, 0, isAlbumArtist);%0A
 %0A   
@@ -1124,24 +1124,70 @@
 t = a.sort;%0A
+        this.isAlbumArtist = a.isAlbumArtist;%0A
     %7D%0A%0A%09prot
@@ -1302,16 +1302,55 @@
 dInt();%0A
+%09%09this.isAlbumArtist=(in.readInt()%3E0);%0A
     %7D%0A%0A%09
@@ -1390,24 +1390,178 @@
 n name;%0A%09%7D%0A%0A
+    public boolean isAlbumArtist() %7B%0A        return isAlbumArtist;%0A    %7D%0A    public void setIsAlbumArtist(boolean aa)%7B%0A        isAlbumArtist = aa;%0A    %7D%0A%0A
 %09public Stri
@@ -2614,24 +2614,79 @@
 lbumCount);%0A
+                dest.writeInt(this.isAlbumArtist?1:0);%0A
 %09%7D%0A%0A%09public 
@@ -2998,10 +2998,103 @@
      %7D;%0A
+%0A%0A    public String info() %7B%0A        return getName() + (isAlbumArtist()?%22 (AA)%22:%22%22);%0A    %7D%0A%0A
 %7D%0A

add isAlbumArtist to Artist.java