package com.datarecovery.master.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.Nullable;

import com.atmob.common.ui.SizeUtil;
import com.datarecovery.master.R;

public class WatermarkView extends View {

    private float textWidth;
    private Paint paint;
    private int textColor;
    private int textSize;
    private float rotate;
    private int alpha;
    private String watermarkText;

    public WatermarkView(Context context) {
        this(context, null);
    }

    public WatermarkView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WatermarkView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WatermarkView);
        textColor = typedArray.getColor(R.styleable.WatermarkView_watermark_color, 0);
        alpha = typedArray.getInt(R.styleable.WatermarkView_watermark_alpha, 255);
        textSize = typedArray.getDimensionPixelSize(R.styleable.WatermarkView_watermark_textSize, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics()));
        watermarkText = typedArray.getString(R.styleable.WatermarkView_watermark_text);
        rotate = typedArray.getInt(R.styleable.WatermarkView_watermark_rotate, -15);
        typedArray.recycle();

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(textSize);
        paint.setColor(textColor);
        paint.setAlpha(alpha);
        paint.setFakeBoldText(true);

        textWidth = paint.measureText(watermarkText);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        canvas.rotate(rotate);
        int index = 0;
        for (int positionY = height / 10; positionY <= height; positionY += height / 10 + 70) {
            float fromX = -width + (index++ % 2) * textWidth;
            for (float positionX = fromX; positionX < width; positionX += textWidth * 2) {
                canvas.drawText(watermarkText, positionX, positionY, paint);
            }
        }
    }
}
