package de.apps4ics.mountainnavigation.pois;

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

import de.apps4ics.mountainnavigation.adapters.CheckboxImageAdapter;
import de.apps4ics.mountainnavigation.Features;
import de.apps4ics.mountainnavigation.MainActivity;
import de.apps4ics.mountainnavigation.R;
import de.apps4ics.mountainnavigation.handlers.PoiHandler;

/**
 * Created by Vinz on 03.11.2015.
 */
public class AddBreakpointDialog extends DialogFragment {
    PoiHandler poiHandler;

    public AddBreakpointDialog(PoiHandler poiHandler){
        this.poiHandler = poiHandler;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final CheckboxImageAdapter adapter = new CheckboxImageAdapter(getActivity(), PoiHandler.getBreakPointOptions(), null, PoiHandler.getBreakPointOptionsImgs());
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.break_point_dialog_title)
                //.setIcon(res.getDrawable(R.drawable.break_point))
                .setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int[] options = adapter.getCheckedItems();
                        int stone = 0;
                        int bench = 0;
                        int table = 0;
                        int roofed = 0;
                        for(int i=0; i<options.length; ++i){
                            switch(options[i]){
                                case 0:
                                    options[i] = Features.BREAK_STONE;
                                    stone = 1;
                                    break;
                                case 1:
                                    options[i] = Features.BREAK_BENCH;
                                    bench = 1;
                                    break;
                                case 2:
                                    options[i] = Features.BREAK_TABLE;
                                    table = 1;
                                    break;
                                case 3:
                                    options[i] = Features.BREAK_ROOFED;
                                    roofed = 1;
                                    break;
                            }
                        }
                        poiHandler.addPoi(new BreakPoint(0, stone, bench, table, roofed, null));
                    }
                })
                .setNegativeButton(R.string.cancel_button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .setAdapter(adapter, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
        return builder.create();
    }
}
