package net.studymongolian.mongollibrary;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ViewConfiguration;

import static android.content.ContentValues.TAG;


public class KeyShift extends KeyImage {

    private static final int CAPS_STATE_INDICATOR_INDENT = 5; // px
    private static final int CAPS_STATE_INDICATOR_RADIUS = 5; // px

    private static final long DOUBLE_CLICK_TIME_DELTA = ViewConfiguration.getDoubleTapTimeout();

    long lastClickTime = 0;

    private boolean isShiftOn = false;
    private boolean isCapsLockOn = false;
    private Paint mCapsStatePaint;
    private static final int DEFAULT_CAPS_STATE_INDICATOR_COLOR = Color.RED;

    GestureDetector gestureDetector;

    public KeyShift(Context context) {
        super(context);
        init(context);
    }

    public KeyShift(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public KeyShift(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mCapsStatePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCapsStatePaint.setStyle(Paint.Style.FILL);
        mCapsStatePaint.setColor(DEFAULT_CAPS_STATE_INDICATOR_COLOR);
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawCircleRepresentingTheShiftState(canvas);
        // TODO: draw something to represent the caps lock state
    }

    private void drawCircleRepresentingTheShiftState(Canvas canvas) {

        if (!isShiftOn) return;

        // make sure a large border radius doesn't cover the indicator
        float indent = CAPS_STATE_INDICATOR_INDENT;
        float borderRadiusAdjustment = (float) (mBorderRadius * ( 1 - (1 / Math.sqrt(2))));
        indent += borderRadiusAdjustment;

        int indicatorDiameter = 2 * CAPS_STATE_INDICATOR_RADIUS;

        int dx = getPaddingLeft() + mKeyWidth - indicatorDiameter - (int) indent;
        int dy = getPaddingTop() + indicatorDiameter + (int) indent;

        // draw indicator
        canvas.save();
        canvas.translate(dx, dy);
        canvas.drawCircle(0, 0, CAPS_STATE_INDICATOR_RADIUS, mCapsStatePaint);
        canvas.restore();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        changeBackgroundColorForClickEvent(event);
        int action = event.getActionMasked();
        switch(action) {
            case (MotionEvent.ACTION_DOWN) :
                return true;
            case (MotionEvent.ACTION_UP) :
                differentiateSingleDoubleTap();
                return true;
            default :
                return super.onTouchEvent(event);
        }
    }

    private void differentiateSingleDoubleTap() {
        long clickTime = System.currentTimeMillis();
        if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA){
            onSecondTapOfDoubleTap();
        } else {
            onSingleTapUp();
        }
        lastClickTime = clickTime;
    }

    private void onSingleTapUp() {
        Log.i(TAG, "onSingleTapUp: ");
        isCapsLockOn = false;
        isShiftOn = !isShiftOn;
        setShift(isShiftOn);
        invalidate();
    }

    private void onSecondTapOfDoubleTap() {
        if (isShiftOn) {
            isCapsLockOn = true;
            invalidate();
        }
    }

    private class GestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            Log.i(TAG, "onSingleTapUp: ");
            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.i(TAG, "onDoubleTap: ");
            return true;
        }
    }

    public void turnOffCapsUnlessLocked() {
        if (isCapsLockOn) return;
        if (!isShiftOn) return;
        isShiftOn = false;
        setShift(isShiftOn);
        invalidate();
    }

    public void setShiftImage(Keyboard.Theme theme) {
        int imageResourceId;
        if (theme == Keyboard.Theme.LIGHT) {
            imageResourceId = R.drawable.ic_keyboard_shift_black_32dp;
        } else {
            imageResourceId = R.drawable.ic_keyboard_shift_white_32dp;
        }
        setImage(BitmapFactory.decodeResource(getResources(), imageResourceId));
    }

    public void setCapsStateIndicatorColor(int color) {
        mCapsStatePaint.setColor(color);
        invalidate();
    }
}
@@ -239,70 +239,8 @@
 et;%0A
-import android.util.Log;%0Aimport android.view.GestureDetector;%0A
 impo
@@ -312,58 +312,8 @@
 n;%0A%0A
-import static android.content.ContentValues.TAG;%0A%0A
 %0Apub
@@ -813,46 +813,8 @@
 D;%0A%0A
-    GestureDetector gestureDetector;%0A%0A
     
@@ -877,39 +877,32 @@
 );%0A        init(
-context
 );%0A    %7D%0A%0A    pu
@@ -990,39 +990,32 @@
 );%0A        init(
-context
 );%0A    %7D%0A%0A    pu
@@ -1143,23 +1143,16 @@
    init(
-context
 );%0A    %7D
@@ -1175,31 +1175,16 @@
 id init(
-Context context
 ) %7B%0A    
@@ -1365,87 +1365,8 @@
 R);%0A
-        gestureDetector = new GestureDetector(context, new GestureListener());%0A
     
@@ -3171,47 +3171,8 @@
 ) %7B%0A
-        Log.i(TAG, %22onSingleTapUp: %22);%0A
     
@@ -3436,498 +3436,8 @@
  %7D%0A%0A
-    private class GestureListener extends GestureDetector.SimpleOnGestureListener %7B%0A%0A        @Override%0A        public boolean onDown(MotionEvent e) %7B%0A            return true;%0A        %7D%0A%0A        @Override%0A        public boolean onSingleTapUp(MotionEvent e) %7B%0A            Log.i(TAG, %22onSingleTapUp: %22);%0A            return true;%0A        %7D%0A%0A        @Override%0A        public boolean onDoubleTap(MotionEvent e) %7B%0A            Log.i(TAG, %22onDoubleTap: %22);%0A            return true;%0A        %7D%0A    %7D%0A%0A
     
@@ -3582,33 +3582,29 @@
    setShift(
-isShiftOn
+false
 );%0A        i

Clean up