package net.sadaji.pressure;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.View;

public class DataView extends View {
    private MainActivity m_MainActivity;
    //constractor
    DataView(Context cntext){
        super(cntext);
        m_MainActivity = (MainActivity)cntext;
        setBackgroundColor(Color.GREEN);
    }

    @Override
    protected void onDraw(Canvas canvas){
        double pressure = m_MainActivity.getPressure();
        drawData(canvas, pressure, "大気圧", 120);
    }

    protected void drawData(Canvas canvas, double data, String name, int top){
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        String str =  String.format("%s:%.1fhPa", name, data);;
        RectF rc = new RectF();
        rc.left = 60;
        rc.right = getWidth() - 60;
        rc.top = top;
        rc.bottom = rc.top + 140;
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setColor(Color.rgb(255, 255, 0));
        canvas.drawRoundRect(rc, 60, 60, paint);

        paint.setTextSize(98);
        paint.setColor(Color.rgb(0, 0, 0));
        float textWidth = paint.measureText(str);
        int x = (int)(rc.left + ((rc.right  - rc.left) - textWidth) / 2);
        Paint.FontMetrics metrics = new Paint.FontMetrics();
        float textHeight = paint.getFontMetrics(metrics);
        int y = (int)(rc.bottom - ((rc.bottom  - rc.top) - textHeight) / 2 );
        y -= metrics.descent;
        canvas.drawText(str, x, y, paint);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawRoundRect(rc, 60, 60, paint);

    }

}
