package de.tudarmstadt.informatik.hostage.ui2.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;

import java.util.ArrayList;

import de.tudarmstadt.informatik.hostage.R;
import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;

//import android.content.Intent;

@SuppressLint("ValidFragment")
public class ChecklistDialog extends DialogFragment {
	
	private ArrayList<Integer> mSelectedItems;
	private ArrayList<String> itemTitles;
    private ChecklistDialogListener mListener;
    
    private int selectedIndex;
    private boolean isMultipleChoice;
    
    public String title;
	
	
    @SuppressLint("ValidFragment")
	public interface ChecklistDialogListener {
        public void onDialogPositiveClick(ChecklistDialog dialog);
        public void onDialogNegativeClick(ChecklistDialog dialog);
    }
    
    public String getTitle(){
    	return this.title;
    }
    
    public boolean isMultipleChoice(){
    	return this.isMultipleChoice;
    }
    
    public ChecklistDialog(String title, ArrayList<String> itemTitles, boolean[] selected, boolean isMultipleChoice , ChecklistDialogListener listener){
    	mListener = listener;
    	this.mSelectedItems = new ArrayList<Integer>();
	    
    	this.isMultipleChoice = isMultipleChoice;
	    this.title = title;
	    this.itemTitles = itemTitles;

	    
	    
	    boolean[] selectedArray = new boolean[this.itemTitles.size()];

	    if(this.isMultipleChoice){
		    for(int i = 0; i < this.itemTitles.size(); i++){
		    	boolean isSelected = selected[i];
		    	selectedArray[i] = isSelected;
		    	if(isSelected) this.mSelectedItems.add(i);
		    }
	    } else {
		    for(int i = 0; i < this.itemTitles.size(); i++){
		    	boolean isSelected = selected[i];
		    	selectedArray[i] = isSelected;
		    	if(isSelected) this.selectedIndex = i;
		    }
	    }
    }
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        
        if(this.mListener == null)
        try {
            
            if (activity.getClass().equals(MainActivity.class)){
            	mListener = (ChecklistDialogListener) (((MainActivity)activity).getDisplayedFragment());
            } else {
            	mListener = (ChecklistDialogListener) activity;
            }
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement ChecklistDialogListener");
        }
    }
    
    public ArrayList<String> getSelectedItemTitles(){
    	ArrayList<String> list = new ArrayList<String>();
        if (this.mSelectedItems == null){
            return  list;
        }
    	for(Integer i : this.mSelectedItems){
    		list.add(this.itemTitles.get(i.intValue()));
    	}
        if (this.mSelectedItems.size() == 0 && !this.isMultipleChoice){
            list.add(this.itemTitles.get(this.selectedIndex));
       }
    	return list;
    }


	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
	    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
	    
	    CharSequence[] titles = this.itemTitles.toArray(new CharSequence[this.itemTitles.size()]);
	    
	    boolean[] selectedArray = new boolean[this.itemTitles.size()];
	    for(Integer selection : this.mSelectedItems){
	    	selectedArray[selection.intValue()] = true;
	    }

	    if(this.isMultipleChoice){
	        builder.setTitle(title).setMultiChoiceItems(titles, selectedArray,
                    new DialogInterface.OnMultiChoiceClickListener() {
             public void onClick(DialogInterface dialog, int which,
                     boolean isChecked) {
          		   if (isChecked) {
                     	mSelectedItems.add(which);
                 	} else if (mSelectedItems.contains(which)) {
                     	mSelectedItems.remove(Integer.valueOf(which));
                 	}
             }
         }).setPositiveButton(R.string.button_title_apply, new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int id) {
                 mListener.onDialogPositiveClick(ChecklistDialog.this);
             }
         })
         .setNegativeButton(R.string.button_title_cancel, new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int id) {
                 mListener.onDialogNegativeClick(ChecklistDialog.this);
             }
         });
	        
	    } else {
	    	
	    	 builder.setTitle(title).setSingleChoiceItems(titles, this.selectedIndex, 
	    			 new DialogInterface.OnClickListener() {
	             public void onClick(DialogInterface dialog, int id) {
	                 mSelectedItems.clear();
	            	 mSelectedItems.add(id);
	             }
	         })
	         .setPositiveButton(R.string.button_title_apply, new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int id) {
                 mListener.onDialogPositiveClick(ChecklistDialog.this);
             }
	         })
	         .setNegativeButton(R.string.button_title_cancel, new DialogInterface.OnClickListener() {
	             public void onClick(DialogInterface dialog, int id) {
	                 mListener.onDialogNegativeClick(ChecklistDialog.this);
	             }
	         });
	    }
	    
	    return builder.create();
	}
}


