package com.basgeekball.screenshotsnanny.core;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.View;

import com.basgeekball.screenshotsnanny.helper.Callback;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ScreenshotsCapturer {
    private ScreenshotsCapturer() {
        throw new AssertionError();
    }

    private static Bitmap captureScreenshot(Activity activity) {
        View rootView = activity.findViewById(android.R.id.content).getRootView();
        Bitmap bitmap = Bitmap.createBitmap(rootView.getWidth(), rootView.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        rootView.draw(canvas);
        return bitmap;
    }

    private static boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        return Environment.MEDIA_MOUNTED.equals(state);
    }

    private static void createDirIfNotExist(File dir) {
        if (!dir.exists()) {
            dir.mkdirs();
        }
    }

    private static void saveToFile(Bitmap bitmap, String filename, Activity activity) {
        String appName = activity.getString(activity.getApplicationInfo().labelRes);
        File screenshotDir = new File(Environment.getExternalStorageDirectory() + "/Screenshots/" + appName);
        createDirIfNotExist(screenshotDir);
        File screenshot = new File(screenshotDir.getPath() + File.separator + filename + ".png");
        try {
            screenshot.createNewFile();
            FileOutputStream outputStream = new FileOutputStream(screenshot);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void execute(Activity activity, Callback callback) {
        saveToFile(captureScreenshot(activity), activity.getClass().getSimpleName(), activity);
        Log.i(Constants.LOG_TAG, "♬ Screenshot is taken");
        callback.execute();
    }

    public static void executeWithMap(final Activity activity, int mapFragmentId, final Callback callback) {
        if (activity instanceof FragmentActivity) {
            FragmentManager fragmentManager = ((FragmentActivity) activity).getSupportFragmentManager();
            Fragment fragment = fragmentManager.findFragmentById(mapFragmentId);
            if (fragment instanceof SupportMapFragment) {
                final GoogleMap map = ((SupportMapFragment) fragment).getMap();
                map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                    @Override
                    public void onMapLoaded() {
                        map.snapshot(new GoogleMap.SnapshotReadyCallback() {
                            @Override
                            public void onSnapshotReady(Bitmap snapshot) {
                                View rootView = activity.findViewById(android.R.id.content).getRootView();
                                rootView.setDrawingCacheEnabled(true);
                                Bitmap background = rootView.getDrawingCache();
                                Bitmap overlay = Bitmap.createBitmap(background.getWidth(), background.getHeight(), background.getConfig());
                                Canvas canvas = new Canvas(overlay);
                                canvas.drawBitmap(background, 0, 0, null);
                                canvas.drawBitmap(snapshot, new Matrix(), null);
                                saveToFile(overlay, activity.getClass().getSimpleName(), activity);
                                Log.i(Constants.LOG_TAG, "♬ Screenshot is taken (including Map)");
                                callback.execute();
                            }
                        });
                    }
                });
            }
        }
    }
}

@@ -138,40 +138,8 @@
 as;%0A
-import android.graphics.Matrix;%0A
 impo
@@ -350,16 +350,44 @@
 ew.View;
+%0Aimport android.view.Window;
 %0A%0Aimport
@@ -2188,32 +2188,182 @@
        %7D%0A    %7D%0A%0A
+    private static int getBarHeight(Activity activity) %7B%0A        return activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();%0A    %7D%0A%0A
     public stati
@@ -3429,24 +3429,19 @@
 (Bitmap 
-snapshot
+map
 ) %7B%0A    
@@ -3733,23 +3733,22 @@
  Bitmap 
-overlay
+bitmap
  = Bitma
@@ -3893,23 +3893,22 @@
  Canvas(
-overlay
+bitmap
 );%0A     
@@ -4031,29 +4031,37 @@
 map(
-snapshot, new Matrix(
+map, 0, getBarHeight(activity
 ), n
@@ -4113,15 +4113,14 @@
 ile(
-overlay
+bitmap
 , ac

Fix wrong vertical shift (due to status bar height) in mixed map view