package com.videonext.sdk_test;

import java.util.Iterator;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import com.videonext.*;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		MediaClient mc = new MediaClient("192.168.0.197", (short) 80, false, 15);
	
		System.err.println("Loging in...");
		AsynchResult<Map<String,Object>> ar = mc.login("admin", "topse");
		Result<Map<String,Object>> r = ar.get(60000); // blocking until get result
		System.err.println("Error: " + r.error + " Error string: " + r.errorString);
		System.err.println("Default role: " + r.value.get("DEFAULT_ROLE"));
		
//		System.err.println("Getting roles...");
//		AsynchResult<Map<Integer/*id*/, String>> ar_roles = mc.getRoles();
//		Result<Map<Integer/*id*/, String>> r_roles = ar_roles.get(60000);
//		System.err.println("Error: " + r_roles.error + " Error string: " + r_roles.errorString);
//		if (r_roles.error == 0)
//		{
//			for (Map.Entry<Integer, String> entry : r_roles.value.entrySet()) {
//				Integer id = entry.getKey();
//				String name = entry.getValue();
//				System.err.println(id + ":" + name);	  
//			}
//		}
		Result<Map<Integer/*id*/, String>> r_roles = mc.getRoles().get(60000);
	
		System.err.println("Getting sets...");
		AsynchResult<Map<Integer/*id*/, String>> ar_sets = mc.getSets(-1);
		Result<Map<Integer/*id*/, String>> r_sets = ar_sets.get(60000);
		System.err.println("Error: " + r_sets.error + " Error string: " + r_sets.errorString);
		if (r_sets.error == 0)
		{
			for (Map.Entry<Integer, String> entry : r_sets.value.entrySet()) {
				Integer id = entry.getKey();
				String name = entry.getValue();
				System.err.println(id + ":" + name);	  
			}
		}

		System.err.println("Getting objects...");
		AsynchResult<Map<Integer/*id*/, Entity>> ar_objs = mc.getObjects(-1);
		Result<Map<Integer/*id*/, Entity>> r_objs = ar_objs.get(60000);
		System.err.println("Error: " + r_objs.error + " Error string: " + r_objs.errorString);
		if (r_objs.error == 0)
		{
			for (Map.Entry<Integer, Entity> entry : r_objs.value.entrySet()) {
				Integer id = entry.getKey();
				Entity entity = entry.getValue();
				System.err.println("Obj: " + id);  
				/*
		    	for (Map.Entry<String, String> entry2 : entity.attrs.entrySet())
		    	{
		    		String key = entry2.getKey();
		    		String val = entry2.getValue();
		    		System.err.println(key + "=" + val);
		    	}
				 */
			}
		}
		
		System.err.println("Getting media url...");
		AsynchResult<String> ar_url = mc.getMediaURL(275, 0, 0, 0, "h264", "320x240", 3);
		Result<String> r_url = ar_url.get(60000);
		System.err.println("Error: " + r_url.error + " Error string: " + r_url.errorString);
		if (r_url.error == 0)
			System.err.println("URL: "  + r_url.value);
		
		System.err.println("Getting snapshot...");
		AsynchResult<byte[]> ar_snapshot = mc.getSnapshot(275, 0, 1, false);
		Result<byte[]> r_snapshot = ar_snapshot.get(60000);
		System.err.println("Error: " + r_snapshot.error + " Error string: " + r_snapshot.errorString);
		if (r_snapshot.error == 0)
		{
			Bitmap bitmap = BitmapFactory.decodeByteArray(r_snapshot.value, 0, r_snapshot.value.length);
			System.err.println("Snapshot: "  + bitmap.getHeight() + "x" + bitmap.getWidth());
		}
		
		
		
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
