package com.ar.tothestars.helpers;

import android.app.WallpaperManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.util.Log;

import com.ar.tothestars.R;
import com.ar.tothestars.activities.SinglePhotoActivity;
import com.ar.tothestars.models.APODPhoto;

import java.io.IOException;

/**
 * Created by ariviere on 07/09/15.
 */
public class PhotoHelper {

    /**
     * photo for activity info transfert
     */
    public static final String PHOTO = "photo";

    private PhotoHelper() {
    }

    /**
     * Used to set a picture as wallpaper for the user
     *
     * @param context     context of the view
     * @param photoBitmap bitmap of the photo to be show as desktop
     */
    public static void setPictureAsWallpaper(final Context context, final Bitmap photoBitmap) {
        if (photoBitmap != null) {
            new AlertDialog.Builder(context)
                    .setTitle("Desktop picture")
                    .setMessage("Use this photo as desktop picture ?")
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            try {
                                WallpaperManager.getInstance(context).setBitmap(photoBitmap);
                            } catch (IOException e) {
                                Log.e("desktop wallpaper", e.getMessage());
                            }
                        }
                    })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    })
                    .show();

        }
    }

    /**
     * Use to share a photo
     *
     * @param context     current context
     * @param photoBitmap photo to share
     * @param photoTitle  title of the photo to share
     */
    public static void sharePicture(Context context, Bitmap photoBitmap, String photoTitle) {
        // convert bitmap to Uri
        String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), photoBitmap, photoTitle, null);
        Uri photoUri = Uri.parse(path);

        // share image with Uri
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_STREAM, photoUri);
        intent.setType("image/jpeg");
        context.startActivity(Intent.createChooser(intent, context.getString(R.string.share_photo)));
    }

    /**
     * Use to start full screen photo activity
     * @param context context
     * @param photo photo to fullscreen
     */
    public static void startFullScreen(Context context, APODPhoto photo) {
        Intent intent = new Intent(context, SinglePhotoActivity.class);
        intent.putExtra(PHOTO, photo);
//                ActivityOptionsCompat options = ActivityOptionsCompat
//                        .makeSceneTransitionAnimation((Activity)getContext(), mPhotoView, "robot");
//
        context.startActivity(intent);
    }
}
@@ -307,33 +307,8 @@
 log;
-%0Aimport android.util.Log;
 %0A%0Aim
@@ -463,16 +463,101 @@
 ption;%0A%0A
+import rx.Observable;%0Aimport rx.functions.Action1;%0Aimport rx.schedulers.Schedulers;%0A%0A
 /**%0A * C
@@ -1482,267 +1482,43 @@
     
-try %7B%0A                                WallpaperManager.getInstance(context).setBitmap(photoBitmap);%0A                            %7D catch (IOException e) %7B%0A                                Log.e(%22desktop wallpaper%22, e.getMessage());%0A                            %7D
+setWallpaper(context, photoBitmap);
 %0A   
@@ -2710,16 +2710,23 @@
 ctivity%0A
+     *%0A
      * @
@@ -2763,24 +2763,26 @@
 param photo 
+  
 photo to ful
@@ -3160,16 +3160,16 @@
 t%22);%0A//%0A
-
         
@@ -3204,10 +3204,589 @@
 );%0A    %7D
+%0A%0A    private static void setWallpaper(final Context context, Bitmap bitmap) %7B%0A        Observable.just(bitmap)%0A                .subscribeOn(Schedulers.io())%0A                .subscribe(new Action1%3CBitmap%3E() %7B%0A                    @Override%0A                    public void call(Bitmap bitmap) %7B%0A                        try %7B%0A                            WallpaperManager.getInstance(context).setBitmap(bitmap);%0A                        %7D catch (IOException e) %7B%0A                            e.printStackTrace();%0A                        %7D%0A                    %7D%0A                %7D);%0A    %7D
 %0A%7D

set desktop wallpaper async