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.util.Log;

import org.osmdroid.bonuspack.overlays.Polyline;
import org.osmdroid.bonuspack.routing.OSRMRoadManager;
import org.osmdroid.bonuspack.routing.Road;
import org.osmdroid.bonuspack.routing.RoadManager;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.overlay.OverlayItem;

import java.util.ArrayList;

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 AddPathDialog extends DialogFragment {
    PoiHandler poiHandler;

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

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final CheckboxImageAdapter adapter = new CheckboxImageAdapter(getActivity(), PoiHandler.getPathOptions(), PoiHandler.getPathDescs(), PoiHandler.getPathOptionImgs());
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.path_dialog_title)
                .setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int[] options = adapter.getCheckedItems();
                        int exposed = 0;
                        int diffWet = 0;
                        int giddiness = 0;
                        int climbing = 0;
                        for (int i = 0; i < options.length; ++i) {
                            switch (options[i]) {
                                case 0:
                                    options[i] = Features.PATH_EXPOSED;
                                    exposed = 1;
                                    break;
                                case 1:
                                    options[i] = Features.PATH_DIFF_WET;
                                    diffWet = 1;
                                    break;
                                case 2:
                                    options[i] = Features.PATH_GIDDINESS;
                                    giddiness = 1;
                                    break;
                                case 3:
                                    options[i] = Features.PATH_CLIMBING;
                                    climbing = 1;
                                    break;
                            }
                        }
                        ArrayList<OverlayItem> pathMarkers = MainActivity.getPathMarkers();

                        if(pathMarkers.size() < 2){
                            Log.e(MainActivity.TAG, "One of the path markers in addPathDialog is null!");
                        }

                        RoadManager roadManager = new OSRMRoadManager(poiHandler.getContext());
                        ArrayList<GeoPoint> waypoints = new ArrayList<>();
                        DbGeoPoint current = null;
                        DbGeoPoint last = null;
                        for(int i=0; i<pathMarkers.size(); ++i){
                            GeoPoint gp = (GeoPoint) pathMarkers.get(i).getPoint();
                            waypoints.add(gp);
                            long time = 0;
                            long rowId = 0;
                            String uid = pathMarkers.get(i).getUid();
                            if(uid != null && !uid.equals("")){
                                String[] uids = pathMarkers.get(i).getUid().split("_"); //format is 123456789_4
                                String timeStr = uids[0];
                                String idStr = uids[1];
                                if(timeStr != null && !timeStr.equals("")) time = Long.parseLong(timeStr) / 1000;
                                if(idStr != null && !idStr.equals("")) rowId = Long.parseLong(uids[1]);
                            }

                            current = new DbGeoPoint(gp.getLatitude(), gp.getLongitude(), gp.getAltitude(), time);
                            if(pathMarkers.get(i).getTitle() == null){
                                rowId = poiHandler.addGp(current);
                            }
                            current.setId(rowId);
                            if(current != null && last != null){
                                long poiId = poiHandler.addPoi(new Path(0, 0, exposed, diffWet, giddiness, climbing, last, current));

                            }
                            last = current;
                        }
                        Road road = roadManager.getRoad(waypoints);
                        Polyline line = RoadManager.buildRoadOverlay(road, poiHandler.getContext());
                        MainActivity.getMapView().getOverlays().add(line);
                        MainActivity.getMapView().invalidate();
                        MainActivity.setPathMarkers(new ArrayList<OverlayItem>());
                    }
                })
                .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();
    }
}