package ru.colibri.ui.steps.ios;

import io.appium.java_client.TouchAction;
import io.appium.java_client.ios.IOSElement;
import org.jbehave.core.annotations.When;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.springframework.stereotype.Component;
import ru.colibri.ui.core.steps.AbsSteps;
import ru.yandex.qatools.allure.annotations.Step;

@Component
public class BaseIOSPickerWheelSteps extends AbsSteps {


    private String pickerWheelXPath = "//XCUIElementTypePickerWheel";
    private double stepToNext = 0.2;
    private double stepToLast = 0.35;

    @Step
    @When("установить пикер в следующее значение")
    public void setNextPickerWheelValue() {
        WebElement webElement = driver.findElement(By.xpath(pickerWheelXPath));
        setNextPickerWheelValue(webElement);
    }

    @Step
    @When("установить пикер в предыдущее значение")
    public void setPrevPickerWheelValue() {
        WebElement webElement = driver.findElement(By.xpath(pickerWheelXPath));
        setPrevPickerWheelValue(webElement);
    }

    @Step
    @When("установить пикер в первое значение")
    public void setFirstPickerWheelValue() {
        WebElement webElement = driver.findElement(By.xpath(pickerWheelXPath));
        Point center = ((IOSElement) webElement).getCenter();
        Dimension size = ((IOSElement) webElement).getSize();
        int height = size.getHeight();
        Point target = new Point(center.getX(), center.getY() - (int) (height * stepToLast));
        TouchAction touchAction = new TouchAction(driver);
        touchAction.press(target.getX(), target.getY()).release();
        touchAction.perform();
    }

    @Step
    @When("установить пикер в последнее значение")
    public void setLastPickerWheelValue() {
        WebElement webElement = driver.findElement(By.xpath(pickerWheelXPath));
        Point center = ((IOSElement) webElement).getCenter();
        Dimension size = ((IOSElement) webElement).getSize();
        int height = size.getHeight();
        Point target = new Point(center.getX(), center.getY() + (int) (height * stepToLast));
        TouchAction touchAction = new TouchAction(driver);
        touchAction.press(target.getX(), target.getY()).release();
        touchAction.perform();
    }

    public void setNextPickerWheelValue(WebElement pickerWheelElement) {
        setNextPickerWheelValue(pickerWheelElement, stepToNext);
    }


    public void setPrevPickerWheelValue(WebElement pickerWheelElement) {
        setPrevPickerWheelValue(pickerWheelElement, stepToNext);
    }


    public void setNextPickerWheelValue(WebElement pickerWheelElement, double step) {
        Point center = ((IOSElement) pickerWheelElement).getCenter();
        Dimension size = ((IOSElement) pickerWheelElement).getSize();
        int height = size.getHeight();
        TouchAction touchAction = new TouchAction(driver);
        touchAction.press(center.getX(), center.getY() + (int) (height * step)).release();
        touchAction.perform();
    }


    public void setPrevPickerWheelValue(WebElement pickerWheelElement, double step) {
        Point center = ((IOSElement) pickerWheelElement).getCenter();
        Dimension size = ((IOSElement) pickerWheelElement).getSize();
        int height = size.getHeight();
        TouchAction touchAction = new TouchAction(driver);
        touchAction.press(center.getX(), center.getY() - (int) (height * step)).release();
        touchAction.perform();
    }
}

@@ -515,21 +515,23 @@
 %0A%0A    pr
-ivate
+otected
  String 
@@ -579,37 +579,39 @@
 erWheel%22;%0A    pr
-ivate
+otected
  double stepToNe
@@ -630,13 +630,15 @@
   pr
-ivate
+otected
  dou

access modifications private -> protected