package com.sms4blood.emergencyhealthservices.util;

import android.content.Intent;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import com.sms4blood.emergencyhealthservices.Sms;
import com.sms4blood.emergencyhealthservices.app.EhsApplication;

import java.text.DecimalFormat;

/**
 * Created by Vishnu on 8/1/2015.
 */
public class AppUtil extends Util {


    public static boolean callNumber(String phoneNumber) {
        boolean isValidPhoneNumber = checkPhoneNumber(phoneNumber);
        if (isValidPhoneNumber) {
            String callUri = "tel:" + phoneNumber;
            Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(callUri));
            callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(callIntent);
        } else {
            Toast.makeText(EhsApplication.getContext(), "Invalid phone number.", Toast.LENGTH_SHORT).show();
        }
        return isValidPhoneNumber;
    }

    public static boolean sendSms(String phoneNumber, String message) {
        boolean isValidPhoneNumber = checkPhoneNumber(phoneNumber);
        if (isValidPhoneNumber) {
//            /*It is better to let the android manage the sms sending task using native messaging app*/
//            Intent smsIntent = new Intent(Intent.ACTION_VIEW);
//            // Invokes only SMS/MMS clients
//            smsIntent.setType("vnd.android-dir/mms-sms");
//            // Specify the Phone Number
//            smsIntent.putExtra("address", phoneNumber);
//            // Specify the Message
//            smsIntent.putExtra("sms_body", message);
//            smsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            try {
//                startActivity(smsIntent);
//            } catch (Exception e) {
//                e.printStackTrace();
//                Toast.makeText(EhsApplication.getContext(), "Unable to send sms to this number.", Toast.LENGTH_SHORT)
//                        .show();
//            }
            Intent i=new Intent(EhsApplication.getContext(), Sms.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            Bundle bundle = new Bundle();
            bundle.putString("mobileno",phoneNumber);
            i.putExtras(bundle);
            startActivity(i);
        } else {
            Toast.makeText(EhsApplication.getContext(), "Invalid phone number.", Toast.LENGTH_SHORT).show();
        }
        return isValidPhoneNumber;
    }

    public static boolean showMap(String latitude, String longitude) {
        try {
            if (!TextUtils.isEmpty(latitude) && (!TextUtils.isEmpty(longitude))) {
                Uri gmmIntentUri = Uri.parse("google.navigation:q=" + latitude + "," + longitude);
                Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
                mapIntent.setPackage("com.google.android.apps.maps");
                mapIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(mapIntent);
                return true;
            } else {
                Toast.makeText(EhsApplication.getContext(), "Address not available.", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean showMap(String address) {
        try {
            if (!TextUtils.isEmpty(address)) {
                String encodedAddress = address.replaceAll("\n", " ");
                Uri gmmIntentUri = Uri.parse("google.navigation:q=" + encodedAddress);
                Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
                mapIntent.setPackage("com.google.android.apps.maps");
                mapIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(mapIntent);
                return true;
            } else {
                Toast.makeText(EhsApplication.getContext(), "Address not available.", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * @param fromLatitude
     * @param fromLongitude
     * @param toLatitude
     * @param toLongitude
     * @return distance between the two latitudes and longitudes in kms.
     */
    public static double calculateDistance(double fromLatitude,double fromLongitude,double toLatitude,double toLongitude) {
        float results[] = new float[1];

        try {
            Location.distanceBetween(fromLatitude, fromLongitude, toLatitude, toLongitude, results);
        } catch (Exception e) {
            if (e != null)
                e.printStackTrace();
        }
        int dist = (int) results[0];
        if(dist<=0) {
            return 0D;
        }

        DecimalFormat decimalFormat = new DecimalFormat("#.##");
        results[0]/=1000D;
        String distance = decimalFormat.format(results[0]);
        double d = Double.parseDouble(distance);
        return d;
    }
}
