package com.abbidot.tracker.widget;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.appcompat.widget.AppCompatSeekBar;

/**
 * 可以通过STATUS设置 Seekbar禁止点击禁止滑动等
 */
public class NoClickSlideSeekBar extends AppCompatSeekBar {
    private static final int PADDING_DEFAULT = 50;

    public enum STATUS {
        STATUS_ENABLE, STATUS_CLICK, STATUS_SLIDE, STATUS_UNABLE
    }

    /**
     * 填充大小
     */
    private final int mPaddingSize = PADDING_DEFAULT;

    private STATUS mStaus = STATUS.STATUS_ENABLE;

    private Rect mThumbRect;

    private int mX, mY;
    //是否可以滑动
    private boolean isCanSile = true;

    private NoClickSlideSeekBar(Context context) {
        super(context);
    }

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

    private NoClickSlideSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (MotionEvent.ACTION_DOWN == event.getAction()) {
            isCanSile = true;
            mThumbRect = getThumb().getBounds();
            mX = (int) event.getX();
            mY = (int) event.getY();

            if (mStaus == STATUS.STATUS_CLICK && !checkBound()) {
                isCanSile = false;
                return true;
            }
            if (mStaus == STATUS.STATUS_SLIDE && checkBound()) {
                isCanSile = false;
                return true;
            }
            if (mStaus == STATUS.STATUS_UNABLE) {
                isCanSile = false;
                return true;
            }

        }
        if (!isCanSile)
            return true;
        return super.onTouchEvent(event);
    }

    /**
     * 判断当前触摸点 是否在滑块之类-mPaddingSize是为了提高体验，因为有些滑块太小
     *
     * @return
     */
    private boolean checkBound() {

        if (mX < mThumbRect.left - mPaddingSize || mX > mThumbRect.right + mPaddingSize || mY < mThumbRect.top - mPaddingSize || mY > mThumbRect.bottom + mPaddingSize) {
            return true;
        }
        return false;
    }

    public void setStatus(STATUS status) {
        this.mStaus = status;
    }

}



