package com.reactlibrary.vishwamdkyclib.Tracker;

/**
 * Vishwam Corp CONFIDENTIAL

 * Vishwam Corp 2018
 * All Rights Reserved.

 * NOTICE:  All information contained herein is, and remains
 * the property of Vishwam Corp. The intellectual and technical concepts contained
 * herein are proprietary to Vishwam Corp
 * and are protected by trade secret or copyright law of U.S.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Vishwam Corp
 */

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
/**
 * This is a custom view to draw out circular view with transparent color for circle and providing white color for the background.This view used on top of preview to capture on only face.
 * **/
public class OvalView extends View {

    private Bitmap bitmap;
    private Canvas cnvs;
    private Paint p = new Paint();
    private Paint transparentPaint = new Paint();
    private Paint semiTransparentPaint = new Paint();
    private int parentWidth;
    private int parentHeight;

    int height;
    int width;
    public RectF oval1;
    /**
     * These constructors are used for different uses cases.First for programmatic implementation  and other for layout implementations.
     * **/
    public OvalView(Context context) {
        super(context);
        init();
    }

    public OvalView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    /**
     * This is method initialises transparent paint object and background color paint object
     * **/
    private void init() {
        transparentPaint.setColor(getResources().getColor(android.R.color.transparent));
        transparentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        semiTransparentPaint.setColor(getResources().getColor(android.R.color.white));
    }

    /**
     * This method is called view init.Here we get display size and draw background white color and circular transparent color on canvas.
     * **/
    @SuppressLint({"DrawAllocation", "CanvasSize"})
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        DisplayMetrics metrics = new DisplayMetrics();
        ((Activity)getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics);

        height = metrics.heightPixels;
        width = metrics.widthPixels;
        bitmap = Bitmap.createBitmap(parentWidth, parentHeight, Bitmap.Config.ARGB_8888);
        cnvs = new Canvas(bitmap);

        oval1 = new RectF((width/2) - (width*2/5), (width*2/3) - (width*2/5), (width/2) + (width*2/5), (width*2/3) + (width*2/5));

        cnvs.drawRect(0, 0, cnvs.getWidth(), cnvs.getHeight(), semiTransparentPaint);
        cnvs.drawOval(oval1,transparentPaint );

        canvas.drawBitmap(bitmap, 0, 0, p);
    }
    /**
     * This method is called to get view measurements.This is basically called before view inflation.It return display measurments.
     * **/
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        parentHeight = MeasureSpec.getSize(heightMeasureSpec);

        this.setMeasuredDimension(parentWidth, parentHeight);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}