package com.xypower.gpuv.egl.filter;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.util.Log;

public class GlWatermarkFilter extends GlOverlayFilter {

    private Object mLocker = new Object();
    private Bitmap mBitmap;
    private boolean invalidated = true;
    private Position mPosition = Position.LEFT_TOP;

    public GlWatermarkFilter(Bitmap bitmap) {
        this.mBitmap = bitmap;
    }


    public GlWatermarkFilter(Bitmap bitmap, Position position) {
        mBitmap = bitmap;
        mPosition = position;
    }

    public Bitmap updateBitmap(Bitmap bm) {
        Bitmap oldBitmap = null;
        Log.d("OSD", "updateBitmap");
        synchronized (mLocker) {
            invalidated = true;
            oldBitmap = mBitmap;
            mBitmap = bm;
        }

        return oldBitmap;
    }

    @Override
    protected void drawCanvas(Canvas canvas) {
        synchronized (mLocker) {
            // Log.d("OSD", "drawCanvas invalidated=" + Boolean.toString(invalidated));
            if (invalidated) {
                canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
                canvas.drawBitmap(mBitmap, null, canvas.getClipBounds(), null);
                invalidated = false;
            }
        }

        /*
        if (bitmap != null && !bitmap.isRecycled()) {
            switch (position) {
                case LEFT_TOP:
                    // canvas.drawBitmap(bitmap, 0, 0, null);
                    canvas.drawBitmap(bitmap, null, canvas.getClipBounds(), null);
                    break;
                case LEFT_BOTTOM:
                    canvas.drawBitmap(bitmap, 0, canvas.getHeight() - bitmap.getHeight(), null);
                    break;
                case RIGHT_TOP:
                    canvas.drawBitmap(bitmap, canvas.getWidth() - bitmap.getWidth(), 0, null);
                    break;
                case RIGHT_BOTTOM:
                    canvas.drawBitmap(bitmap, canvas.getWidth() - bitmap.getWidth(), canvas.getHeight() - bitmap.getHeight(), null);
                    break;
            }
        }

         */
    }

    public enum Position {
        LEFT_TOP,
        LEFT_BOTTOM,
        RIGHT_TOP,
        RIGHT_BOTTOM
    }
}
