package com.ym.library.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.ym.library.R;
import com.ym.library.utils.Utils;
import com.ym.modulecommon.AppliContext;

/**
 * Created by huchen on 2017/12/1.
 */
public class TimerReadProgressBar extends View {

    private int mMaxProgress = 100;

    private int mProgress = 0;

    private int mCenterText = 0;

    private final int mCircleLineStrokeWidth = Utils.dip2px(4);
    // 画圆所在的距形区域
    private RectF mRectF;
    private RectF mBitmapRectF;
    private RectF mCenterRectF;

    private Paint mPaint;

    private int dp5, dp11, dp8, dp38, dp44, mTextSize;

    private Bitmap mProgressBg, mCenterBg;

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

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

    private void init() {
        mRectF = new RectF();
        mBitmapRectF = new RectF();
        mCenterRectF = new RectF();
        mPaint = new Paint();

        dp11 = Utils.dip2px(11);
        dp5 = Utils.dip2px(3);
        dp8 = Utils.dip2px(7);
        dp38 = Utils.dip2px(19);
        dp44 = Utils.dip2px(22);
        mTextSize = Utils.dip2px(18);

        Glide.with(AppliContext.get()).asBitmap().load(R.drawable.icon_timer_read_bg).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                mProgressBg = resource;
            }
        });
        Glide.with(AppliContext.get()).asBitmap().load(R.drawable.icon_timer_read_center_bg).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                mCenterBg = resource;
            }
        });

        setBackgroundResource(R.drawable.icon_timer_read_bg);
        setPaint();

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = this.getWidth();
        int height = this.getHeight();

        if (width != height) {
            int min = Math.min(width, height);
            width = min;
            height = min;
        }
        if (mPaint == null) {
            return;
        }
        drawProgress(canvas, width, height);
        if (0 == getProgress()) {
            drawCenterCenterIcon(canvas, width, height);
        } else {
            drawProgressText(canvas, width, height);
        }
    }

    private String mTextContent;

    private void drawProgressText(Canvas canvas, int cx, int cy) {
        mPaint.setColor(0xfff7403b);
        mPaint.setTextSize(mTextSize);
        mPaint.setTypeface(Typeface.DEFAULT_BOLD);
        mPaint.setStrokeWidth(0);
        mTextContent = mProgress + "s";
        float textWidth = mPaint.measureText(mTextContent);
        canvas.drawText(mTextContent, (cx / 2) - textWidth / 2, (cx / 2) + (mTextSize / 2 - dp5), mPaint);
    }

    private void drawCenterCenterIcon(Canvas canvas, int width, int height) {
        mCenterRectF.left = dp44;
        mCenterRectF.top = dp38;
        mCenterRectF.right = width - dp44;
        mCenterRectF.bottom = height - dp38;
        if (mCenterBg != null && !mCenterBg.isRecycled()) {
            canvas.drawBitmap(mCenterBg, null, mCenterRectF, mPaint);
        }
    }

    private void drawProgress(Canvas canvas, int width, int height) {
        mRectF.left = dp11; // 左上角x
        mRectF.top = dp11; // 左上角y
        mRectF.right = width - dp11; // 左下角x
        mRectF.bottom = height - dp11; // 右下角y
        mPaint.setColor(0xfff7403b);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mCircleLineStrokeWidth);
        canvas.drawArc(mRectF, -90, ((float) mProgress / mMaxProgress) * 360, false, mPaint);
    }

    private void setPaint() {
        // 设置画笔相关属性
        mPaint.setAntiAlias(true);
    }

    public int getMaxProgress() {
        return mMaxProgress;
    }

    public void setMaxProgress(int maxProgress) {
        this.mMaxProgress = maxProgress;
    }


    public void setProgress(int progress) {
        if (progress >= mMaxProgress) {
            this.mProgress = 0;
        } else {
            this.mProgress = progress;
        }
        this.invalidate();
    }

    public int getProgress() {
        return mProgress;
    }

    public void setProgressNotInUiThread(int progress) {
        this.mProgress = progress;
        this.postInvalidate();
    }


    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }
}
