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 android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;

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.
 */
//TODO beautify
public class AddHutDialog extends DialogFragment {
    PoiHandler poiHandler;

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

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.hut_dialog, null, true);
        final EditText input = (EditText) view.findViewById(R.id.hutName);
        final Spinner types = (Spinner) view.findViewById(R.id.hutTypes);
        final CheckBox cbWinterroom = (CheckBox) view.findViewById(R.id.hutWinterroom);
        builder.setTitle(R.string.hut_dialog_title)
                .setMessage(R.string.hut_dialog_msg)
                .setView(view)
                .setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String name = input.getText().toString().trim();
                        if(name != null && !name.equals("")) {
                            int type = types.getSelectedItemPosition();
                            int winterroom = cbWinterroom.isChecked() ? 1 : 0;
                            switch (type){
                                case 0:
                                    type = Features.HUT_REFUGE;
                                    break;
                                case 1:
                                    type = Features.HUT_SUPPORT;
                                    break;
                                case 2:
                                    type = Features.HUT_CATERING;
                                    break;
                            }
                            poiHandler.addPoi(new Hut(0, type, winterroom, name, null));
                        }
                    }
                })
                .setNegativeButton(R.string.cancel_button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
        return builder.create();
    }
}
