/*
 * Copyright (c) 2010-2016, openHAB.org and others.
 *
 *   All rights reserved. This program and the accompanying materials
 *   are made available under the terms of the Eclipse Public License v1.0
 *   which accompanies this distribution, and is available at
 *   http://www.eclipse.org/legal/epl-v10.html
 */

package org.openhab.habdroid.core;

import android.app.IntentService;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

import org.openhab.habdroid.R;
import org.openhab.habdroid.core.connection.Connection;
import org.openhab.habdroid.core.connection.ConnectionFactory;
import org.openhab.habdroid.core.connection.exception.ConnectionException;
import org.openhab.habdroid.util.SyncHttpClient;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;

/**
 * This service handles voice commands and sends them to OpenHAB.
 * It will use the openHAB base URL if passed in the intent's extra.
 */
public class OpenHABVoiceService extends IntentService {
    private static final String TAG = OpenHABVoiceService.class.getSimpleName();

    public OpenHABVoiceService() {
        super("OpenHABVoiceService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        final String voiceCommand = extractVoiceCommand(intent);
        if (voiceCommand.isEmpty()) {
            return;
        }

        ConnectionFactory.waitForInitialization();
        Connection connection = null;

        try {
            connection = ConnectionFactory.getUsableConnection();
        } catch (ConnectionException e) {
            Log.w(TAG, "Couldn't determine OpenHAB URL", e);
        }

        if (connection != null) {
            sendVoiceCommand(connection.getSyncHttpClient(), voiceCommand);
        } else {
            Toast.makeText(this,
                    R.string.error_couldnt_determine_openhab_url, Toast.LENGTH_SHORT)
                    .show();
        }
    }

    private String extractVoiceCommand(Intent data) {
        String voiceCommand = "";
        List<String> textMatchList = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        if (!textMatchList.isEmpty()) {
            voiceCommand = textMatchList.get(0);
        }
        Log.i(TAG, "Recognized text: " + voiceCommand);
        final String message = getString(R.string.info_voice_recognized_text, voiceCommand);
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
        return voiceCommand;
    }

    private void sendVoiceCommand(final SyncHttpClient client, final String command) {
        final HashMap<String, String> headers = new HashMap<>();
        headers.put("Accept-Language", Locale.getDefault().getLanguage());

        SyncHttpClient.HttpStatusResult result = client.post("rest/voice/interpreters",
                command, "text/plain", headers).asStatus();
        if (result.statusCode == 404) {
            Log.d(TAG, "Voice interpreter endpoint returned 404, falling back to item");
            result = client.post("rest/items/VoiceCommand", command, "text/plain").asStatus();
        }
        if (result.isSuccessful()) {
            Log.d(TAG, "Voice command was sent successfully");
        } else {
            Log.e(TAG, "Sending voice command failed", result.error);
        }
    }
}

@@ -403,32 +403,85 @@
 content.Intent;%0A
+import android.os.Handler;%0Aimport android.os.Looper;%0A
 import android.s
@@ -496,32 +496,32 @@
 cognizerIntent;%0A
-
 import android.s
@@ -1233,16 +1233,84 @@
 eName();
+%0A    private Handler mHandler = new Handler(Looper.getMainLooper());
 %0A%0A    pu
@@ -2019,49 +2019,28 @@
     
+show
 Toast
-.makeText(this,%0A                    
+(getString(
 R.st
@@ -2083,56 +2083,9 @@
 _url
-, Toast.LENGTH_SHORT)%0A                    .show(
+)
 );%0A 
@@ -2454,31 +2454,18 @@
     
-final String message = 
+showToast(
 getS
@@ -2524,72 +2524,8 @@
 and)
-;%0A        Toast.makeText(this, message, Toast.LENGTH_LONG).show(
 );%0A 
@@ -3356,16 +3356,16 @@
 error);%0A
-
         
@@ -3371,11 +3371,150 @@
  %7D%0A    %7D
+%0A%0A    private void showToast(CharSequence text) %7B%0A        mHandler.post(() -%3E Toast.makeText(this, text, Toast.LENGTH_SHORT).show());%0A    %7D
 %0A%7D%0A

Fix voice service toast generation. (#872)