package org.androidtown.auto_tracking_cctvapp;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;

import org.androidtown.auto_tracking_cctvapp.retrofit.RetrofitService;
import org.androidtown.auto_tracking_cctvapp.retrofit.direction_message;

import java.util.List;

import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by soeun on 2017-07-17.
 */

public class send_command_to_web_server extends Activity {

    ImageButton up_btn, left_btn, right_btn, down_btn;

    direction_message DirectionMessage; //retrofit

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selected_camera_play);

        //ImageButton Setting
        up_btn = (ImageButton) findViewById(R.id.up_button);
        left_btn = (ImageButton) findViewById(R.id.left_button);
        right_btn = (ImageButton) findViewById(R.id.right_button);
        down_btn = (ImageButton) findViewById(R.id.down_button);

        //ImageButton Event
        up_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send_command("up");  //서버에 "up" 보내기
            }
        });

        left_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send_command("left");  //서버에 "left" 보내기
            }
        });

        right_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send_command("right");  //서버에 "right" 보내기
            }
        });

        down_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send_command("down");  //서버에 "down" 보내기
            }
        });
    }

    public void send_command(String direction) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://localhost:3000/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service = retrofit.create(RetrofitService.class);

        Call<List<direction_message>> call = service.get_direction_message(direction);
        /*
        call.enqueue(new Callback<org.androidtown.auto_tracking_cctvapp.retrofit.direction_message>() {
            @Override
            public void onResponse(Call<direction_message> call, Response<direction_message> response) {
                if(response.isSuccessful()) {
                    DirectionMessage = response.body();
                }
            }

            @Override
            public void onFailure(Call<direction_message> call, Throwable t) {
                Log.e("Error", t.getMessage());
            }
        });
        */
        Log.i("Sended Message", direction);
    }
}

@@ -375,16 +375,70 @@
 2.Call;%0A
+import retrofit2.Callback;%0Aimport retrofit2.Response;%0A
 import r
@@ -2199,18 +2199,21 @@
 p://
-localhost:
+192.168.1.3:1
 3000
@@ -2476,19 +2476,8 @@
 n);%0A
-        /*%0A
     
@@ -2510,55 +2510,13 @@
 ack%3C
-org.androidtown.auto_tracking_cctvapp.retrofit.
+List%3C
 dire
@@ -2529,16 +2529,17 @@
 message%3E
+%3E
 () %7B%0A   
@@ -2593,24 +2593,29 @@
 sponse(Call%3C
+List%3C
 direction_me
@@ -2616,24 +2616,25 @@
 ion_message%3E
+%3E
  call, Respo
@@ -2637,16 +2637,21 @@
 esponse%3C
+List%3C
 directio
@@ -2660,16 +2660,17 @@
 message%3E
+%3E
  respons
@@ -2678,127 +2678,8 @@
 ) %7B%0A
-                if(response.isSuccessful()) %7B%0A                    DirectionMessage = response.body();%0A                %7D
 %0A   
@@ -2751,16 +2751,21 @@
 re(Call%3C
+List%3C
 directio
@@ -2774,16 +2774,17 @@
 message%3E
+%3E
  call, T
@@ -2793,32 +2793,33 @@
 owable t) %7B%0A
+%0A
         
         Log.
@@ -2798,32 +2798,53 @@
 e t) %7B%0A%0A        
+    %7D%0A        %7D);%0A%0A//
         Log.e(%22E
@@ -2843,44 +2843,75 @@
 Log.
-e(%22Error%22, t.getMessage
+d(%22SEND%22, %22send_command() returned: %22 + call.request
 ());%0A
+//
         
     
@@ -2910,36 +2910,153 @@
     
-    %7D%0A        %7D);%0A        */
+Log.d(%22EXCUTED%22, %22send_command() returned: %22 + call.isExecuted());%0A//        Log.d(%22CANCELED%22, %22send_command() returned: %22 + call.isCanceled());%0A
 %0A   

send command to server