package com.leandog.brazenhead;

import android.app.Instrumentation;
import android.view.KeyEvent;
import android.widget.Spinner;

import com.jayway.android.robotium.solo.*;

public class SpinnerPresser {

    private final BrazenheadSleeper sleeper;
    private final Instrumentation instrumentation;

    public SpinnerPresser(final Instrumentation instrumentation) {
        this.instrumentation = instrumentation;
        sleeper = new BrazenheadSleeper();
    }

    public SpinnerPresser(final Instrumentation instrumentation, final BrazenheadSleeper sleeper) {
        this.instrumentation = instrumentation;
        this.sleeper = sleeper;
    }

    public void pressSpinnerItemById(int spinnerId, int itemIndex) {
        pressSpinnerItem((Spinner) solo().getView(spinnerId), itemIndex);
    }

    public void pressSpinnerItem(final Spinner spinner, int itemIndex) {
        solo().clickOnView(spinner);
        sleeper.sleep();
        try {
            instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
        } catch (SecurityException ignored) {
        }
        boolean countingUp = true;
        if (itemIndex < 0) {
            countingUp = false;
            itemIndex *= -1;
        }
        for (int i = 0; i < itemIndex; i++) {
            sleeper.sleepMini();
            if (countingUp) {
                try {
                    instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
                } catch (SecurityException ignored) {
                }
            } else {
                try {
                    instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_UP);
                } catch (SecurityException ignored) {
                }
            }
        }
        try {
            instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_ENTER);
        } catch (SecurityException ignored) {
        }
    }

    private Solo solo() {
        return TestRunInformation.getSolo();
    }

}

@@ -883,341 +883,158 @@
     
-solo().clickOnView(spinner);%0A        sleeper.sleep();%0A        try %7B%0A            instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);%0A        %7D catch (SecurityException ignored) %7B%0A        %7D%0A        boolean countingUp = true;%0A        if (itemIndex %3C 0) %7B%0A            countingUp = false;%0A            itemIndex *= -1;%0A        %7D
+final int direction = whichDirection(itemIndex);%0A        final int numberOfMoves = Math.abs(itemIndex);%0A        %0A        setTheInitialFocus(spinner);%0A
 %0A   
@@ -1051,31 +1051,47 @@
 int 
-i
+moves
  = 0; 
-i %3C itemIndex; i
+moves %3C numberOfMoves; moves
 ++) 
@@ -1141,25 +1141,24 @@
     
-if (countingUp) %7B
+move(direction);
 %0A   
@@ -1158,32 +1158,35 @@
 n);%0A        
+%7D%0A%0A
         
 try %7B%0A      
@@ -1177,265 +1177,468 @@
     
-try %7B
+selectTheItem();
 %0A    
+%7D%0A%0A
     
-            instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);%0A                %7D catch (SecurityException ignored) %7B%0A                %7D
+private void selectTheItem() %7B%0A        sendKey(KeyEvent.KEYCODE_ENTER);%0A    %7D%0A%0A    private int whichDirection(int itemIndex) %7B%0A        return itemIndex %3C 0 ? KeyEvent.KEYCODE_DPAD_UP : KeyEvent.KEYCODE_DPAD_DOWN;
 %0A    
+%7D%0A%0A
     
-    %7D else %7B%0A        
+private void move(int direction) %7B%0A        sendKey(direction);%0A
     
+%7D%0A%0A
     
-try %7B%0A                    instrumentation.sendKeyDownUpSync
+private void setTheInitialFocus(final Spinner spinner) %7B%0A        solo().clickOnView(spinner);%0A        sleeper.sleep();%0A        sendKey
 (Key
@@ -1660,108 +1660,57 @@
 PAD_
-UP
+DOWN
 );%0A    
+%7D%0A%0A
     
-        %7D catch (SecurityException ignored) %7B%0A                %7D%0A            %7D%0A        %7D
+private void sendKey(int key) %7B
 %0A   
@@ -1766,38 +1766,19 @@
 nUpSync(
-K
+k
 ey
-Event.KEYCODE_ENTER
 );%0A     

refactor SpinnerPresser