package hh.dejsem.fm;

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TextView;

import hh.dejsem.Act;
import hh.dejsem.GetReady;
import hh.dejsem.K;
import hh.dejsem.Main;
import hh.dejsem.R;
import hh.dejsem.TitleBar;

/**
 * responds to intentions to send data to server
 */
public class UploadAct extends Act implements GetReady.ReadyListener {
	static final String TITLETEXT = "upload";
	
	@Override 
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		d.logPrefix += String.format("[%x]", hashCode());

		/*if(getClass().getPackage().getName().startsWith("hh.dejsem")) {   // +++
			K.UDP_PORT = 4224;
			d.l(String.format("========= application %s, %s =========",
					getResources().getString(R.string.app_name), getClass().getSimpleName()));    // +++ log eye catcher
		}*/
		Main.eyeCatcher(d, this);

		setContentView(R.layout.basic_layout);
		TitleBar.assign(this, R.id.title_bar, TITLETEXT);

		if(GetReady.isReady(d)) upload();
		else {
			TextView tv = new TextView(this);
			tv.setText("waiting for password & SD card permissions");
			((ViewGroup) findViewById(R.id.panel)).addView(tv);
			GetReady.getReady(d, this);
		}
	}

	@Override
	public void onStart() {
		super.onStart();
		if(K.title != null) K.title.reassign(this, R.id.title_bar).setText(TITLETEXT);
	}

	@Override
	public void onReady(boolean success) {
		if(success) upload();
		else finish();
	}

	void upload() {
		UploadFrag upload = (UploadFrag)getSupportFragmentManager().findFragmentByTag(K.UPLOAD_TAG);
		if (upload == null) {
			upload = UploadFrag.instantiate(d, getIntent());
		}
		getSupportFragmentManager()
				.beginTransaction()
				.replace(R.id.panel, upload, K.UPLOAD_TAG)
				.commitAllowingStateLoss();
	}
}
