package io.techery.progresshint.addition;

import android.graphics.Point;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;
import io.techery.progresshint.ProgressHintDelegate;

public class VerticalProgressHintDelegate extends ProgressHintDelegate {

  public VerticalProgressHintDelegate(SeekBar seekBar, AttributeSet attrs, int defStyleAttr) {
    super(seekBar, attrs, defStyleAttr);
  }

  final int[] tmpCoords = new int[2];

  @Override protected PointF getHintDragCoordinates(MotionEvent event) {
    mSeekBar.getLocationOnScreen(tmpCoords);
    float x = (event.getRawY() - tmpCoords[1]) * getOrientation();
    float y = mSeekBar.getY();
    return new PointF(x, y);
  }

  @Override protected Point getFixedHintOffset() {
    int xOffset = getHorizontalOffset();
    int yOffset = getVerticalOffset(mSeekBar.getMax() / 2);
    return new Point(xOffset, yOffset);
  }

  @Override protected Point getFollowHintOffset() {
    int xOffset = getHorizontalOffset();
    int yOffset = getVerticalOffset(mSeekBar.getProgress());
    return new Point(xOffset, yOffset);
  }

  private int getHorizontalOffset() {
    switch (getOrientation()) {
      case 1:
        return mPopupOffset;
      case -1:
        return mSeekBar.getHeight() + mPopupOffset;
    }
    return 0;
  }

  private int getVerticalOffset(int progress) {
    int yOddOffset = 0;
    switch (getOrientation()) {
      case 1:
        //yOddOffset = offsetPadding + mPopupView.getMeasuredHeight() / 2;
        yOddOffset = mPopupView.getMeasuredHeight() / 2 + mSeekBar.getHeight() / 2 + 1;
        break;
      case -1:
        yOddOffset = mPopupView.getMeasuredHeight() / 2 - mSeekBar.getHeight() / 2 - 1;
        break;
    }
    return getFollowPosition(progress) - yOddOffset;
  }

  private int getOrientation() {
    return (int) (mSeekBar.getRotation() / 90f);
  }
}

@@ -99,16 +99,58 @@
 PointF;%0A
+import android.support.annotation.IntDef;%0A
 import a
@@ -291,16 +291,100 @@
 elegate;
+%0Aimport java.lang.annotation.Retention;%0Aimport java.lang.annotation.RetentionPolicy;
 %0A%0Apublic
@@ -1350,33 +1350,34 @@
 )) %7B%0A      case 
-1
+CW
 :%0A        return
@@ -1394,34 +1394,35 @@
 set;%0A      case 
--1
+CCW
 :%0A        return
@@ -1467,23 +1467,109 @@
     
-%7D%0A    return 0;
+  default:%0A        throw new IllegalStateException(%22This widget orientation is not supported%22);%0A    %7D
 %0A  %7D
@@ -1614,24 +1614,78 @@
 progress) %7B%0A
+    int followPosition = getFollowPosition(progress);%0A
     int yOdd
@@ -1694,12 +1694,8 @@
 fset
- = 0
 ;%0A  
@@ -1739,86 +1739,12 @@
 ase 
-1
+CW
 :%0A
-        //yOddOffset = offsetPadding + mPopupView.getMeasuredHeight() / 2;%0A
     
@@ -1826,11 +1826,12 @@
 / 2 
-+
+-
  1
+5
 ;%0A  
@@ -1832,29 +1832,58 @@
 15;%0A        
-b
 re
-ak
+turn followPosition - yOddOffset
 ;%0A      case
@@ -1887,10 +1887,11 @@
 ase 
--1
+CCW
 :%0A  
@@ -1944,17 +1944,17 @@
 t() / 2 
--
++
  mSeekBa
@@ -1967,23 +1967,19 @@
 eight() 
-/
+*
  2
- - 1
 ;%0A      
@@ -1984,163 +1984,449 @@
     
-b
 re
-ak;%0A    %7D%0A    return getFollowPosition(progress) - yOddOffset;%0A  %7D%0A%0A  private int getOrientation() %7B%0A    return (int) (mSeekBar.getRotation() / 90f);%0A  %7D
+turn -followPosition - yOddOffset;%0A      default:%0A        throw new IllegalStateException(%22This widget orientation is not supported%22);%0A    %7D%0A  %7D%0A%0A  @VerticalOrientation private int getOrientation() %7B%0A    return (int) (mSeekBar.getRotation() / 90f) == 1 ? CW : CCW;%0A  %7D%0A%0A  @Retention(RetentionPolicy.SOURCE) @IntDef(%7B CW, CCW %7D) public @interface VerticalOrientation %7B%7D%0A%0A  private static final int CW = 1;%0A  private static final int CCW = 0;
 %0A%7D%0A

Fix popup position for vertical ccw orientation