package de.fau.cs.mad.kwikshop.android.view;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

import com.fasterxml.jackson.databind.ObjectMapper;


import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import dagger.ObjectGraph;
import de.fau.cs.mad.kwikshop.android.R;
import de.fau.cs.mad.kwikshop.android.di.KwikShopModule;
import de.fau.cs.mad.kwikshop.android.model.RestClientFactory;
import de.fau.cs.mad.kwikshop.android.restclient.ShoppingListResource;
import de.fau.cs.mad.kwikshop.common.ShoppingListServer;
import de.greenrobot.event.EventBus;

public class ServerIntegrationDebugActivity extends BaseActivity {


    public static Intent getIntent(Context context) {

        return new Intent(context, ServerIntegrationDebugActivity.class);
    }


    private final EventBus privateBus = EventBus.builder().build();
    private final ObjectMapper mapper = new ObjectMapper();

    @InjectView(R.id.textView)
    TextView textView_Result;

    @Inject
    RestClientFactory clientFactory;



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


        ButterKnife.inject(this);

        ObjectGraph objectGraph = ObjectGraph.create(new KwikShopModule(this));
        objectGraph.inject(this);

        privateBus.register(this);

    }



    @OnClick(R.id.button_getShoppingLists)
    void getShoppingLists() {

        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                try {

                    privateBus.post("Getting shopping lists...");

                    ShoppingListResource client = clientFactory.getShoppingListClient();

                    List<ShoppingListServer> shoppingLists = client.getListSynchronously();

                    String serialized = mapper.writeValueAsString(shoppingLists);
                    privateBus.post(serialized);

                } catch (Exception e) {

                    privateBus.post(getStackTrace(e));
                }

                return null;
            }
        }.execute();
    }

    @OnClick(R.id.button_createShoppingist)
    void createShoppingList()
    {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... voids) {

                try{

                    privateBus.post("Creating sample shopping list on server...");

                    ShoppingListServer newList = new ShoppingListServer();
                    newList.setName("New List");
                    newList.setLastModifiedDate(new Date());


                    ShoppingListResource client = clientFactory.getShoppingListClient();
                    newList = client.createListSynchronously(newList);

                    privateBus.post(mapper.writeValueAsString(newList));

                } catch (Exception e) {
                    privateBus.post(getStackTrace(e));
                }

                return null;
            }
        }.execute();
    }



    @Override
    public void onStop() {
        super.onStop();
        privateBus.unregister(this);
    }


    public void onEventMainThread(String value) {
        textView_Result.setText(value);
    }


    public static String getStackTrace(final Throwable throwable) {
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw, true);
        throwable.printStackTrace(pw);
        return sw.getBuffer().toString();
    }


//    private String toString(ShoppingListServer shoppingList) {
//
//        String format = "   %s = %s\n";
//        String result = "";
//
//        result += "ShoppingList: \n";
//        result += String.format(format, "id", shoppingList.getId());
//        result += String.format(format, "name", shoppingList.getName());
//        result += String.format(format, "sortType", shoppingList.getSortTypeInt());
//        result += String.format(format, "location", toString(shoppingList.getLocation()));
//        result += String.format(format, "lastModifiedDate", shoppingList.getLastModifiedDate());
//        result += "   Items: \n";
//
//        for(Item item : shoppingList.getItems()) {
//
//            result += toString(item);
//            result += "\n";
//        }
//
//
//        return result;
//    }
//
//    private String toString(LastLocation location) {
//
//        if(location == null) {
//            return "";
//        }
//
//        return  String.format("latitide = %s, longitude = %s, address = %s, name = %s, timestamp = %s",
//                location.getLatitude(),
//                location.getLongitude(),
//                location.getAddress(),
//                location.getName(),
//                location.getTimestamp());
//    }
//



}

@@ -242,16 +242,76 @@
 Mapper;%0A
+import com.fasterxml.jackson.databind.SerializationFeature;%0A
 %0A%0Aimport
@@ -1197,29 +1197,8 @@
 pper
- = new ObjectMapper()
 ;%0A%0A 
@@ -1309,16 +1309,167 @@
 tory;%0A%0A%0A
+    public ServerIntegrationDebugActivity() %7B%0A%0A        mapper = new ObjectMapper();%0A        mapper.enable(SerializationFeature.INDENT_OUTPUT);%0A    %7D%0A%0A%0A
 %0A    @Ov
@@ -1831,32 +1831,140 @@
 (this);%0A%0A    %7D%0A%0A
+    @Override%0A    public void onStop() %7B%0A        super.onStop();%0A        privateBus.unregister(this);%0A    %7D%0A
 %0A%0A    @OnClick(R
@@ -3665,119 +3665,8 @@
 %7D%0A%0A%0A
-%0A    @Override%0A    public void onStop() %7B%0A        super.onStop();%0A        privateBus.unregister(this);%0A    %7D%0A%0A%0A
     
@@ -3750,33 +3750,32 @@
 (value);%0A    %7D%0A%0A
-%0A
     public stati
@@ -4017,24 +4017,24 @@
 ring();%0A
+
     %7D%0A%0A%0A
 //    pr
@@ -4029,1291 +4029,6 @@
 %7D%0A%0A%0A
-//    private String toString(ShoppingListServer shoppingList) %7B%0A//%0A//        String format = %22   %25s = %25s%5Cn%22;%0A//        String result = %22%22;%0A//%0A//        result += %22ShoppingList: %5Cn%22;%0A//        result += String.format(format, %22id%22, shoppingList.getId());%0A//        result += String.format(format, %22name%22, shoppingList.getName());%0A//        result += String.format(format, %22sortType%22, shoppingList.getSortTypeInt());%0A//        result += String.format(format, %22location%22, toString(shoppingList.getLocation()));%0A//        result += String.format(format, %22lastModifiedDate%22, shoppingList.getLastModifiedDate());%0A//        result += %22   Items: %5Cn%22;%0A//%0A//        for(Item item : shoppingList.getItems()) %7B%0A//%0A//            result += toString(item);%0A//            result += %22%5Cn%22;%0A//        %7D%0A//%0A//%0A//        return result;%0A//    %7D%0A//%0A//    private String toString(LastLocation location) %7B%0A//%0A//        if(location == null) %7B%0A//            return %22%22;%0A//        %7D%0A//%0A//        return  String.format(%22latitide = %25s, longitude = %25s, address = %25s, name = %25s, timestamp = %25s%22,%0A//                location.getLatitude(),%0A//                location.getLongitude(),%0A//                location.getAddress(),%0A//                location.getName(),%0A//                location.getTimestamp());%0A//    %7D%0A//%0A%0A%0A%0A
 %7D%0A

Format JSON output in ServerIntegrationDebugActivity