package io.mokshjn.cosmo.provider;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.media.MediaMetadataCompat;

import java.util.ArrayList;
import java.util.Iterator;

import io.mokshjn.cosmo.utils.LibUtils;

/**
 * Created by moksh on 19/3/17.
 */

public class LibrarySource implements MusicProviderSource {

    private ContentResolver resolver;

    public LibrarySource(ContentResolver resolver) {
        this.resolver = resolver;
    }

    @Override
    public Iterator<MediaMetadataCompat> iterator() {
        ArrayList<MediaMetadataCompat> tracks = new ArrayList<>();
        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String sortOrder = MediaStore.Audio.AudioColumns.TITLE + " COLLATE LOCALIZED ASC";
        Cursor cursor = resolver.query(uri, null, null, null, sortOrder);
        if(cursor != null && cursor.moveToFirst()) {
            do {
                MediaMetadataCompat metadata = new MediaMetadataCompat.Builder()
                        .putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, String.valueOf(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.AudioColumns._ID))))
                        .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM_ID)))
                        .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST)))
                        .putString(MediaMetadataCompat.METADATA_KEY_TITLE, cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.TITLE)))
                        .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)))
                        .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.TRACK)))
                        .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI, LibUtils.getMediaStoreAlbumCoverUri(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID))).toString())
                        .putString(LibUtils.ALBUM_TITLE, cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM)))
                        .build();
                tracks.add(metadata);
            } while (cursor.moveToNext());
            cursor.close();
        }

        return tracks.iterator();
    }
}

@@ -2535,11 +2535,1360 @@
 );%0A    %7D
+%0A%0A    public Iterator%3CMediaMetadataCompat%3E albums() %7B%0A        ArrayList%3CMediaMetadataCompat%3E album = new ArrayList%3C%3E();%0A        Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;%0A        String sortOrder = MediaStore.Audio.AudioColumns.TITLE + %22 COLLATE LOCALIZED ASC%22;%0A        Cursor cursor = resolver.query(uri, null, null, null, sortOrder);%0A        if (cursor != null && cursor.moveToFirst()) %7B%0A            do %7B%0A                MediaMetadataCompat mediaMetadataCompat = new MediaMetadataCompat.Builder()%0A                        .putString(MediaMetadataCompat.METADATA_KEY_TITLE, cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM)))%0A                        .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AlbumColumns.ARTIST)))%0A                        .putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM_ID)))%0A                        .putString(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AlbumColumns.NUMBER_OF_SONGS)))%0A                        .build();%0A                album.add(mediaMetadataCompat);%0A            %7D while (cursor.moveToNext());%0A            cursor.close();%0A        %7D%0A%0A        return album.iterator();%0A    %7D
 %0A%7D%0A

add album reading method