package com.junjunguo.pocketmaps.util;

import android.util.Log;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.UiThread;
import com.graphhopper.util.details.PathDetail;
import com.junjunguo.pocketmaps.map.Navigator;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Starcommander@github.com
 */
public class SpeedUtil
{
//    ArrayList<SpeedArea> maxSpeedList = new ArrayList<SpeedArea>();
//    ArrayList<SpeedArea> aveSpeedList = new ArrayList<SpeedArea>();
    List<PathDetail> maxSpeedList;
    List<PathDetail> aveSpeedList;
    
    TextView view;
    boolean enabled = false;
    int pointsDone = 0;
    
    public SpeedUtil() {}
    
    public void initTextView(TextView view)
    {
        this.view = view;
        updateViewVis();
    }
    
    @UiThread
    public void updateTextView(int pointNum)
    {
        if (!enabled) { return; }
        if (view==null) { Log.w(SpeedUtil.class.getName(), "Missing view for speed!"); }
        else
        {
            view.setText(getSpeedValue(pointNum + pointsDone));
        }
    }
    
    public void setEnabled(boolean enabled)
    {
        this.enabled = enabled;
        updateViewVis();
        if (view != null)
        { view.setVisibility(View.GONE); }
    }
    
    private void updateViewVis()
    {
        if (view == null) { return; }
        if (enabled) { view.setVisibility(View.VISIBLE); }
        else { view.setVisibility(View.GONE); }
    }
    
    public void updateList(Map<String, List<PathDetail>> pathDetails)
    {
        maxSpeedList = pathDetails.get("max_speed");
        aveSpeedList = pathDetails.get("average_speed");
        pointsDone = 0;
//        for (java.util.Map.Entry e : pathDetails.entrySet())
//        { //TODO: Delete this debug info
//            System.out.println("PD: " + e.getKey() + "=" + e.getValue());
//        }
    }
    
    public void updateInstructionDone(int pLen)
    {
        pointsDone += pLen;
    }
    
    private String getSpeedValue(int pos)
    {
        if (maxSpeedList == null) { return "---"; }
        if (aveSpeedList == null) { return "---"; }
        for (PathDetail curDetail : maxSpeedList)
        {
            if (curDetail.getFirst() > pos) { continue; }
            if (curDetail.getLast() < pos) { continue; }
            if (curDetail.getValue()==null) { continue; }
//System.out.println("PD: " + pos + " MaxSpeed=" + curDetail.getValue());
            return "" + ((int)Double.parseDouble(curDetail.getValue().toString()));
        }
        for (PathDetail curDetail : aveSpeedList)
        {
            if (curDetail.getFirst() > pos) { continue; }
            if (curDetail.getLast() < pos) { continue; }
            if (curDetail.getValue()==null) { continue; }
//System.out.println("PD: " + pos + " AvgSpeed=" + curDetail.getValue());
            return "" + ((int)Double.parseDouble(curDetail.getValue().toString())) + "?";
        }
        return "---";
    }

}

@@ -398,148 +398,8 @@
 l%0A%7B%0A
-//    ArrayList%3CSpeedArea%3E maxSpeedList = new ArrayList%3CSpeedArea%3E();%0A//    ArrayList%3CSpeedArea%3E aveSpeedList = new ArrayList%3CSpeedArea%3E();%0A
     
@@ -1581,202 +1581,8 @@
  0;%0A
-//        for (java.util.Map.Entry e : pathDetails.entrySet())%0A//        %7B //TODO: Delete this debug info%0A//            System.out.println(%22PD: %22 + e.getKey() + %22=%22 + e.getValue());%0A//        %7D%0A
     
@@ -2070,82 +2070,8 @@
 ; %7D%0A
-//System.out.println(%22PD: %22 + pos + %22 MaxSpeed=%22 + curDetail.getValue());%0A
     
@@ -2082,38 +2082,48 @@
     return %22%22 + 
-((int)
+getUnitIntValue(
 Double.parseDoub
@@ -2407,59 +2407,67 @@
 ; %7D%0A
-//System.out.println(%22PD: %22 + pos + %22 AvgSpeed=%22 + 
+            return %22%22 + getUnitIntValue(Double.parseDouble(
 curD
@@ -2482,23 +2482,47 @@
 tValue()
-)
+.toString())) + %22?%22
 ;%0A    
+    %7D%0A
         
@@ -2533,77 +2533,258 @@
 rn %22
-%22 + ((int)Double.parseDouble(curDetail.getValue().toString())) + %22?%22;
+---%22;%0A    %7D%0A    %0A    private int getUnitIntValue(double v)%0A    %7B%0A        if (Variable.getVariable().isImperalUnit())%0A        %7B%0A            v = v / (UnitCalculator.METERS_OF_MILE * 0.001);%0A            v = 5*(Math.round(v/5)); // Round to multible of 5
 %0A   
@@ -2797,32 +2797,33 @@
 %0A        return 
-%22---%22
+(int)v
 ;%0A    %7D%0A%0A%7D%0A

Convert to miles rounded to mult of 5 as suggested in issue #144