package SelendroidTest;

 

import objects.ImageRecognitionSettings;

import objects.ImageSearchResult;

 

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

 

import io.selendroid.client.SelendroidDriver;

import io.selendroid.client.SelendroidKeys;

import io.selendroid.client.TouchAction;

import io.selendroid.client.TouchActionBuilder;

import io.selendroid.server.common.action.touch.FlickDirection;

import io.selendroid.common.SelendroidCapabilities;

 

import org.openqa.selenium.Point;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.interactions.touch.TouchActions;

import org.openqa.selenium.interactions.Actions;

 

import imagerecognition.ImageRecognition;

 

 

public class AndroidSample extends TestdroidImageRecognition {

      int xcorrect = 0, ycorrect = 0;

      double resize = 0;

     

      private WebDriver driver = null;

 

    @Before

    public void setUp() throws Exception {

      Thread.sleep(10000);

      driver = new SelendroidDriver(new SelendroidCapabilities("com.GatoSalvaje.ARKIEng:1.9"));

    }

 

    @After

    public void tearDown() {

      driver.quit();

    }

 

    @Test

    public void mainPageTest() throws Exception {

     

 

      detectGameViewport("main_menu_complete.png",10);

      Tap(700, 113);

         

          waitForImageToDisappearFromScreen("main_menu.png");

          SkipCutscene();

         

          waitForImageToAppearAtScreen("own_room.png",10);

          SkipComment(0,0,100,1);

      Tap(535, 163);

         

          waitForImageToDisappearFromScreen("own_room.png");

          SkipCutscene();

         

          waitForImageToAppearAtScreen("own_room.png",5);

      Tap(720, 163);

 

          waitForImageToDisappearFromScreen("own_room.png");

          SkipComment(0,200,0,1);

         

          waitForImageToAppearAtScreen("street_general.png",10);

          SkipComment(2000,200,4500,4);

          Tap(500, 243);

         

     

          waitForImageToAppearAtScreen("park_general.png",10);

      Tap(560, 233);

        

          waitForImageToAppearAtScreen("junction_general.png",10);

      Tap(730, 253);

         

          waitForImageToAppearAtScreen("uni_entrance_general.png",10);

          Tap(430, 213);

     

         

          waitForImageToAppearAtScreen("uni_foyer_general.png",10);

          SkipComment(0,100,0,4);

      Tap(300, 223);

          SkipComment(0,200,0,12);

         

          waitForImageToAppearAtScreen("uni_foyer_buy_skunk.png",5);

      Tap(90, 358);

          SkipComment(100,100,0,2);

         

          waitForImageToDisappearFromScreen("uni_foyer_general.png");

          SkipCutscene();

         

          waitForImageToAppearAtScreen("uni_foyer_general.png",5);

      Tap(200, 143);

         

         

          waitForImageToAppearAtScreen("uni_1st_floor_arrive.png",5);

      Tap(710, 423);

      Wait(4000);

          Tap(710, 423);

          Wait(4000);

          Tap(710, 423);

          Wait(4000);

          Tap(620, 193);

         

          waitForImageToDisappearFromScreen("uni_1st_floor_door.png");

          SkipComment(0,0,0,1);

         

          waitForImageToAppearAtScreen("uni_1st_floor_door.png",10);

          SkipComment(100,100,0,5);

          Tap(150, 423);

          Wait(4000);

          Tap(150, 423);

          Wait(4000);

          Tap(40, 243);

         

         

          waitForImageToAppearAtScreen("uni_foyer_general.png",10);

          Tap(60, 233);

         

          waitForImageToAppearAtScreen("uni_entrance_general.png",10);

          Tap(35, 373);

         

          waitForImageToAppearAtScreen("junction_general.png",10);

          Tap(515, 453);

         

          waitForImageToAppearAtScreen("park_general.png",10);

          Tap(15, 343);

         

         

          waitForImageToAppearAtScreen("street_general.png",10);

          UseInventoryItem(170, 100, 65, 410);

          SkipComment(4000,100,2000,10);

          Tap(405, 393);

          SkipComment(2000,100,0,2);

         

          waitForImageToDisappearFromScreen("street_general.png");

          SkipCutscene();

         

          waitForImageToAppearAtScreen("street_general.png",10);

          Tap(500, 243);

         

         

          waitForImageToAppearAtScreen("park_general.png",10);

          Tap(560, 233);

         

          waitForImageToAppearAtScreen("junction_general.png",10);

          Tap(730, 253);

         

          waitForImageToAppearAtScreen("uni_entrance_general.png",10);

          Tap(430, 213);

         

         

          waitForImageToAppearAtScreen("uni_foyer_general.png",10);

          UseInventoryItem(245, 230, 650, 260);

          SkipComment(4000,100,0,3);

         

          waitForImageToAppearAtScreen("uni_foyer_offer_alcohol.png",10);

          Tap(90, 273);

          SkipComment(500,100,0,3);

                

          waitForImageToDisappearFromScreen("uni_foyer_general.png");

          SkipCutscene();

         

          waitForImageToAppearAtScreen("uni_foyer_general.png",10);

          SkipComment(100,0,0,1);

         

          waitForImageToDisappearFromScreen("uni_foyer_general.png");

          SkipCutscene();

         

          waitForImageToAppearAtScreen("uni_foyer_general.png",10);

          Tap(765, 23);

         

         

          waitForImageToAppearAtScreen("main_menu.png",10);

          Tap(645, 163);

         

          Wait(10000);

        log("Success.");

    }

   

    private void Wait(int delay) throws Exception {Thread.sleep(delay);}

   

    private void SkipCutscene() throws Exception {

      TouchActions AndroidTouchActions = new TouchActions(driver);

      AndroidTouchActions.down((int) (Math.round(xcorrect+400*resize)), (int) (Math.round(ycorrect+233*resize))).perform();

          Thread.sleep(100);

          AndroidTouchActions.up((int) (Math.round(xcorrect+400*resize)), (int) (Math.round(ycorrect+233*resize))).perform(); 

    }

   

    private void Tap(int xposition, int yposition) throws Exception {

      TouchActions AndroidTouchActions = new TouchActions(driver);

      AndroidTouchActions.down((int) (Math.round(xcorrect+xposition*resize)), (int) (Math.round(ycorrect+yposition*resize))).perform();

          Thread.sleep(100);

          AndroidTouchActions.up((int) (Math.round(xcorrect+xposition*resize)), (int) (Math.round(ycorrect+yposition*resize))).perform(); 

    }

   

    private void SkipComment(int delaybefore, int delay, int delayafter, int count) throws Exception {

      Actions AndroidActions = new Actions(driver);

      Thread.sleep(delaybefore);

      for (int i = 0; i < count; i++) {

            AndroidActions.sendKeys(SelendroidKeys.BACK).perform();

            Thread.sleep(delay);

      }

      Thread.sleep(delayafter);

    }

   

    private void UseInventoryItem(int xpositionstart, int ypositionstart, int xposition, int yposition) throws Exception {

      TouchActions AndroidTouchActions = new TouchActions(driver);   

          AndroidTouchActions.down((int) (Math.round(xcorrect+680*resize)), (int) (Math.round(ycorrect+23*resize))).perform();

          Thread.sleep(100);

          AndroidTouchActions.up((int) (Math.round(xcorrect+680*resize)), (int) (Math.round(ycorrect+23*resize))).perform();

          Thread.sleep(500);

      AndroidTouchActions.down((int) (Math.round(xcorrect+xpositionstart*resize)), (int) (Math.round(ycorrect+ypositionstart*resize))).perform();

          Thread.sleep(100);

          int i = (int) (Math.round(xcorrect+xpositionstart*resize));

          while(i > (xcorrect+1))

          {

            i = i - 20;

            AndroidTouchActions.move(i, (int) (Math.round(ycorrect+ypositionstart*resize))).perform();

                Thread.sleep(1);

          }

          AndroidTouchActions.move((int) (Math.round(xcorrect+1)), (int) (Math.round(ycorrect+ypositionstart*resize))).perform();

          Thread.sleep(500);

          AndroidTouchActions.move((int) (Math.round(xcorrect+xposition*resize)), (int) (Math.round(ycorrect+yposition*resize))).perform();

          Thread.sleep(100);

          AndroidTouchActions.up((int) (Math.round(xcorrect+xposition*resize)), (int) (Math.round(ycorrect+yposition*resize))).perform();

    }

   

    private void swipe(int xpositionstart, int ypositionstart, String swipedirection, int swipelength, int swipeduration) throws Exception {

      Point StartingPoint = new Point((int) (Math.round(xcorrect+xpositionstart*resize)), (int) (Math.round(ycorrect+ypositionstart*resize)));

      FlickDirection Direction = FlickDirection.fromString(swipedirection);

      TouchAction ta = new TouchActionBuilder().flick(StartingPoint, Direction, swipelength, swipeduration).build();

      ta.perform(driver);    

    }

   

    private void detectGameViewport(String image, int Retries) throws Exception {

      int LengthAxisX = 0, width = 0, height = 0, LengthAxis1 = 0, LengthAxis2 = 0;

      double xGapLeft = 0, xGapRight = 0, yGapTop = 0, yGapBottom= 0;

     

      if (ImageRecognition.getAndroidScreenSize().width > ImageRecognition.getAndroidScreenSize().height) {

            width = (int) ImageRecognition.getAndroidScreenSize().width;

            height = (int) ImageRecognition.getAndroidScreenSize().height;

      }

      else {

            height = (int) ImageRecognition.getAndroidScreenSize().width;

            width = (int) ImageRecognition.getAndroidScreenSize().height;

      }

     

      ImageRecognitionSettings settings = new ImageRecognitionSettings();

      settings.setRetryWaitTime(1);

      settings.setRetries(Retries);

      ImageSearchResult imageSearch = findImageOnScreen(image, settings);

 

      LengthAxis1 = Math.abs((int) (imageSearch.getImageLocation().getTopLeft().x - imageSearch.getImageLocation().getTopRight().x));

      LengthAxis2 = Math.abs((int) (imageSearch.getImageLocation().getTopLeft().y - imageSearch.getImageLocation().getBottomLeft().y));

            if (LengthAxis1 > LengthAxis2) {

                  LengthAxisX = LengthAxis1;

                  if (Math.abs((int) imageSearch.getImageLocation().getTopRight().x) > (width/2)){

                        xGapLeft = imageSearch.getImageLocation().getTopLeft().x;

                        xGapRight = width - imageSearch.getImageLocation().getTopRight().x;     

                  }

                  else {

                        xGapRight = width - imageSearch.getImageLocation().getTopLeft().x;

                        xGapLeft = imageSearch.getImageLocation().getTopRight().x;

                  }

                  if (Math.abs((int) imageSearch.getImageLocation().getBottomLeft().y) > (height/2)){

                        yGapTop = imageSearch.getImageLocation().getTopLeft().y;

                        yGapBottom = height - imageSearch.getImageLocation().getBottomLeft().y;   

                  }

                  else {

                        yGapBottom = height - imageSearch.getImageLocation().getTopLeft().y;

                        yGapTop = imageSearch.getImageLocation().getBottomLeft().y;     

                  }

            }

            else {

                  LengthAxisX = LengthAxis2;

                  if (Math.abs((int) imageSearch.getImageLocation().getBottomLeft().y) > (width/2)){

                        xGapLeft = imageSearch.getImageLocation().getTopLeft().y;

                        xGapRight = width - imageSearch.getImageLocation().getBottomLeft().y;

                  }

                  else {

                        xGapRight = width - imageSearch.getImageLocation().getTopLeft().y;

                        xGapLeft = imageSearch.getImageLocation().getBottomLeft().y;

                  }

                  if (Math.abs((int) imageSearch.getImageLocation().getTopRight().x) > (height/2)){

                        yGapTop = imageSearch.getImageLocation().getTopLeft().x;

                        yGapBottom = height - imageSearch.getImageLocation().getTopRight().x;     

                  }

                  else {

                        yGapTop = height - imageSearch.getImageLocation().getTopLeft().x;

                        yGapBottom = imageSearch.getImageLocation().getTopRight().x;

                  }

            }

             

      if (Math.abs(xGapLeft) > Math.abs(xGapRight)){

            if ((Math.abs(xGapLeft + xGapRight) / 2) < (xGapRight + 10)) { xcorrect = (int) Math.round((Math.abs(xGapLeft + xGapRight) / 2));}

            else { xcorrect = (int) Math.round((Math.abs(xGapRight)));}

      }

      else {

            if ((Math.abs(xGapLeft + xGapRight) / 2) < (xGapLeft + 10)) { xcorrect = (int) Math.round((Math.abs(xGapLeft + xGapRight) / 2));}

            else { xcorrect = (int) Math.round((Math.abs(xGapLeft)));}

      }

      if (Math.abs(yGapTop) > Math.abs(yGapBottom)){

            if ((Math.abs(yGapTop + yGapBottom) / 2) < (yGapBottom + 10)) { ycorrect = (int) Math.round((Math.abs(yGapTop + yGapBottom) / 2));}

            else { ycorrect = (int) Math.round((Math.abs(yGapBottom)));}

      }

      else {

            if ((Math.abs(yGapTop + yGapBottom) / 2) < (yGapTop + 10)) { ycorrect = (int) Math.round((Math.abs(yGapTop + yGapBottom) / 2));}

            else { ycorrect = (int) Math.round((Math.abs(yGapTop)));}

      }

 

      resize = (double) LengthAxisX / (double) 800;

      log("left border: " + xcorrect + " | top border: " + ycorrect + " | position rescale factor: " + resize);      

      assert(imageSearch.isFound());

    }

   

}