package com.crowdshelf.app.io.network;

import android.util.Log;

import com.crowdshelf.app.io.DBEventType;
import com.crowdshelf.app.io.network.responseHandlers.BookHandler;
import com.crowdshelf.app.io.network.responseHandlers.BookListHandler;
import com.crowdshelf.app.io.network.responseHandlers.CrowdHandler;
import com.crowdshelf.app.io.network.responseHandlers.CrowdListHandler;
import com.crowdshelf.app.io.network.responseHandlers.UserHandler;
import com.crowdshelf.app.io.network.serializers.BookSerializer;
import com.crowdshelf.app.io.network.serializers.CrowdSerializer;
import com.crowdshelf.app.io.network.serializers.UserSerializer;
import com.crowdshelf.app.models.Book;
import com.crowdshelf.app.models.Crowd;
import com.crowdshelf.app.models.User;
import com.crowdshelf.app.ui.activities.MainTabbedActivity;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;

/**
 * Created by Torstein on 01.09.2015.
 */
public class NetworkController {
    private static BookListHandler bookListHandler = new BookListHandler();
    private static CrowdListHandler crowdListHandler = new CrowdListHandler();
    private static CrowdHandler crowdHandler = new CrowdHandler();
    private static BookHandler bookHandler = new BookHandler();
    private static UserHandler userHandler = new UserHandler();

    private static Type bookType = new TypeToken<Book>(){}.getType();

    private static Gson gson = new GsonBuilder()
            .registerTypeAdapter(Book.class, new BookSerializer())
            .registerTypeAdapter(Crowd.class, new CrowdSerializer())
            .registerTypeAdapter(User.class, new UserSerializer())
            .setPrettyPrinting()
            .create();


    /*
    Books
     */

    // Add book to database or update existing one
    public static void createBook(Book book, DBEventType dbEventType) {
        Log.i(MainTabbedActivity.TAG, "NetworkController - createBook");
        String jsonString = gson.toJson(book, bookType);

        NetworkHelper.sendRequest(HTTPRequestMethod.POST,
                "/books", jsonString,
                bookHandler, dbEventType);
    }

    public static void getBook(String id, DBEventType dbEventType) {
        NetworkHelper.sendRequest(HTTPRequestMethod.GET,
                "/books/" + id, null,
                bookHandler, dbEventType);
    }

    public static void getBooks(DBEventType dbEventType) {
        NetworkHelper.sendRequest(HTTPRequestMethod.GET,
                "/books", null,
                bookListHandler, dbEventType);
    }

    public static void getBooksByIsbn(String isbn, DBEventType dbEventType) {
        NetworkHelper.sendRequest(HTTPRequestMethod.GET,
                "/books?isbn=" + isbn, null,
                bookListHandler, dbEventType);
    }

    public static void getBooksByIsbnOwner(String isbn, String userId, DBEventType dbEventType) {
        NetworkHelper.sendRequest(HTTPRequestMethod.GET,
                "/books?isbn=" + isbn + "&?owner=" + userId, null,
                bookListHandler, dbEventType);
    }

    public static void getBooksOwned(String userId, DBEventType dbEventType) {
        NetworkHelper.sendRequest(HTTPRequestMethod.GET,
                "/books?owner=" + userId, null,
                bookListHandler, dbEventType);
    }

    public static void getBooksRented(String userId, DBEventType dbEventType) {
        NetworkHelper.sendRequest(HTTPRequestMethod.GET,
                "/books?rentedTo=" + userId, null,
                bookListHandler, dbEventType);
    }

    public static void getBooksOwnedAndRented(String userId, DBEventType dbEventType) {
        NetworkHelper.sendRequest(HTTPRequestMethod.GET,
                "/books?owner=" + userId + "&rentedTo=" + userId, null,
                bookListHandler, dbEventType);
    }

    public static void addRenter(String bookId, String userId, DBEventType dbEventType) {
        NetworkHelper.sendRequest(HTTPRequestMethod.PUT,
                "/books/" + bookId +  "/renter/" + userId, null,
                null, dbEventType);
    }

    public static void removeRenter(String bookId, String userId, DBEventType dbEventType) {
        NetworkHelper.sendRequest(HTTPRequestMethod.DELETE,
                "/books/" + bookId + "/renter/" + userId, null,
                null, dbEventType);
    }

    /*
    Crowds
     */

    public static void createCrowd(Crowd crowd, DBEventType dbEventType) {
        NetworkHelper.sendRequest(HTTPRequestMethod.POST,
                "/crowds", gson.toJson(crowd, Crowd.class),
                crowdHandler, dbEventType);
    }

    public static void getCrowd(String crowdId, DBEventType dbEventType) {
        Log.d("NETDBTEST", "NetworkController getCrowd");
        NetworkHelper.sendRequest(HTTPRequestMethod.GET,
                "/crowds/" + crowdId, null,
                crowdHandler, dbEventType);
    }

    public static void getCrowds(DBEventType dbEventType) {
        NetworkHelper.sendRequest(HTTPRequestMethod.GET,
                "/crowds", null,
                crowdListHandler, dbEventType);
    }

    public static void addCrowdMember(String crowdId, String userId, DBEventType dbEventType) {
        NetworkHelper.sendRequest(HTTPRequestMethod.PUT,
                "/crowds/" + crowdId + "/members/" + userId, null,
                null, dbEventType);
    }

    public static void removeCrowdMember(String crowdId, String userId, DBEventType dbEventType) {
        NetworkHelper.sendRequest(HTTPRequestMethod.DELETE,
                "/crowds/" + crowdId + "/members/" + userId, null,
                null, dbEventType);
    }

    /*
    Users
     */

    public static void createUser(User user, DBEventType dbEventType) {
        NetworkHelper.sendRequest(HTTPRequestMethod.PUT,
                "/users", gson.toJson(user, User.class),
                userHandler, dbEventType);
    }

    public static void getUser(String userId, DBEventType dbEventType){
        NetworkHelper.sendRequest(HTTPRequestMethod.GET,
                "/users/" + userId, null,
                userHandler, dbEventType);
    }
}

@@ -1770,17 +1770,16 @@
 ate();%0A%0A
-%0A
     /*%0A 
@@ -2046,24 +2046,179 @@
 bookType);%0A%0A
+        String jsonData = gson.toJson(book, Book.class);%0A        Log.i(MainTabbedActivity.TAG, %22NetworkController - createBook - jsonData: %22 + jsonData);%0A%0A
         Netw
@@ -2255,32 +2255,45 @@
 estMethod.POST,%0A
+%3C%3C%3C%3C%3C%3C%3C HEAD%0A
                 
@@ -2314,16 +2314,79 @@
 String,%0A
+=======%0A                %22/books%22, jsonData,%0A%3E%3E%3E%3E%3E%3E%3E origin/0.2%0A
         

Use typetokens to force GSON to use custom serializers