package hh.dejsem.trash;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import hh.dejsem.K;
import hh.dejsem.Prefs;
import hh.dejsem.R;
import hh.dejsem.fm.NetTaskRef;
import hh.lib.D;

/**
 * ... obsah 1.řádky (záhlaví) okna GUI aktivit a jejich fragmentů</br>
 * ● záhlaví má 4 pole
 * <ol>
 *     <im>app id</im>
 *     <im>proměnné stavové pole probíhajících přenosů</im>
 *     <im>momentálně nastavený server a číslo šifrovaného kanálu</im>
 *     <im>label aktuální komponenty GUI</im>
 * </ol></br>
 * ● každá komponenta GUI
 *
 */
public class TitleStat extends Toolbar implements Runnable,  Handler.Callback {
	static boolean pending = false;
	public static void setPending() { pending = true; }
	public static void unSetPending() {
		pending = false;
	}

	ViewGroup container = null;

	D d;
	View titleView;
	ImageView pendingIcon;
	TextView pendingView;
	TextView titleTextView;
	String titleText = "";
	Handler handler = new Handler(this);
	public Messenger msgr = new Messenger(handler);

	public TitleStat(Context context, AttributeSet attrs) {
		super(context, attrs);
		d = new D(context, getClass().getSimpleName());
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch(msg.what) {
			case K.MSG_WATCH_PROGRESS:
				update();
				return true;
		}
		return false;
	}

	@Override
	public void run() {
		update();
	}

	public TitleStat activate() {
		final Context c = d.getContext();
		final Resources r = c.getResources();
		if(Prefs.host == null) new Prefs().refreshPrefs(c);

		((TextView)findViewById(R.id.title_left_text)).setText(String.format("%s.%s", r.getString(R.string.app_name), r.getString(R.string.app_version_name)));
		((TextView)findViewById(R.id.channel)).setText(String.format("%s ch %s", Prefs.host.subSequence(0, Prefs.host.indexOf('.')), r.getString(R.string.channel)));
		titleTextView = findViewById(R.id.title_right_text);
		pendingIcon = findViewById(R.id.pending_icon);
	    ((AnimationDrawable)pendingIcon.getBackground()).start();
//	    ((AnimationDrawable)((ImageView) this.titleView.findViewById(R.id.pending_icon)).getBackground()).start();
//		pendingIcon.setImageResource(R.drawable.pending);
		pendingView = findViewById(R.id.pending_progress);
//		OnClickListener lastMileListener = new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				c.startActivity(new Intent(c, LastMileMeterAct.class));
//			} };
//		findViewById(R.id.last_mile_button).setOnClickListener(lastMileListener);
		return this;
	}

	public TitleStat setText(String titleText) { this.titleText = titleText; return this; }

	public void update() {
		if(pending) {
			/*((NotificationManager)d.getContext().getSystemService(Context.NOTIFICATION_SERVICE)).getActiveNotifications().length
					// od API 23 se dá využít ke kontrole přítomnosti progres-notifkace na notification bar*/
			String progressText = "";
			synchronized(K.transfProgress.statSet) {
				for(NetTaskRef tr : K.transfProgress.statSet) {
					progressText += String.format("%2d%% ", tr.getProgress().es.getPct());
				}
			}
			pendingView.setText(progressText);
			handler.postDelayed(this, K.transfProgress.refreshInterval);
		}
		pendingView.setVisibility(pending ? View.VISIBLE : View.INVISIBLE);
		pendingIcon.setVisibility(pending ? View.VISIBLE : View.INVISIBLE);
		titleTextView.setText(titleText);
		invalidate();
	}
}

