package net.yaht.m.tryview;

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 ...
 *
 */

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...");

        float _display_density = context.getResources().getDisplayMetrics().density;
        log2me("RoundB(context)", "display density: " + Float.toString(_display_density));

        /*

            possible densities:

            .75 on ldpi (120 dpi)
            1.0 on mdpi (160 dpi; baseline)
            1.5 on hdpi (240 dpi)
            2.0 on xhdpi (320 dpi)
            3.0 on xxhdpi (480 dpi)
            4.0 on xxxhdpi (640 dpi)

            more: https://material.io/devices/

         */

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

    }

    public RoundB(Context context, AttributeSet attrs) {

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

        float _display_density = context.getResources().getDisplayMetrics().density;
        log2me("RoundB(context,attrs)", "display density: " + Float.toHexString(_display_density));

        // 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 chosen layout and layout manager decision)!

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

        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); // UNSPECIFIED, AT_MOST, EXACTLY
        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();

        // NOTE: if we want x and y location inside our View
        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...");
                log2me("onTouchEvent", "x: " + Integer.toString(x) + ", y: " + Integer.toString(y));
                mRoundPaint.setColor(_alternative_color);
                break;
            case MotionEvent.ACTION_UP:
                log2me("onTouchEvent", "UP...");
                log2me("onTouchEvent", "x: " + Integer.toString(x) + ", y: " + Integer.toString(y));
                mRoundPaint.setColor(_foreground_color);
                break;
            case MotionEvent.ACTION_MOVE:
                log2me("onTouchEvent", "MOVE...");
                log2me("onTouchEvent", "x: " + Integer.toString(x) + ", y: " + Integer.toString(y));
                break;

        }

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

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

}
