package com.marverenic.music.data.store;

import android.content.Context;
import android.support.v4.util.LongSparseArray;

import com.marverenic.music.instances.Song;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class LocalPlayCountStore implements PlayCountStore {

    private Context mContext;
    private final LongSparseArray<Count> mCounts;

    public LocalPlayCountStore(Context context) {
        mContext = context;
        mCounts = new LongSparseArray<>();
    }

    @Override
    public Observable<Void> refresh() {
        return Observable.fromCallable(
                () -> {
                    mCounts.clear();

                    // TODO read counts from disk

                    return (Void) null;
                })
                .observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public void save() {
        // TODO write counts to disk
    }

    @Override
    public int getPlayCount(Song song) {
        Count count = mCounts.get(song.getSongId());
        if (count == null) {
            return 0;
        } else {
            return count.mPlays;
        }
    }

    @Override
    public int getSkipCount(Song song) {
        Count count = mCounts.get(song.getSongId());
        if (count == null) {
            return 0;
        } else {
            return count.mSkips;
        }
    }

    @Override
    public long getPlayDate(Song song) {
        Count count = mCounts.get(song.getSongId());
        if (count == null) {
            return 0;
        } else {
            return count.mDate;
        }
    }

    @Override
    public void incrementPlayCount(Song song) {
        setPlayCount(song, getPlayCount(song) + 1);
    }

    @Override
    public void incrementSkipCount(Song song) {
        setSkipCount(song, getSkipCount(song) + 1);
    }

    @Override
    public void setPlayDateToNow(Song song) {
        setPlayDate(song, System.currentTimeMillis());
    }

    private Count getOrInitializeCount(Song song) {
        Count count = mCounts.get(song.getSongId());
        if (count == null) {
            count = new Count();
            mCounts.put(song.getSongId(), count);
        }

        return count;
    }

    @Override
    public void setPlayCount(Song song, int count) {
        getOrInitializeCount(song).mPlays = count;
    }

    @Override
    public void setSkipCount(Song song, int count) {
        getOrInitializeCount(song).mSkips = count;
    }

    @Override
    public void setPlayDate(Song song, long time) {
        getOrInitializeCount(song).mDate = time;
    }

    private static class Count {

        int mPlays;
        int mSkips;
        long mDate;

    }
}

@@ -119,218 +119,674 @@
 ay;%0A
-%0Aimport com.marverenic.music.instances.Song;%0A%0Aimport rx.Observable;%0Aimport rx.android.schedulers.AndroidSchedulers;%0Aimport rx.schedulers.Schedulers;%0A%0Apublic class LocalPlayCountStore implements PlayCountStore %7B
+import android.util.Log;%0A%0Aimport com.crashlytics.android.Crashlytics;%0Aimport com.marverenic.music.instances.Song;%0A%0Aimport java.io.File;%0Aimport java.io.FileWriter;%0Aimport java.io.IOException;%0Aimport java.util.Properties;%0A%0Aimport rx.Observable;%0Aimport rx.android.schedulers.AndroidSchedulers;%0Aimport rx.schedulers.Schedulers;%0A%0Apublic class LocalPlayCountStore implements PlayCountStore %7B%0A%0A    private static final String TAG = %22LocalPlayCountStore%22;%0A%0A    private static final String PLAY_COUNT_FILENAME = %22.playcount%22;%0A    private static final String PLAY_COUNT_HEADER = %22This file contains play count information for %22%0A            + %22Jockey and should not be edited%22;
 %0A%0A  
@@ -1379,65 +1379,594 @@
     
-@Override%0A    public void save() %7B%0A        // TODO
+private File getPlayCountFile() %7B%0A        return new File(mContext.getExternalFilesDir(null), PLAY_COUNT_FILENAME);%0A    %7D%0A%0A    @Override%0A    public void save() %7B%0A        Properties properties = new Properties();%0A%0A        for (int i = 0; i %3C mCounts.size(); i++) %7B%0A            String key = Long.toString(mCounts.keyAt(i));%0A            String value = mCounts.valueAt(i).getCommaSeparatedValues();%0A            properties.put(key, value);%0A        %7D%0A%0A        try %7B%0A            writePlayCounts(properties);%0A        %7D catch (IOException e) %7B%0A            Log.e(TAG, %22save: Failed to
  write 
+play 
 coun
@@ -1975,16 +1975,450 @@
  to disk
+%22, e);%0A            Crashlytics.logException(e);%0A        %7D%0A    %7D%0A%0A    private void writePlayCounts(Properties playCounts) throws IOException %7B%0A        FileWriter fileWriter = null;%0A%0A        try %7B%0A            fileWriter = new FileWriter(getPlayCountFile());%0A            playCounts.store(fileWriter, PLAY_COUNT_HEADER);%0A        %7D finally %7B%0A            if (fileWriter != null) %7B%0A                fileWriter.close();%0A            %7D%0A        %7D
 %0A    %7D%0A%0A

Implement play count writing