package hh.dejsem.hack;

import android.content.DialogInterface;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;

import java.util.GregorianCalendar;

import hh.dejsem.K;
import hh.dejsem.R;
import hh.dejsem.hack.HackViewDialog;
import hh.lib.D;

public class HackDfUse implements HackViewDialog.ViewDialogView, DialogInterface.OnClickListener, View.OnClickListener, Runnable {

	D d;
	HackViewDialog hackDF;
	Handler hackHandler = new Handler();
	ImageView hackView = null;
	boolean hackSw = false;
	final String hackTag = "zkus";

	HackDfUse(D d) { this.d = d.klon(this); }

	void onActivityCreate() {
		if(K.base.containsKey(hackTag)) {
			final HackViewDialog df = (HackViewDialog) K.base.get(hackTag);
			d.l("+++ notice dialog text getView=" + df.getView.hashCode());
			if(hackDF != null && hackDF.isDetached()) hackHandler.postDelayed(this, 1000);
		}
	}

	void dialogFragmentTest() {
		d.l("+++ hack, getView=" + this.hashCode());
		hackHandler.removeCallbacks(this);
		if(hackDF != null) hackDF.dismiss();
		hackDF = new HackViewDialog();
		hackDF.display(d, hackTag, this, this);
		hackHandler.postDelayed(this, 1000);
	}

	void hackSetDfView() {
		hackSw = !hackSw;
		hackView.setImageResource(hackSw ? R.raw.menu : R.raw.up);
	}

	@Override
	public View getView() {
		hackView = new ImageView(d.getContext());
		hackSetDfView();
		return hackView;
	}

	@Override
	public void run() {
		if(hackDF.isDetached()) return;
		hackSetDfView();
		hackView.invalidate();
		hackHandler.postDelayed(this, 1000);
	}

	@Override
	public void onClick(DialogInterface i, int which) {
		d.l(String.format("+++ DialogInterface OnClick, this=%x, which=%d", hashCode(), which));
	}

	@Override
	public void onClick(View v) {
		final GregorianCalendar cal = new GregorianCalendar();
		final String s = String.format("● %d.%02d.%02d %02d:%02d:%02d.%03d UTC+%d",
				cal.get(cal.YEAR), cal.get(cal.MONTH) + 1, cal.get(cal.DAY_OF_MONTH),
				cal.get(cal.HOUR_OF_DAY), cal.get(cal.MINUTE), cal.get(cal.SECOND), cal.get(cal.MILLISECOND),
				((cal.get(cal.ZONE_OFFSET) + cal.get(cal.DST_OFFSET)) / (3600 * 1000)));
		new HackViewDialog().display(d, "zkouška převracení", "zkouška převracení", s,null);
	}

}
