package com.kfzs.duanduan.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;

import com.sheep.jiuyan.samllsheep.R;

/**
 * 带进度按钮
 * Created by HooRang on 2017/2/17.
 */
public class KFProgressButton extends android.support.v7.widget.AppCompatButton {


    public static final int TAG_1 = R.id.view_tag_1;

    public static final int TAG_2 = R.id.view_tag_2;

    public static final int TAG_3 = R.id.view_tag_3;

    public static final int TAG_4 = R.id.view_tag_4;

    public static final int TAG_5 = R.id.view_tag_5;

    public static final int TAG_6 = R.id.view_tag_6;

    public static final int TAG_7 = R.id.view_tag_7;

    public static final int TAG_8 = R.id.view_tag_8;

    private STATUS mStatus;

    private String mDefaultBtnStr = "下载";


    public enum STATUS {
        STATUS_INIT,
        STATUS_INSTALLING,
        STATUS_PROGRESSING,
        STATUS_PAUSED,
        STATUS_FINISH,
        STATUS_FAIL,
        STATUS_DELETE
    }

    public STATUS getStatus() {
        return mStatus == null ? STATUS.STATUS_INIT : mStatus;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    private int position;

    private Paint mPaint = new Paint();

    public KFProgressButton(Context context) {
        super(context);
    }

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

    public KFProgressButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }


    @Override
    protected void onDraw(Canvas canvas) {

//        mPaint.setColor(ThemeUtils.getInstance().getColorDark());
//        mPaint.setAntiAlias(true);
//        mPaint.setStrokeWidth(2.0f);
//
//        Rect rect = new Rect();
//        canvas.getClipBounds(rect);
//        rect.left += getPaddingLeft() + 1;
//        rect.top += getPaddingTop();
//        rect.bottom -= getPaddingBottom();
//        rect.right = (rect.left - getPaddingLeft()) + (0 * getWidth() / 100) - getPaddingRight() - 1;
//        int width = rect.right - rect.left;
//
//        if (width < 10) {
//            double tmp = 10 - Math.sqrt(10 * 10 - (10 - width) * (10 - width));
//            rect.top = (int) (rect.top + tmp + 0.5f);
//            rect.bottom = (int) (rect.bottom - tmp + 0.5f);
//            canvas.drawRoundRect(new RectF(rect), width, width, mPaint);
//        } else {
//            canvas.drawRoundRect(new RectF(rect), 10, 10, mPaint);
//        }

        super.onDraw(canvas);
    }

    public void updateButtonUI(STATUS status, boolean isPkgInstalled) {
        if(isPkgInstalled){
            setBtnProgressingStyle("打开");
            return;
        }
        this.mStatus = status;
        switch (status) {
            case STATUS_INIT://这个地方，有时候是预下载，这样重新按键后会变成安装，所以弄个defaultstr
                mDefaultBtnStr = "下载|预约".contains(getText().toString()) ? getText().toString() : "下载";
            case STATUS_DELETE:
                resetButton();
                break;
            case STATUS_INSTALLING:
            case STATUS_PROGRESSING:
                setBtnProgressingStyle("下载中");
                break;
            case STATUS_PAUSED:
                setBtnProgressingStyle("已暂停");
                break;
            case STATUS_FINISH:
                updateProgress(100);
                break;
            case STATUS_FAIL:
                this.setText("下载失败");
                break;
        }
    }


    public void resetButton() {
//        this.setBackgroundResource(R.drawable.progress_button_normal);
//        this.setTextColor(ThemeUtils.getInstance().getColorMain());
        this.setText(mDefaultBtnStr);
        invalidate();
    }


    /**
     * 设置变PROGRESSING模式
     *
     * @param progress
     */
    public void updateProgress(int progress) {
        if(progress == 100)
            setBtnProgressingStyle("安装");
        else
            setBtnProgressingStyle("下载中");

    }


    private void setBtnProgressingStyle(String text) {
        this.setText(text);
//        this.setBackgroundResource(R.drawable.button_full_disable);
//        this.setTextColor(Color.WHITE);
        invalidate();
    }

}
