package com.kpl.CourierMgmt.GoogleMapClient;

import java.net.URL;
import java.time.Duration;

public class DirectionsApi implements IGoogleMapApi {

	private String apiKey;

    public DirectionsApi(String key)
    {
        apiKey = key;
    }

 /* private String CreateRequestUrl(String origin, String destination)
    {
        String baseUrl = "https://maps.googleapis.com/maps/api/distancematrix/json?units=metric";
        origin = escapeDataString(origin);
        destination = escapeDataString(destination);
        return baseUrl + "&origins=" + origin + "&destinations=" + destination + "&key=" + apiKey;
    }

    private String escapeDataString(String data)
    {
    	String uriEncodeVariable = data;
    	URL tempURL = new URL(uriEncodeVariable);
    	String uriResult = tempURL.toURI().toString();
    } */
    
    @Override
    public IGoogleRoute GetRoute(String origin, String destination)
    {
        //var task = GetRouteAsync(origin, destination);
        //return task.Result;
    	
    	GoogleRoute g = new GoogleRoute();
    	g.Origin = origin;
    	g.Destination = destination;
    	g.Distance = 5.0;
    	g.Duration = Duration.ofMinutes(5);
    	g.Status = "OK";
    	return g;
    }
/*
    public async Task<IGoogleRoute> GetRouteAsync(String origin, String destination)
    {
        String url = CreateRequestUrl(origin, destination);
        using (var client = new HttpClient())
        {
            using (var response = await client.GetAsync(url))
            {
                using (var content = response.Content)
                {
                    string result = await content.ReadAsStringAsync();
                    JsonMapResponse jmr = JsonConvert.DeserializeObject<JsonMapResponse>(result);
                    if (jmr != null
                        && jmr.status != null
                        && jmr.status == "OK"
                        && jmr.origin_addresses != null
                        && jmr.origin_addresses.Length > 0
                        && jmr.destination_addresses != null
                        && jmr.destination_addresses.Length > 0
                        && jmr.rows != null
                        && jmr.rows.Length > 0
                        && jmr.rows[0].elements != null
                        && jmr.rows[0].elements.Length > 0
                        && jmr.rows[0].elements[0].status == "OK")
                        return new GoogleRoute
                        {
                            Status = jmr.status,
                            Origin = jmr.origin_addresses[0],
                            Destination = jmr.destination_addresses[0],
                            Distance = jmr.rows[0].elements[0].distance.value / 1000,
                            Duration = TimeSpan.FromSeconds(jmr.rows[0].elements[0].duration.value)
                        };
                    else
                    {
                        string errMsg = string.Empty;
                        if (jmr.status != "OK")
                        {
                            errMsg += jmr.status;
                            if (!string.IsNullOrEmpty(jmr.error_message))
                                errMsg += $" ({jmr.error_message})";
                        }
                        else if (jmr.rows != null
                            && jmr.rows.Length > 0
                            && jmr.rows[0].elements != null
                            && jmr.rows[0].elements.Length > 0)
                        {
                            if (jmr.rows[0].elements[0].status != "OK")
                                errMsg += $" Route error: {jmr.rows[0].elements[0].status}";
                        }
                        return new GoogleRoute
                        {
                            Status = errMsg
                        };
                    }
                }
            }
        }
    }
*/
}
