package net.heartshield.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

import net.heartshield.prevent.R;

/**
 * @author David Madl (git@abanbytes.eu)
 * @date 2017-10-29
 */
public class CircleView extends View {
    private double mViewDiameter = 1.0;
    private double mMeasureSize;
    private int mWidth;
    private int mHeight;
    private Paint mPaint;

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

    private void init(Context context) {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(ContextCompat.getColor(context, R.color.square_image_background_highlight));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //canvas.drawCircle(x, y, radius, paint);
        canvas.drawCircle(mWidth / 2, mHeight / 2, (float) (mMeasureSize / 2.0 * (0.2 + mViewDiameter * 0.60)), mPaint);
    }

    public void setDiameter(double diameter) {
        mViewDiameter = diameter;
        mViewDiameter = Math.min(Math.max(mViewDiameter, 0.0), 1.0);
        invalidate();
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        /*
        if(false) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        }
        */

        //final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        final int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        //final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        final int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mMeasureSize = Math.min(widthSpecSize, heightSpecSize);
        mWidth = widthSpecSize;
        mHeight = heightSpecSize;

        /*
        if(widthSpecSize < heightSpecSize) {
            super.onMeasure(widthMeasureSpec, widthMeasureSpec);
            mMeasureSize = widthSpecSize;
        } else {
            super.onMeasure(heightMeasureSpec, heightMeasureSpec);
            mMeasureSize = heightSpecSize;
        }
        */
    }
}
