package net.yaht.m.trycust;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 *
 * Created by lisp on 3/12/17.
 *
 */

public class RoundB extends View {


    private String _str_width = null; // in dp(s);
    private String _str_height = null; // in dp(s);

    private int _foreground_color = Color.GREEN;
    private int _alternative_color = Color.YELLOW;

    private Paint mRoundPaint = null;

    private static final String _LOG_TAG = "=-= RoundB";
    public void log2me(String where, String what) {
        Log.v(_LOG_TAG + "." + where, what);
    }

    /*
        NOTE: Creation, Constructors:

        There is a form of the constructor that are called when the view is created from code and
        a form that is called when the view is inflated from a layout file.
        The second form should parse and apply any attributes defined in the layout file.
     */

    public RoundB(Context context) {

        super(context);
        log2me("RoundB(context)", "happened...");

        mRoundPaint = new Paint();
        mRoundPaint.setColor(_foreground_color);

    }

    public RoundB(Context context, AttributeSet attrs) {

        super(context, attrs);

        // NOTE: https://developer.android.com/reference/android/util/AttributeSet.html

        _str_width = attrs.getAttributeValue(
            "http://schemas.android.com/apk/res/android",
             "layout_width"
        );

        _str_height = attrs.getAttributeValue(
            "http://schemas.android.com/apk/res/android",
            "layout_height"
        );

        String _foreground = attrs.getAttributeValue(
            "http://schemas.android.com/apk/res/android",
            "foreground"
        );

        if (_foreground != null)
            _foreground_color = Color.parseColor(_foreground);

        mRoundPaint = new Paint();
        mRoundPaint.setColor(_foreground_color);

        log2me("RoundB(context,attrs)", "_width: " + _str_width);
        log2me("RoundB(context,attrs)", "_height: " + _str_height);

        log2me("RoundB(context,attrs)", "_foreground_color: " + Integer.toHexString(_foreground_color));

    }



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

        // NOTE: must understand what is our size (based on layout manager decisions) !!!

        int _min_w = getPaddingLeft() + getPaddingRight() + getSuggestedMinimumWidth();
        int w = resolveSizeAndState(_min_w, widthMeasureSpec, 1);

        int _min_h = MeasureSpec.getSize(w) + getPaddingBottom() + getPaddingTop();
        int h = resolveSizeAndState(_min_h, heightMeasureSpec, 0);

        int _spec_mode_w = MeasureSpec.getMode(w); // UNSPECIFIED, AT_MOST, EXACTLY
        int _spec_size_w = MeasureSpec.getSize(w);

        int _spec_mode_h = MeasureSpec.getMode(h);
        int _spec_size_h = MeasureSpec.getSize(h);

        log2me("onMeasure(w)", Integer.toString(_spec_size_w));
        log2me("onMeasure(h)", Integer.toString(_spec_size_h));

        setMeasuredDimension(w, h);

    }

    protected void onDraw(Canvas canvas) {

        log2me("onDraw", "happened...");

        super.onDraw(canvas);

        int w = canvas.getWidth();
        int h = canvas.getHeight();

        log2me("onDraw.w", Integer.toString(w));
        log2me("onDraw.h", Integer.toString(h));

        float x = (float) (w / 2.0);
        float y = (float) (h / 2.0);
        float r;

        // NOTE: and without this?

        if (w < h) {

            r = (float) (w / 2.0);

        } else {

            r = (float) (h / 2.0);

        }

        log2me("onDraw.canvas", ".drawCircle()");

        // and if our Paint is null ... ?

        canvas.drawCircle(
            x,
            y,
            r,
            mRoundPaint
        );

    }


    public boolean onTouchEvent(MotionEvent event) {

        log2me("onTouchEvent", "happened...");

        int eventAction = event.getAction();

        // we you may need the x/y location!
        int x = (int)event.getX();
        int y = (int)event.getY();

        // our code here to handle the actual event;
        switch (eventAction) {
            case MotionEvent.ACTION_DOWN:
                log2me("onTouchEvent", "DOWN...");
                mRoundPaint.setColor(_alternative_color);
                break;
            case MotionEvent.ACTION_UP:
                log2me("onTouchEvent", "UP...");
                mRoundPaint.setColor(_foreground_color);
                break;
            case MotionEvent.ACTION_MOVE:
                log2me("onTouchEvent", "MOVE...");
                break;
        }

        // tell the View to redraw the Canvas;
        invalidate();

        // tell the View that we got (handled) the event;
        return true;
    }

}
