package de.apps4ics.mountainnavigation.overlays;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;

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.MapView;
import org.osmdroid.views.overlay.ItemizedIconOverlay;
import org.osmdroid.views.overlay.OverlayItem;

import java.util.ArrayList;
import java.util.List;

import de.apps4ics.mountainnavigation.MainActivity;
import de.apps4ics.mountainnavigation.R;

/**
 * Created by Vinz on 21.10.2015.
 */
public class MapDialogItemizedIconOverlay extends ItemizedIconOverlay<OverlayItem> {
    private Context context;
    private AlertDialog alertDialog;
    private List<Boolean> items;
    private int selected;
    private Drawable posIcon;
    private Drawable negIcon;
    private MapView mapView;

    private RoadManager roadManager;
    private ArrayList<GeoPoint> waypoints;
    private ArrayList<Integer> waypointIds;
    private Road road;
    private Polyline line;

    public MapDialogItemizedIconOverlay(final Context context, final List<OverlayItem> aList, AlertDialog alertDialog, MapView mapView) {
        super(context, aList, new OnItemGestureListener<OverlayItem>() {
            @Override
            public boolean onItemSingleTapUp(int index, OverlayItem item) {
                return false;
            }

            @Override
            public boolean onItemLongPress(int index, OverlayItem item) {
                return false;
            }
        });
        this.context = context;
        this.items = new ArrayList<>();
        this.waypointIds = new ArrayList<>();
        this.alertDialog = alertDialog;
        this.mapView = mapView;
        this.selected = 0;
        Resources res = context.getResources();
        posIcon = res.getDrawable(R.mipmap.ic_poi);
        negIcon = res.getDrawable(R.mipmap.ic_poi_red);
        for(int i=0; i<aList.size(); ++i){
            items.add(false);
            waypointIds.add(-1);
        }

        roadManager = new OSRMRoadManager(context);
        waypoints = new ArrayList<>();
        road = new Road();
    }

    @Override
    protected boolean onLongPressHelper(int index, OverlayItem item) {
        final int remIndex = index;
        AlertDialog.Builder removeBuilder = new AlertDialog.Builder(context);
        removeBuilder.setTitle(R.string.path_map_remove_marker_dialog_title)
                .setMessage(R.string.path_map_remove_marker_dialog_text)
                .setPositiveButton(R.string.ok_button_remove_marker, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        removeItem(remIndex);
                        mapView.invalidate();
                    }
                })
                .setNegativeButton(R.string.cancel_button_remove_marker, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
        AlertDialog dialog = removeBuilder.create();
        dialog.show();
        return true;
    }

    @Override
    public boolean addItem(OverlayItem item) {
        boolean ret = super.addItem(item);
        items.add(false);
        waypointIds.add(-1);
        return ret;
    }

    @Override
    public OverlayItem removeItem(int position) {
        OverlayItem oI = super.removeItem(position);
        if(items.get(position)) selected--;
        items.remove(position);
        int waypointIndex = waypointIds.get(position);
        waypoints.remove(waypointIndex);
        waypointIds.set(position, -1);
        MainActivity.pathMarkers.remove(waypointIndex);
        checkForSelection();
        return oI;
    }

    private void updateItem(int index){
        if(items.get(index)){
            items.set(index, false);
            selected--;
            getItem(index).setMarker(negIcon);
            int waypointIndex = waypointIds.get(index);
            waypoints.remove(waypointIndex);
            waypointIds.set(index, -1);
            MainActivity.pathMarkers.remove(waypointIndex);
        } else {
            items.set(index, true);
            selected++;
            getItem(index).setMarker(posIcon);
            waypoints.add((GeoPoint) getItem(index).getPoint());
            waypointIds.set(index, waypoints.size()-1);
            MainActivity.pathMarkers.add(getItem(index));
        }
        mapView.invalidate();
        checkForSelection();
    }

    private void checkForSelection(){
        //TODO disable network calls for now
        //mapView.getOverlays().remove(line);
        if(selected >= 2){
            //road = roadManager.getRoad(waypoints);
            //line = RoadManager.buildRoadOverlay(road, context);
            //mapView.getOverlays().add(line);
            //mapView.invalidate();
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
        } else {
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
        }
    }

    @Override
    protected boolean onSingleTapUpHelper(int index, OverlayItem item, MapView mapView) {
        updateItem(index);
        return true;
    }
}
