package edu.byu.suite.features.diningLocations.controller;

import android.app.Activity;
import android.app.AlertDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by romrell4 on 6/16/17
 */

class AllergenDialog {
	private static List<String> allAllergens = new ArrayList<>();
	private static boolean[] selectedIndexes;

	static List<String> getSelectedAllergens() {
		List<String> filteredAllergens = new ArrayList<>();
		for (int i = 0; i < allAllergens.size(); i++) {
			if (selectedIndexes[i]) {
				filteredAllergens.add(allAllergens.get(i));
			}
		}
		return filteredAllergens;
	}

	static void setAllergens(List<String> allergens) {
		allAllergens = allergens;
		selectedIndexes = new boolean[allergens.size()];
	}

	static void show(Activity activity, AllergenListener listener) {
		boolean[] tempSelected = selectedIndexes.clone();
		new AlertDialog.Builder(activity)
				.setTitle("Check an allergen to remove menu items with that allergen")
				//You have to implement the listener here, or else the tempSelected array won't be modified
				.setMultiChoiceItems(allAllergens.toArray(new String[allAllergens.size()]), tempSelected, (dialog, which, isChecked) -> {})
				.setPositiveButton("Save", (dialog, which) -> {
					selectedIndexes = tempSelected;
					if (listener != null) {
						listener.allergensUpdated();
					}
				})
				.setNegativeButton("Cancel", null)
				.show();
	}

	interface AllergenListener {
		void allergensUpdated();
	}
}
