package hh.dejsem.hack;

import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;

import java.io.File;

import hh.dejsem.Act;
import hh.dejsem.K;
import hh.dejsem.net.SrvCmd;

/**
 * The type Hack a.
 */
public class HackA extends Act {

	final static String TITLETEXT = "hack";

//	TitleIndiv title;
	Uri uri;
	View.OnLongClickListener longClickToUpload = new View.OnLongClickListener() {
		@Override 
		public boolean onLongClick(View v) { return handleUpload(); } 
	};
	
	@Override 
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		d.tv = new TextView(this);
		d.sc = new ScrollView(this);
		d.sc.addView(d.tv);
		setContentView(d.sc);
	}

	void analyze() {
		d.tv.setText(d.logTag + "\n\n");
		Intent i = getIntent();
		d.tv.append("action=" + i.getAction() + "\n");
		if(d.ll(4)) {
			d.l("data=" + i.getData() + ", getType=" + i.getType());
			d.l("toUri()=" + i.toUri(0));
			d.l("getExtras()=" + i.getExtras());
			d.l("Intent.EXTRA_STREAM=" + Intent.EXTRA_STREAM);
		}
		d.tv.append("describeContents()=" + i.describeContents() + "\n");
		d.tv.append("getAction()=" + i.getAction() + "\n");
		d.tv.append("getCategories()=" + i.getCategories() + "\n");
		d.tv.append("getComponent()=" + i.getComponent() + "\n");
		d.tv.append("getData()=" + i.getDataString() + "\n");
		d.tv.append("getDataString()=" + i.getDataString() + "\n");
		d.tv.append("getExtras()=" + i.getExtras() + "\n");
		if(i.getExtras() != null) d.tv.append("describeContents()=" + i.getExtras().describeContents() + "\n");
		d.tv.append("hasExtra[EXTRA_TEXT]=" + i.hasExtra(Intent.EXTRA_TEXT) + "\n");
		d.tv.append("EXTRA_TEXT=" + i.getStringExtra(Intent.EXTRA_TEXT) + "\n");
		d.tv.append("hasExtra[EXTRA_STREAM]=" + i.hasExtra(Intent.EXTRA_STREAM) + "\n");
		d.tv.append("EXTRA_STREAM=" + (Uri)i.getParcelableExtra(Intent.EXTRA_STREAM) + "\n");
		d.tv.append("getFlags()=" + i.getFlags() + "\n");
		d.tv.append("getPackage()=" + i.getPackage() + "\n");
		d.tv.append("getScheme()=" + i.getScheme() + "\n");
		d.tv.append("getType()=" + i.getType() + "\n");
		d.tv.append("hasFileDescriptors()=" + i.hasFileDescriptors() + "\n");
		if(i.getCategories() != null) {
			d.tv.append("categores:\n");
			for(String s: i.getCategories()) d.tv.append("  " + s + "\n"); }
		if(i.getExtras() != null) {
			d.tv.append("Bundle:\n");
			Bundle b = i.getExtras();
			d.tv.append(i.getExtras().toString() + "\n");
			if(b.containsKey(K.FN_KEY)) d.tv.append("file=" + b.getString(K.FN_KEY) + "\n");
			uri = (Uri)b.getParcelable(Intent.EXTRA_STREAM);
			if(uri != null) {
				d.tv.append("URI:\n");
				d.tv.append("isAbsolute()=" + uri.isAbsolute() + "\n");
				d.tv.append("isHierarchical()=" + uri.isHierarchical() + "\n");
				d.tv.append("getScheme()=" + uri.getScheme() + "\n");
				d.tv.append("getAuthority()=" + uri.getAuthority() + "\n");
				d.tv.append("getHost()=" + uri.getHost() + "\n");
				d.tv.append("getFragment()=" + uri.getFragment() + "\n");
				d.tv.append("getPath()=" + uri.getPath() + "\n");
				d.tv.append("getScheme()=" + uri.getScheme() + "\n");

				if(d.ll(4)) {
					d.l(String.format("getParcelable(EXTRA_STREAM)=%s", uri.toString()));
					d.l(String.format("uri=%s", uri));
					d.l(String.format("uri.path=%s", uri.getPath()));
					ContentResolver cr = getContentResolver();
					d.l(String.format("canonicalized=%s", cr.canonicalize(uri)));
					try {
						java.net.URI juri = new java.net.URI(uri.getPath());
						d.l(String.format("java.net.URI=%s", juri));
//						File furi = new File(juri);
//						d.l(String.format("uri file=%s", furi));
					} catch(Exception e) { d.abendMsg(e); finish(); }
				}
			}
		}
	}

	boolean handleUpload() {
		try {
			d.l(4, "handleUpload, START");
			int port = (Integer) SrvCmd.LONGTASK.exec(d, null);
			K.cmdConnClose(d);
			if(port == 0) throw new Exception("all server ports busy", null);
			String to = "exposed";
			Bundle parms = new Bundle();
			parms.putInt(K.PORT_KEY, port);
			d.l(4, String.format("uri=%s", uri.toString()));
//			InputStream fileInputStream=yourContext.getContentResolver().openInputStream(uri);
			String[] selection = new String[] { new File(new java.net.URI(uri.getPath())).getPath() };
			parms.putStringArray(K.FILES_FROM_KEY, selection);
			parms.putString(K.FILE_TO_KEY, to);
//			TaskProgress taskProgress = new TaskProgress(d, FileList.reckon(selection));
	
	//		fmsrv.iSvc.sendToService(D.COPY_TO_SRV, taskRef);	// execution under Service
			d.l(4, "handleUpload, FINISH");
			return true;
		}
		catch(Exception e) { 
			d.abendMsg(e);
			return false;
		}
	}
}
