package com.basgeekball.screenshotsnanny.core;

import android.os.Handler;

import com.basgeekball.screenshotsnanny.activityassistant.ActivityHelper;
import com.basgeekball.screenshotsnanny.helper.Callback;
import com.basgeekball.screenshotsnanny.helper.KeyboardHelper;

public class ScreenshotsTask {
    public static void perform(final Class<?> T, final Callback callback, final Callback completionCallback, final long screenshotDelay) {
        new Runnable() {
            @Override
            public void run() {
                callback.execute();
                ActivityHelper.performTaskWhenActivityIsReady(T, new Callback() {
                    @Override
                    public void execute() {
                        KeyboardHelper.hideKeyboard();
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                ScreenshotsCapturer.execute(ActivityHelper.getCurrentActivity(), completionCallback);
                            }
                        }, screenshotDelay);
                    }
                });
            }
        }.run();
    }

    public static void perform(final Class<?> T, final Callback callback, final Callback completionCallback, final int mapFragmentId) {
        new Runnable() {
            @Override
            public void run() {
                callback.execute();
                ActivityHelper.performTaskWhenActivityIsReady(T, new Callback() {
                    @Override
                    public void execute() {
                        KeyboardHelper.hideKeyboard();
                        ScreenshotsCapturer.executeWithMap(ActivityHelper.getCurrentActivity(), mapFragmentId, completionCallback);
                    }
                });
            }
        }.run();
    }
}

@@ -296,29 +296,30 @@
 Task %7B%0A    p
-ublic
+rivate
  static void
@@ -415,28 +415,75 @@
 nal 
-long screenshotDelay
+boolean hasMap, final long screenshotDelay, final int mapFragmentId
 ) %7B%0A
@@ -800,32 +800,243 @@
 hideKeyboard();%0A
+                        if (hasMap) %7B%0A                            ScreenshotsCapturer.executeWithMap(ActivityHelper.getCurrentActivity(), mapFragmentId, completionCallback);%0A                        %7D else %7B%0A    
                 
@@ -1106,32 +1106,36 @@
                 
+    
 @Override%0A      
@@ -1120,32 +1120,36 @@
       @Override%0A
+    
                 
@@ -1172,32 +1172,36 @@
 ic void run() %7B%0A
+    
                 
@@ -1322,34 +1322,42 @@
                 
+  
-%7D%0A
+  %7D%0A    
                 
@@ -1385,16 +1385,42 @@
 Delay);%0A
+                        %7D%0A
         
@@ -1514,38 +1514,32 @@
 ic void perform(
-final 
 Class%3C?%3E T, fina
@@ -1525,38 +1525,32 @@
 form(Class%3C?%3E T,
- final
  Callback callba
@@ -1544,38 +1544,32 @@
 llback callback,
- final
  Callback comple
@@ -1586,563 +1586,299 @@
 ck, 
-final int mapFragmentId) %7B%0A        new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                callback.execute(
+long screenshotDelay) %7B%0A        perform(T, callback, completionCallback, false, screenshotDelay, 0
 );%0A    
+%7D%0A%0A
     
-        ActivityHelper.performTaskWhenActivityIsReady(T, new Callback() %7B%0A                    @Override%0A                    public void execute() %7B%0A                        KeyboardHelper.hideKeyboard();%0A                        ScreenshotsCapturer.executeWithMap(ActivityHelper.getCurrentActivity(), mapFragmentId, completionCallback);%0A                    %7D%0A                %7D);%0A            %7D%0A        %7D.run(
+public static void perform(Class%3C?%3E T, Callback callback, Callback completionCallback, int mapFragmentId) %7B%0A        perform(T, callback, completionCallback, true, 0, mapFragmentId
 );%0A 

Refactor duplicated code in ScreenshotsTask