/*
* This file is part of the Remote Sensor Actuator Interface (ReSAI).
*
* Copyright(c) Andreas Kipp, Frederic Siepmann
* http://opensource.cit-ec.de/projects/resai
*
* This file may be licensed under the terms of of the
* GNU Lesser General Public License Version 3 (the ``LGPL''),
* or (at your option) any later version.
*
* Software distributed under the License is distributed
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the LGPL for the specific language
* governing rights and limitations.
*
* You should have received a copy of the LGPL along with this
* program. If not, go to http://www.gnu.org/licenses/lgpl.html
* or write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The development of this software was supported by the
* Excellence Cluster EXC 277 Cognitive Interaction Technology.
* The Excellence Cluster EXC 277 is a grant of the Deutsche
* Forschungsgemeinschaft (DFG) in the context of the German
* Excellence Initiative.
*
*/

/**
 * Tab showing information from laser sensor.
 *
 * @author akipp
 */

package de.unibi.airobots.resaidroid.tabactivities;

import org.jivesoftware.smack.util.Base64;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import de.unibi.airobots.resaidroid.R;
import de.unibi.airobots.resaidroid.helper.TimeConverter;
import de.unibi.airobots.resaidroid.tabactivities.template.TabTemplate;

public class TabLaserSensor extends TabTemplate {

	private ImageView ivLaser;
	private TextView lblStatus;
	private static Matrix matrix;
	private byte[] imageByte;
	private Bitmap bitmap;
	private Bitmap rotatedBMP;
	private String _img = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lasersensor);

		ivLaser = (ImageView) findViewById(R.id.ivLaser);
		lblStatus = (TextView) findViewById(R.id.lblStatus);
		matrix = new Matrix();
		Log.i(TAG, matrix.toString());
		matrix.postRotate(90);

		addStatus("created");
	}

	@Override
	public void processProperties() {

		messageHandler.post(new Runnable() {
			public void run() {

				
				synchronized (receivedProperties) {
					for (String prop : receivedProperties.keySet()) {
						if (prop.equals(cm.getComponent(TAG) + "_IMG")) {
							_img = receivedProperties.get(prop).toString();
						}
					}
				}
				if (_img != null) {
					try {

//						imageByte = DataPacker.decompressBytes(Base64
//								.decode(_img));
						imageByte = Base64.decode(_img,Base64.GZIP);
						Log.i(TAG, "draw bitmap");
						bitmap = BitmapFactory.decodeByteArray(
								imageByte, 0, imageByte.length);

						if (bitmap != null) {

							rotatedBMP = Bitmap.createBitmap(bitmap, 0,
									0, bitmap.getWidth(), bitmap.getHeight(),
									matrix, true);

							ivLaser.setImageBitmap(rotatedBMP);
						} else {
							Log.i(TAG, "ROTATE BITMAP IS NULL");
						}
					} catch (Exception e) {
						Log.i(TAG, e.getMessage());
					}

				} else {
					Log.i(TAG, "IMAGE IS NULL");
				}
			}
		});
	}

	private void addStatus(String message) {
		lblStatus.append("\n"
				+ TimeConverter.toTime(System.currentTimeMillis()) + ": "
				+ message);
	}
}