package amai.org.conventions.events;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.support.v4.widget.CompoundButtonCompat;
import android.support.v7.widget.AppCompatCheckBox;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import amai.org.conventions.R;
import amai.org.conventions.ThemeAttributes;
import amai.org.conventions.model.EventType;

public class SearchCategoryBox extends LinearLayout {

	private EventType searchCategory;
	private AppCompatCheckBox checkBox;
	private TextView textView;

	public SearchCategoryBox(Context context, AttributeSet attrs) {
		super(context, attrs);

		LayoutInflater.from(this.getContext()).inflate(R.layout.search_category_box, this, true);
		checkBox = (AppCompatCheckBox) this.findViewById(R.id.search_category_checkbox);
		textView = (TextView) this.findViewById(R.id.search_category_text);
	}

	public void setSearchCategory(EventType searchCategory) {
		textView.setText(searchCategory.getDescription());
		int color;
		if (searchCategory.hasBackgroundColor()) {
			color = darkenColor(searchCategory.getBackgroundColor());
		} else {
			color = ThemeAttributes.getColor(getContext(), R.attr.eventTimeDefaultBackgroundColor);
		}
		CompoundButtonCompat.setButtonTintList(this.checkBox, ColorStateList.valueOf(color));
		this.searchCategory = searchCategory;
	}

	public EventType getSearchCategory() {
		return searchCategory;
	}

	public void toggle() {
		checkBox.setChecked(!checkBox.isChecked());
	}

	public void check() {
		checkBox.setChecked(true);
	}

	public boolean isChecked() {
		return checkBox.isChecked();
	}

	private int darkenColor(int color) {
		float[] hsv = new float[3];
		Color.colorToHSV(color, hsv);
		hsv[2] *= 0.9f;
		color = Color.HSVToColor(hsv);
		return color;
	}
}

@@ -1229,19 +1229,22 @@
 dColor()
+, 1
 );%0A
-
 %09%09%7D else
@@ -1341,16 +1341,239 @@
 r);%0A%09%09%7D%0A
+%09%09ColorStateList checkboxColors = new ColorStateList(%0A%09%09%09new int%5B%5D%5B%5D%7B%0A%09%09%09%09new int%5B%5D%7B-android.R.attr.state_checked%7D,%0A%09%09%09%09new int%5B%5D%7Bandroid.R.attr.state_checked%7D%0A%09%09%09%7D,%0A%09%09%09new int%5B%5D%7B%0A%09%09%09%09darkenColor(color, 2),%0A%09%09%09%09color%0A%09%09%7D);%0A
 %09%09Compou
@@ -1624,37 +1624,22 @@
 ox, 
-ColorStateList.valueOf(color)
+checkboxColors
 );%0A%09
@@ -1979,16 +1979,28 @@
 nt color
+, int factor
 ) %7B%0A%09%09fl
@@ -2057,16 +2057,54 @@
 , hsv);%0A
+%09%09for (int i = 0; i %3C factor; ++i) %7B%0A%09
 %09%09hsv%5B2%5D
@@ -2113,16 +2113,20 @@
 = 0.9f;%0A
+%09%09%7D%0A
 %09%09color 

Make unselected search category checboxes a bit darker