package com.rthoni.camotion.ui.dialogs;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

import com.luticate.utils.business.LuPromise;
import com.rthoni.camotion.R;
import com.rthoni.camotion.business.CamotionBusiness;
import com.rthoni.camotion.dbo.LocationDbo;

import java.util.List;

/**
 * Created by robin on 12/10/15.
 */
public abstract class LocationPickerDialog {
    public static LuPromise<LocationDbo> getLocation(Context context)
    {
        final LuPromise<LocationDbo> promise = new LuPromise<>();

        final List<LocationDbo> locations = CamotionBusiness.getLocations(context);

        String[] locationStrings = new String[locations.size()];
        for (int i = 0; i < locations.size(); ++i) {
            LocationDbo location = locations.get(i);
            locationStrings[i] = location.getPseudoMail();
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(R.string.locations)
                .setItems(locationStrings, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        promise.resolve(locations.get(which));
                    }
                })
                .setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        promise.reject(null);
                    }
                })
        .show();
        return promise;
    }
}
