/*
 * Copyright (C) 2008 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.zxing.client.android;

import com.google.zxing.client.android.camera.CameraManager;
import com.alva.flutter.plugin.scanner.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.util.Log;

/**
 * This view is overlaid on top of the camera preview. It adds the viewfinder rectangle and partial
 * transparency outside it, as well as the laser scanner animation and result points.
 *
 * @author dswitkin@google.com (Daniel Switkin)
 */
public final class ViewfinderView extends View {

  private static final long ANIMATION_DELAY = 50L;

  private static final int SPEED_DISTANCE = 10;
  private static final int POINT_SIZE = 6;

  private CameraManager cameraManager;
  private final Paint paint;
  private final Path path;
  private final int maskColor;
  private static int laserColor;

  private int slideTop;
  private int slideMid;
  private Bitmap netBitmap;

  // This constructor is used when the class is built from an XML resource.
  public ViewfinderView(Context context, AttributeSet attrs) {
    super(context, attrs);

    // Initialize these once for performance rather than calling them every time in onDraw().
    path = new Path();
    paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Resources resources = getResources();
    maskColor = resources.getColor(R.color.viewfinder_mask);
    laserColor = resources.getColor(R.color.viewfinder_laser);
  }

  public void setCameraManager(CameraManager cameraManager, int laserColor) {
    this.cameraManager = cameraManager;
    if(laserColor != 0)
      ViewfinderView.laserColor = laserColor;
  }

  @SuppressLint("DrawAllocation")
  @Override
  public void onDraw(Canvas canvas) {
    if (cameraManager == null) {
      return; // not ready yet, early draw before done configuring
    }
    Rect frame = cameraManager.getFramingRect();
    Rect previewFrame = cameraManager.getFramingRectInPreview();
    if (frame == null || previewFrame == null) {
      return;
    }
    int width = getWidth();
    int height = getHeight();

    // Draw the exterior (i.e. outside the framing rect) darkened
    paint.reset();
    paint.setColor(maskColor);
    canvas.drawRect(0, 0, width, frame.top, paint);
    canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
    canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint);
    canvas.drawRect(0, frame.bottom + 1, width, height, paint);

    // Draw a red "laser scanner" line through the middle to show decoding is active
    if (path.isEmpty()) {
      int iWidth = width / 15;
      path.moveTo(frame.left + iWidth, frame.top);
      path.lineTo(frame.left, frame.top);
      path.lineTo(frame.left, frame.top + iWidth);
      path.moveTo(frame.right - iWidth, frame.top);
      path.lineTo(frame.right, frame.top);
      path.lineTo(frame.right, frame.top + iWidth);
      path.moveTo(frame.left + iWidth, frame.bottom);
      path.lineTo(frame.left, frame.bottom);
      path.lineTo(frame.left, frame.bottom - iWidth);
      path.moveTo(frame.right - iWidth, frame.bottom);
      path.lineTo(frame.right, frame.bottom);
      path.lineTo(frame.right, frame.bottom - iWidth);
    }
    paint.setColor(laserColor);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(POINT_SIZE);
    canvas.drawPath(path, paint);
    canvas.saveLayerAlpha(0, 0, width, height, 120, Canvas.ALL_SAVE_FLAG);

    if (slideTop <= SPEED_DISTANCE || slideTop >= frame.bottom) {
      slideTop = frame.top;
    }
    if (slideMid == 0)
      slideMid = frame.top + (frame.bottom - frame.top) / 2;
    if (slideTop < slideMid - 100) {
      paint.setAlpha((int) ((slideTop - frame.top) / (slideMid - frame.top - 100.0) * 255));
    } else if (slideTop > slideMid + 100) {
      paint.setAlpha((int) ((slideMid + 100.0 - frame.top) / (slideTop - frame.top) * 255));
    } else
      paint.setAlpha(255);
    if (netBitmap == null)
      netBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.scan_finder_net);

    Rect lineRect = new Rect();
    lineRect.left = frame.left;
    lineRect.right = frame.right;
    lineRect.top = slideTop - 400;
    lineRect.bottom = slideTop;
    canvas.drawBitmap(netBitmap, null, lineRect, paint);
    canvas.clipRect(0, 0, width, frame.top);
    canvas.drawColor(maskColor, PorterDuff.Mode.CLEAR);

    slideTop += SPEED_DISTANCE;

    // Request another update at the animation interval, but only repaint the laser line,
    // not the entire viewfinder mask.
    postInvalidateDelayed(ANIMATION_DELAY, frame.left - POINT_SIZE, frame.top,
        frame.right + POINT_SIZE, frame.bottom);
  }

  public void drawViewfinder() {
    invalidate();
  }

}
