package com.gh.gamecenter.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.gh.gamecenter.common.R;
import com.gh.gamecenter.core.utils.DisplayUtils;

public class CircleProgressBar extends View {
    private int height;
    private int width;
    private Paint mPaint;
    private int strokeWidth = 5;//线条宽度
    private RectF rectF;


    private int normalColor = Color.parseColor("#999999");//普通的颜色
    private int progressColor = Color.parseColor("#2496FF");//已经走了的进度条颜色
    private int textColor = Color.parseColor("#1F89EC");//文字颜色

    private int disableNormalColor = Color.parseColor("#80999999");//禁用普通的颜色
    private int disableProgressColor = Color.parseColor("#999999");//禁用已经走了的进度条颜色
    private int disableTextColor = Color.parseColor("#999999");//禁用文字颜色

    private int currentNormalColor;
    private int currentProgressColor;
    private int currentTextColor;

    private float textSize = 20;//文字大小
    private int progress = 0;//进度条
    private String centerText = "0%";//中心填充文字
    private Paint fontPaint = null;
    private Paint.Style progress_style = Paint.Style.STROKE;//填充式还是环形式
    private boolean mIsShowProgressText;

    public CircleProgressBar(Context context) {
        this(context, null);
    }

    public CircleProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressBar);
        textSize = array.getDimension(R.styleable.CircleProgressBar_text_size, textSize);
        centerText = array.getString(R.styleable.CircleProgressBar_text) == null ? centerText : array.getString(R.styleable.CircleProgressBar_text);
        strokeWidth = DisplayUtils.dip2px(array.getInteger(R.styleable.CircleProgressBar_stroke_width, strokeWidth));

        textColor = array.getColor(R.styleable.CircleProgressBar_text_color, textColor);
        normalColor = array.getColor(R.styleable.CircleProgressBar_normal_color, normalColor);
        progressColor = array.getColor(R.styleable.CircleProgressBar_progress_color, progressColor);

        disableTextColor = array.getColor(R.styleable.CircleProgressBar_disable_text_color, disableTextColor);
        disableNormalColor = array.getColor(R.styleable.CircleProgressBar_disable_normal_color, disableNormalColor);
        disableProgressColor = array.getColor(R.styleable.CircleProgressBar_disable_progress_color, disableProgressColor);

        progress = array.getInt(R.styleable.CircleProgressBar_progress, progress);
        progress_style = array.getInt(R.styleable.CircleProgressBar_progress_style, 0) == 0 ? Paint.Style.STROKE : Paint.Style.FILL;

        mIsShowProgressText = array.getBoolean(R.styleable.CircleProgressBar_show_progress, true);

        currentNormalColor = normalColor;
        currentProgressColor = progressColor;
        currentTextColor = textColor;
        array.recycle();
        initPaint();
    }

    // 2.初始化画笔
    private void initPaint() {
        mPaint = new Paint();
        mPaint.setColor(currentNormalColor);       //设置画笔颜色
        mPaint.setAntiAlias(true);
        mPaint.setStyle(progress_style);  //设置画笔模式为描边
        mPaint.setStrokeWidth(strokeWidth);         //设置画笔宽度为10px

        fontPaint = new Paint();
        fontPaint.setTextSize(textSize);
        fontPaint.setAntiAlias(true);
        fontPaint.setColor(currentTextColor);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        height = MeasureSpec.getSize(heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);

        if (height > width)//高大于宽的情况
        {
            rectF = new RectF(strokeWidth, (height / 2 - width / 2) + strokeWidth, width - strokeWidth, (height / 2 + width / 2) - strokeWidth);
        } else if (width > height)//宽大于高的情况
        {
            rectF = new RectF((width / 2 - height / 2) + strokeWidth, strokeWidth, (width / 2 + height / 2) - strokeWidth, height - strokeWidth);
        } else//宽等于高的情况
        {
            rectF = new RectF(strokeWidth, strokeWidth, width - strokeWidth, height - strokeWidth);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    Paint.FontMetrics fontMetrics = null;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(currentNormalColor);
        if (progress < 360) {
            canvas.drawArc(rectF, 270 + progress, 360 - progress, progress_style == Paint.Style.FILL, mPaint);
        }

        mPaint.setColor(currentProgressColor);
        canvas.drawArc(rectF, 270, progress, progress_style == Paint.Style.FILL, mPaint);

        if (mIsShowProgressText) {
            fontMetrics = fontPaint.getFontMetrics();
            float textWidth = fontPaint.measureText(centerText);
            float textHeight = fontPaint.ascent() + fontPaint.descent();
            canvas.drawText(centerText, (float) width / 2 - textWidth / 2, (float) height / 2 - textHeight / 2, fontPaint);
        }
    }


    /**
     * 更新界面
     *
     * @param progress
     * @param text
     */
    public void update(int progress, String text) {
        this.progress = progress;
        this.centerText = text;
        postInvalidate();
    }

    /**
     * 是否禁用
     *
     * @param isDisable
     */
    public void isDisable(boolean isDisable) {
        currentNormalColor = isDisable ? disableNormalColor : normalColor;
        currentProgressColor = isDisable ? disableProgressColor : progressColor;
        currentTextColor = isDisable ? disableTextColor : textColor;
        fontPaint.setColor(currentTextColor);
        postInvalidate();
    }
}
