package hh.michelson;

import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import hh.lib.D;
import hh.lib.DA;

class Handle implements SeekBar.OnSeekBarChangeListener, View.OnLongClickListener {
	static final String labelDefault = "rowViewRes";
	static final double minDefault = 0f;
	static final double maxDefault = 100f;

	String key;
	String label = labelDefault;
	double min = minDefault;
	double max = maxDefault;
	double def = min;
	volatile double actual = def;

	View handleView;
	SeekBar bar;
	TextView actualDisp;
	TextView labelView;
	int listPosition = -1;

	D d;

	Handle(D d, HandleDef def, double actual) { this(d, def.Label, def.Min, def.Max, def.Default, actual, def.Key); }

	Handle(D d, String label, double min, double max, double def, double actual, String key) {
		this.d = d.klon(this);
		this.label = label;
		this.min = min;
		this.max = max;
		this.def = def;
		this.actual = actual;
		this.key = key;
	}

	/* listener implementation */
	@Override
	public void onStopTrackingTouch(SeekBar bar) { saveActual(); }

	@Override
	public void onStartTrackingTouch(SeekBar bar) { }

	@Override
	public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) {
		if(fromUser) { actual = progress2value(bar.getProgress()); }
		dispActual();
	}

	@Override
	public boolean onLongClick(View v) {
		reset();
		return true;
	}

	void dispActual() {
		actualDisp.setText(String.format(A.lo, "%.2f", actual));
		actualDisp.invalidate();
	}

	double getActual() { return actual; }

	int getActualInt() { return (int)Math.round(actual); }

	void adjustBar() {
//		d.l(String.format("+++ adjustBar, key=%s, actual=%f, bar=%d", key, actual, bar.hashCode()));
		bar.setProgress(value2progress(actual));
		bar.invalidate();
	}

	void setActual(double actual) {
		this.actual = actual;
		if(isInflated()) adjustBar();
		saveActual();
	}

	void reset() { setActual(def); }

	void saveActual() {
		DA.sp.edit().putFloat(key, Double.valueOf(actual).floatValue()).apply(); }

	boolean isInflated() {
		return (handleView != null && handleView.getId() == listPosition); }

	double progress2value(int progress) { return min + (max - min) * progress / C.maxBarProgress; }

	int value2progress(double value) { return (int)Math.round(C.maxBarProgress * (value - min) / (max - min)); }

	void infillView(View handleView, int listPosition) {
		this.listPosition = listPosition;
		this.handleView = handleView;
		handleView.setId(listPosition);
		labelView = ((TextView)handleView.findViewById(R.id.label));
		labelView.setText(label);
		labelView.setOnLongClickListener(this);
		final TextView tmin = (TextView)handleView.findViewById(R.id.min);
		if(tmin != null) tmin.setText(String.format(A.lo, "%.1f", min));
		final TextView tmax = (TextView)handleView.findViewById(R.id.max);
		if(tmax != null) tmax.setText(String.format(A.lo, "%.1f", max));
		bar = (SeekBar)handleView.findViewById(R.id.bar);
		bar.setMax(C.maxBarProgress);
		adjustBar();
		actualDisp = handleView.findViewById(R.id.actual);
		dispActual();
		bar.setOnSeekBarChangeListener(this);
		actualDisp.setOnLongClickListener(this);
	}
}
