package com.marverenic.music.instances.section;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.support.design.widget.Snackbar;

import com.marverenic.music.BuildConfig;
import com.marverenic.music.JockeyApplication;
import com.marverenic.music.R;
import com.marverenic.music.data.store.MediaStoreUtil;
import com.marverenic.music.data.store.MusicStore;
import com.marverenic.music.data.store.PlaylistStore;

import javax.inject.Inject;

public class LibraryEmptyState extends BasicEmptyState {

    private Activity mActivity;

    @Inject MusicStore mMusicStore;
    @Inject PlaylistStore mPlaylistStore;

    public LibraryEmptyState(Activity activity) {
        mActivity = activity;
        JockeyApplication.getComponent(activity).inject(this);
    }

    public String getEmptyMessage() {
        return mActivity.getString(R.string.empty);
    }

    @Override
    public final String getMessage() {
        if (MediaStoreUtil.hasPermission(mActivity)) {
            return getEmptyMessage();
        } else {
            return mActivity.getString(R.string.empty_no_permission);
        }
    }

    public String getEmptyMessageDetail() {
        return mActivity.getString(R.string.empty_detail);
    }

    @Override
    public final String getDetail() {
        if (MediaStoreUtil.hasPermission(mActivity)) {
            return getEmptyMessageDetail();
        } else {
            return mActivity.getString(R.string.empty_no_permission_detail);
        }
    }

    public String getEmptyAction1Label() {
        return mActivity.getString(R.string.action_refresh);
    }

    @Override
    public final String getAction1Label() {
        if (MediaStoreUtil.hasPermission(mActivity)) {
            return getEmptyAction1Label();
        } else {
            return mActivity.getString(R.string.action_try_again);
        }
    }

    public String getEmptyAction2Label() {
        return super.getAction2Label();
    }

    @Override
    public final String getAction2Label() {
        if (MediaStoreUtil.hasPermission(mActivity)) {
            return getEmptyAction2Label();
        } else {
            return mActivity.getString(R.string.action_open_settings);
        }
    }

    @Override
    public void onAction1() {
        mMusicStore.refresh().subscribe(successful -> {
            if (successful) {
                Snackbar
                        .make(
                                mActivity.findViewById(R.id.list),
                                R.string.confirm_refresh_library,
                                Snackbar.LENGTH_SHORT)
                        .show();
            }
        });
    }

    @Override
    public void onAction2() {
        if (!MediaStoreUtil.hasPermission(mActivity)) {
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            Uri uri = Uri.fromParts("package", BuildConfig.APPLICATION_ID, null);
            intent.setData(uri);
            mActivity.startActivity(intent);
        }
    }
}

@@ -206,16 +206,42 @@
 nackbar;
+%0Aimport android.view.View;
 %0A%0Aimport
@@ -543,16 +543,107 @@
 nject;%0A%0A
+import rx.Observable;%0A%0Aimport static android.support.design.widget.Snackbar.LENGTH_SHORT;%0A%0A
 public c
@@ -2448,149 +2448,429 @@
     
-mMusicStore.refresh().subscribe(successful -%3E %7B%0A            if (successful) %7B%0A                Snackbar%0A                        .make(%0A       
+Observable%3CBoolean%3E musicStoreResult = mMusicStore.refresh();%0A        Observable%3CBoolean%3E playlistStoreResult = mPlaylistStore.refresh();%0A%0A        Observable%3CBoolean%3E combinedResult = Observable.combineLatest(%0A                musicStoreResult, playlistStoreResult, (result1, result2) -%3E result1 && result2);%0A%0A        combinedResult.take(1)%0A                .subscribe(successful -%3E %7B%0A                    if (successful) %7B%0A
     
@@ -2881,32 +2881,48 @@
                 
+View container =
  mActivity.findV
@@ -2939,17 +2939,17 @@
 id.list)
-,
+;
 %0A       
@@ -2965,23 +2965,40 @@
         
-       
+Snackbar.make(container,
  R.strin
@@ -3023,16 +3023,30 @@
 library,
+ LENGTH_SHORT)
 %0A       
@@ -3074,30 +3074,16 @@
     
-Snackbar.LENGTH_SHORT)
+.show();
 %0A   
@@ -3095,36 +3095,25 @@
             
-    .show();
+%7D
 %0A           
@@ -3101,38 +3101,32 @@
       %7D%0A        
-    %7D%0A
         %7D);%0A    

Allow empty states to refresh playlists