package com.tsi.navi.util;

import android.annotation.SuppressLint;

import com.naver.maps.geometry.LatLng;
import com.tsi.navi.vo.NodeVo;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class TsiUtils {

    public static final int DATA_NONE = -1;
    public static final int LIGHT_STATUS_NONE = 7;

    public static final int SIGNAL_UNSPECIFIED = 0;
    public static final int SIGNAL_STRAIGHT = 1;
    public static final int SIGNAL_LEFT_TURN = 2;
    public static final int SIGNAL_WALKER = 3;
    public static final int SIGNAL_BICYCLE = 4;
    public static final int SIGNAL_RIGHT_TURN = 5;
    public static final int SIGNAL_BUS = 6;
    public static final int SIGNAL_U_TURN = 7;
    public static final int MAX_LIGHT_TYPE = SIGNAL_U_TURN+1;

    public static final int SIGNAL_MIN = SIGNAL_UNSPECIFIED;
    public static final int SIGNAL_MAX = SIGNAL_U_TURN;

    public static int getDistanceFromCar(LatLng from, NodeVo to) {
        if (to == null) return 0;
        //Location.distanceTo(from, to)
        return (int)LocationUtils.geoDistance(from.latitude, from.longitude, to.getLatLng().latitude, to.getLatLng().longitude);
    }
    @SuppressLint("DefaultLocale")
    public static String elapsedTimeStr(long elapsed) {
        long seconds = TimeUnit.SECONDS.convert(elapsed, TimeUnit.NANOSECONDS);
        long miliSeconds = TimeUnit.MILLISECONDS.convert(elapsed, TimeUnit.NANOSECONDS) % 1000L;
        long microSeconds = TimeUnit.MICROSECONDS.convert(elapsed, TimeUnit.NANOSECONDS) % 1000L;
        long nanoSeconds = TimeUnit.NANOSECONDS.convert(elapsed, TimeUnit.NANOSECONDS) % 1000L;
        if (seconds > 0L) {
            return String.format("%3d sec. %3d ms. %3d us. %3d ns.", seconds, miliSeconds, microSeconds, nanoSeconds);
        } else if (miliSeconds > 0L) {
            return String.format("--- sec. %3d ms. %3d us. %3d ns.", miliSeconds, microSeconds, nanoSeconds);
        } else {
            return microSeconds > 0L ? String.format("--- sec. --- ms. %3d us. %3d ns.", microSeconds, nanoSeconds)
                                     : String.format("--- sec. --- ms. --- us. %3d ns.", nanoSeconds);
        }
    }

    public static int getDirectionCode(double bearing) {

        if (bearing >= 22.5 && bearing < 67.5) return 70;
        if (bearing >= 67.5 && bearing < 112.5) return 40;
        if (bearing >= 112.5 && bearing < 157.5) return 80;
        if (bearing >= 157.5 && bearing < 202.5) return 10;
        if (bearing >= 202.5 && bearing < 247.5) return 50;
        if (bearing >= 247.5 && bearing < 292.5) return 20;
        if (bearing >= 292.5 && bearing < 337.5) return 60;
        return 30;
    }

    public static int getRunDirectionCode(double bearing) {

        if (bearing >= 22.5 && bearing < 67.5) return 50;
        if (bearing >= 67.5 && bearing < 112.5) return 20;
        if (bearing >= 112.5 && bearing < 157.5) return 60;
        if (bearing >= 157.5 && bearing < 202.5) return 30;
        if (bearing >= 202.5 && bearing < 247.5) return 70;
        if (bearing >= 247.5 && bearing < 292.5) return 40;
        if (bearing >= 292.5 && bearing < 337.5) return 80;
        return 10;
    }

    public static String calcDistance(double lat1, double lon1, double lat2, double lon2){
        double EARTH_R, Rad, radLat1, radLat2, radDist;
        double distance, ret;
        EARTH_R = 6371000.0;
        Rad = Math.PI/180;
        radLat1 = Rad * lat1;
        radLat2 = Rad * lat2;
        radDist = Rad * (lon1 - lon2);

        distance = Math.sin(radLat1) * Math.sin(radLat2);
        distance = distance + Math.cos(radLat1) * Math.cos(radLat2) * Math.cos(radDist);
        ret = EARTH_R * Math.acos(distance);
        double rslt = Math.round(Math.round(ret) / 1000);
        String result = rslt + " km";
        if(rslt == 0) result = Math.round(ret) +" m";

        return result;
    }

    public static List<String> split(String value, String separator) {
        String[] splits = value.split(separator);
        List<String> results = new ArrayList();

        for(int ii = 0; ii < splits.length; ++ii) {
            splits[ii] = splits[ii].trim();
            if (splits[ii].length() > 0) {
                results.add(splits[ii]);
            }
        }

        return results;
    }

    /*
            double speed = 0;
            if (this.lastLocation != null)
                speed = Math.sqrt(
                        Math.pow(location.getLongitude() - this.lastLocation.getLongitude(), 2)
                                + Math.pow(location.getLatitude() - this.lastLocation.getLatitude(), 2)
                ) / (location.getTime() - this.lastLocation.getTime());
            if (location.hasSpeed()) {
                speed = location.getSpeed();
            }
            Log.e(TAG, "S: " + (int)(speed*3.6F) + ", " + (int)(location.getSpeed()*3.6F));

*/

}
