package com.tencent.liteav.demo.superplayer.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.tencent.liteav.demo.superplayer.util.TimerUtil;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicInteger;

public class UnlockProgressView extends androidx.appcompat.widget.AppCompatImageView {
    public interface OnUnlockListener {
        void startUnlock();

        void unlockSuccess();

        void unlockFailed();
    }

    private Paint paint;

    private final Path segmentPath = new Path();

    private final PathMeasure pathMeasure = new PathMeasure();

    private TimerUtil timerUtil;

    private OnUnlockListener unlockListener;

    public void setUnlockListener(OnUnlockListener listener) {
        this.unlockListener = listener;
    }

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

    public UnlockProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public UnlockProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(12f);
        paint.setStrokeCap(Paint.Cap.ROUND);

        timerUtil = new TimerUtil
                .Builder()
                .setPeriod(16)
                .setOnTick(new TimerUtil.OnTickListener() {
                    @Override
                    public void onStart() {
                        if (unlockListener != null) {
                            unlockListener.startUnlock();
                        }
                    }

                    @Override
                    public void onTick(int count, long period) {
                        if (count >= 120) {
                            if (unlockListener != null) {
                                getHandler().postDelayed(() -> {
                                    unlockListener.unlockSuccess();
                                }, 0);
                            }
                            timerUtil.reset();
                        }

                        invalidate();
                    }
                }).build();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Path entirePath = getPath(w ,h);
        pathMeasure.setPath(entirePath, true);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            timerUtil.start();
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            if (timerUtil.getCount() < 120 && unlockListener != null) {
                unlockListener.unlockFailed();
            }
            timerUtil.reset();
        }

        return true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);


    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        float pathLength = pathMeasure.getLength();
//        segmentPath.reset();
//        pathMeasure.getSegment(0, pathLength * timerUtil.getCount() / 120, segmentPath, true);
//
//        canvas.drawPath(segmentPath, paint);
    }

    private Path getPath(int width, int height) {
        Path path = new Path();
        final float radius = (width - getPaddingLeft() - getPaddingRight()) / 2;
        path.addCircle(width / 2, height / 2, radius, Path.Direction.CW);
        return path;
    }
}
