package hh.dejsem.hack;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.ArrayList;

import hh.dejsem.Act;
import hh.dejsem.R;
import hh.lib.D;

public class HackNotifListAct extends Act implements DialogInterface.OnKeyListener, DialogInterface.OnClickListener, View.OnClickListener {
	static int cnt = 0;
	static ArrayList<LinearLayout> statSet = null;

	HackListAdapter la = null;
	TextView progressFiles, progressFn, progressNum;
	ProgressBar progressBar;
	ImageView cancelButt;
	int toRemove = -1;

	@Override
	public void onCreate(Bundle b) {
		super.onCreate(b);
		statSet = new ArrayList<LinearLayout>();
		statSet.add(fillLv(1));
		statSet.add(fillLv(2));
		statSet.add(fillLv(3));
		la = new HackListAdapter(d);
		new AlertDialog.Builder(this)
				.setAdapter(la, this)
				.setTitle("test notif activity")
				.setOnKeyListener(this)
				.show();
	}

	@Override
	public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
		d.l(String.format("+++ onKey, keyCode=%d, KeyEvent.KEYCODE_BACK=%d, dialog interface=%s", keyCode, KeyEvent.KEYCODE_BACK, dialog));
		if(keyCode == KeyEvent.KEYCODE_BACK) {
			d.l("+++ key down");
			finishActivity(dialog);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		d.l("+++ onClick, which=" + which);
		if(which == DialogInterface.BUTTON_POSITIVE && toRemove > -1) {
			HackNotifListAct.statSet.remove(toRemove);
			toRemove = -1;
			if(statSet.size() == 0) finishActivity(dialog);
			else la.notifyDataSetChanged();
		}
	}

	@Override
	public void onClick(View v) {
		d.l("+++ onClick, view tag=" + v.getTag());
		toRemove = ((int)(v.getTag()) - 1);
		new AlertDialog.Builder(this)
				.setMessage("cancel ?")
				.setNegativeButton("cancel", null)
				.setPositiveButton("OK", this)
				.setOnKeyListener(this)
				.show();
	}

	void finishActivity(DialogInterface dialog) {
		if(dialog != null) dialog.dismiss();
		finish();
	}

	LinearLayout fillLv(int position) {
		final LayoutInflater lInflater = LayoutInflater.from(d.getContext());
		LinearLayout lv = (LinearLayout)lInflater.inflate(R.layout.progress_entry, null);
		progressFiles = ((TextView)(lv.findViewById(R.id.progressFiles)));
		progressFiles.setText(String.valueOf(position*3));
		progressFn = ((TextView)(lv.findViewById(R.id.progressFn)));
		progressFn.setText("test file " + position);
		progressNum = ((TextView)(lv.findViewById(R.id.progressNum)));
		progressNum.setText(String.valueOf(position));
		progressBar = ((ProgressBar)(lv.findViewById(R.id.progressBar)));
		progressBar.setProgress(position*20);
		cancelButt = ((ImageView)(lv.findViewById(R.id.cancel)));
		cancelButt.setTag(position);
		cancelButt.setClickable(true);
		cancelButt.setOnClickListener((View.OnClickListener)(d.getAct()));
		return lv;
	}

	class HackListAdapter extends BaseAdapter {
		/**----------------------------------------------------------
		 * progress list adapter
		 * ----------------------------------------------------------*/
		D d;
		/* ----------------------------------------------------------*/

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

		public int getCount() { return statSet.size(); }

		public Object getItem(int position) { return null; }

		public long getItemId(int position) { return position; }

		public int getViewTypeCount() { return 8; }

		public View getView(int position, View listEntry, ViewGroup parent) {
			d.l(String.format("getView, position=%d, view=%x", position, listEntry == null ? 0 : listEntry.hashCode()));
			if(position < statSet.size()) {
				LinearLayout lv = HackNotifListAct.statSet.get(position);
				if(d.ll(5)) d.l(String.format("getView return, position=%d, listEntry=%x, listRow=%x", position, listEntry.hashCode(), lv.hashCode()));
				return lv;
			}
			else return null;
		}
	}
}
