package com.artemzin.android.wail.ui.fragment.dialogs;

import android.app.Dialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.afollestad.materialdialogs.Theme;
import com.artemzin.android.wail.R;
import com.artemzin.android.wail.service.WAILService;
import com.artemzin.android.wail.storage.db.LovedTracksDBHelper;
import com.artemzin.android.wail.storage.model.Track;
import com.artemzin.android.wail.util.AsyncTaskExecutor;

/**
 * @author Ilya Murzinov [murz42@gmail.com]
 */
public class TrackActionsDialog extends DialogDecorator {
    private Track track;

    public static TrackActionsDialog newInstance(Track track) {
        TrackActionsDialog dialog = new TrackActionsDialog();
        dialog.track = track;
        return dialog;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new MaterialDialog.Builder(activity)
                .items(R.array.track_actions)
                .itemsCallback(new MaterialDialog.ListCallback() {
                    @Override
                    public void onSelection(MaterialDialog materialDialog, View view,
                                            int i, CharSequence charSequence) {
                        switch (i) {
                            case 0: // Love
                                loveTrack();
                                break;
                        }
                    }
                })
                .theme(Theme.DARK)
                .build();
    }

    private void loveTrack() {
        if (track != null) {
            AsyncTaskExecutor.executeConcurrently(new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... objects) {
                    LovedTracksDBHelper.getInstance(activity).add(track);

                    Intent intent = new Intent(activity, WAILService.class);
                    intent.setAction(WAILService.INTENT_ACTION_HANDLE_LOVED_TRACKS);
                    activity.startService(intent);

                    return null;
                }

                @Override
                protected void onPostExecute(Void o) {
                    Toast.makeText(
                            activity,
                            getString(R.string.main_track_loved),
                            Toast.LENGTH_SHORT
                    ).show();
                }
            });

            dismiss();
        }
    }
}

@@ -111,37 +111,8 @@
 nt;%0A
-import android.os.AsyncTask;%0A
 impo
@@ -500,65 +500,8 @@
 ack;
-%0Aimport com.artemzin.android.wail.util.AsyncTaskExecutor;
 %0A%0A/*
@@ -1621,193 +1621,8 @@
 ) %7B%0A
-            AsyncTaskExecutor.executeConcurrently(new AsyncTask%3CVoid, Void, Void%3E() %7B%0A                @Override%0A                protected Void doInBackground(Void... objects) %7B%0A        
     
@@ -1680,32 +1680,24 @@
 dd(track);%0A%0A
-        
             
@@ -1765,24 +1765,16 @@
         
-  
-      
 intent.s
@@ -1838,32 +1838,24 @@
             
-       
- 
 activity.sta
@@ -1878,149 +1878,8 @@
 );%0A%0A
-                    return null;%0A                %7D%0A%0A                @Override%0A                protected void onPostExecute(Void o) %7B%0A        
     
@@ -1918,32 +1918,24 @@
             
-        
 activity,%0A  
@@ -1952,24 +1952,16 @@
         
-      
-  
 getStrin
@@ -2006,32 +2006,24 @@
             
-        
 Toast.LENGTH
@@ -2041,24 +2041,16 @@
         
-   
-     
 ).show()
@@ -2054,42 +2054,8 @@
 w();
-%0A                %7D%0A            %7D);
 %0A%0A  

fix NPE in TrackActionsDialog (again)