package com.asus.robotdevsample;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;

import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.asus.robotframework.API.RobotAPI;

import java.util.ArrayList;
import java.util.Locale;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class QaActivity extends AppCompatActivity {

    private TextView txvResult;
    private Context context;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qa);
        context = getApplicationContext();
        txvResult = (TextView) findViewById(R.id.txvResult);

    }

    public void getSpeechInput(View view) {

        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());


        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(intent, 10);
        } else {
            Toast.makeText(this, "Your Device Don't Support Speech Input", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        RobotAPI API= new RobotAPI(context);
        HttpAsyncTask task = new HttpAsyncTask();
        switch (requestCode) {
            case 10:
                if (resultCode == RESULT_OK && data != null) {
                    final ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    txvResult.setText(result.get(0));
                    //API.robot.speak(result.get(0));
                    new AsyncTask<Void,Void,String>(){
                        @Override
                        protected String doInBackground(Void... params) {
                            RobotAPI API= new RobotAPI(context);

                            OkHttpClient client = new OkHttpClient();
                            Request request = new Request.Builder().url("http://220.134.39.80:5000/"+ result.get(0)).build();

                            try {
                                Response response = client.newCall(request).execute();
                                API.robot.speak(response.body().string());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                            return null;
                        }
                    }.execute();

                }
                break;
        }
    }




}

@@ -257,24 +257,54 @@
 .view.View;%0A
+import android.widget.Button;%0A
 import andro
@@ -644,16 +644,23 @@
 xvResult
+,answer
 ;%0A    pr
@@ -682,16 +682,43 @@
 ontext;%0A
+    private Button result;%0A
 %0A%0A%0A    @
@@ -977,24 +977,128 @@
 esult);%0A
+        answer= (TextView)findViewById(R.id.answer);%0A        result = (Button)findViewById(R.id.button);
 %0A    %7D%0A%0A
     publ
@@ -1085,24 +1085,26 @@
 on);%0A    %7D%0A%0A
+%0A%0A
     public v
@@ -1517,16 +1517,17 @@
 t, 10);%0A
+%0A
         
@@ -2179,16 +2179,31 @@
 setText(
+%22%E8%BE%A8%E8%AD%98%E7%B5%90%E6%9E%9C:      %22+ 
 result.g
@@ -2256,32 +2256,33 @@
 result.get(0));%0A
+%0A
                 
@@ -2492,16 +2492,61 @@
 ontext);
+%0A                            String sss = %22%22;
 %0A%0A      
@@ -2802,16 +2802,22 @@
         
+final 
 Response
@@ -2895,32 +2895,22 @@
         
-API.robot.speak(
+sss = 
 response
@@ -2925,19 +2925,73 @@
 string()
+;%0A                                API.robot.speak(sss
 );%0A
+%0A
         
@@ -3159,13 +3159,264 @@
 urn 
-null;
+sss;%0A                        %7D%0A                        @Override%0A                        protected void onPostExecute(final String sss) %7B%0A                            super.onPostExecute(sss);%0A%0A%0A                                answer.setText(%22%E5%9B%9E%E7%AD%94: %5Cn%22+sss);%0A
 %0A   
@@ -3430,32 +3430,33 @@
               %7D%0A
+%0A
                 
@@ -3469,24 +3469,27 @@
 execute();%0A%0A
+%0A%0A%0A
             
@@ -3517,16 +3517,17 @@
  break;%0A
+%0A
         
@@ -3535,13 +3535,98 @@
 %0A    %7D%0A%0A
-%0A
+///551HIHIHIHIHIHIHIHIHIHIHIHIHIHIHI%0A//5656+++++++++++++++++++++++++++++++++++++++++++
 %0A%0A%7D%0A

my name is BO