/* 
 * Copyright (C) 2008 ZXing authors
 * Copyright 2011 Robert Theis
 * Copyright 2015 Jakub Kříž
 *
 * 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 cz.muni.fi.digitrecognition;

import java.util.List;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.Log;

/**
 * Encapsulates the result of OCR.
 */
public class OcrResult {
  private Bitmap bitmap;
  private String text;
  private float consumedData;

  private long timestamp;
  private long recognitionTimeRequired;

  private Paint paint;

  public OcrResult() {
    timestamp = System.currentTimeMillis();
    this.paint = new Paint();
    this.consumedData = 0f;
  }

  public Bitmap getBitmap() {
    return getAnnotatedBitmap();
  }

  
  private Bitmap getAnnotatedBitmap() {
    Canvas canvas = new Canvas(bitmap);
	paint.setAlpha(0xFF);
	paint.setColor(0xFF00CCFF);
	paint.setStyle(Style.STROKE);
	paint.setStrokeWidth(3);
	int position = 0;
	while(getText().charAt(position) != ']' && position != getText().length())
		position++;
	if(position != getText().length()) {
		String temp = getText().substring(1, position);
		String[] parts = temp.split(",");
		if(parts.length == 4) {
			int left   = Integer.parseInt(parts[0]) +1;
			int top    = Integer.parseInt(parts[1]) +1;
			int right  = Integer.parseInt(parts[2]) -1;
			int bottom = Integer.parseInt(parts[3]) -1;
			Rect r = new Rect(left, top, right, bottom);
			canvas.drawRect(r, paint);
		}
	}

    
    return bitmap;
  }

    public float getConsumedData() {
        return consumedData;
    }

    public void setConsumedData(float consumedData) {
        this.consumedData = consumedData;
    }

  public String getText() {
    return text;
  }


  public long getRecognitionTimeRequired() {
    return recognitionTimeRequired;
  }

  public Point getBitmapDimensions() {
    return new Point(bitmap.getWidth(), bitmap.getHeight());
  }

  public long getTimestamp() {
    return timestamp;
  }

  public void setBitmap(Bitmap bitmap) {
    this.bitmap = bitmap;
  }

  
  public void setText(String text) {
    this.text = text;
  }

  public void setRecognitionTimeRequired(long recognitionTimeRequired) {
    this.recognitionTimeRequired = recognitionTimeRequired;
  }

    //
    @Override
    public String toString() {
        return text + " " + recognitionTimeRequired + " " + timestamp;
    }
}
