package net.yasme.android.storage;

import android.content.Context;

import com.j256.ormlite.stmt.DeleteBuilder;

import net.yasme.android.connection.ChatTask;
import net.yasme.android.entities.Chat;
import net.yasme.android.exception.RestServiceException;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by robert on 13.06.14.
 */
public class DatabaseManager {

    static private DatabaseManager instance;
    private static Boolean initialized = false;

    static public void init(Context context, long userId, String accessToken) {
        if (null == instance) {
            instance = new DatabaseManager(context);
        }

        long numberOfChats = 16L;
        Chat chat = null;
        for(long i = 0; i < numberOfChats; i++) {
            try {
                chat = ChatTask.getInstance().getInfoOfChat(i, userId, accessToken);
            } catch (RestServiceException e) {
                e.printStackTrace();
            }
            if(chat != null) {
                instance.addChat(chat);
            }

        }

        initialized = true;
    }

    public static Boolean isInitialized() {
        return initialized;
    }

    static public DatabaseManager getInstance() {
        return instance;
    }

    private DatabaseHelper helper;

    private DatabaseManager(Context context) {
        helper = new DatabaseHelper(context);
    }

    private DatabaseHelper getHelper() {
        return helper;
    }


    /******* CRUD functions ******/

    /**
     * Adds one chat to database
     */
    public void addChat(Chat c) {
        try {
            getHelper().getChatDao().create(c);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * This function will return all chats from database
     *
     * @return List of chats or null on error
     */
    public ArrayList<Chat> getAllChats() {
        List<Chat> chats = null;
        try {
            chats = getHelper().getChatDao().queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        if(chats == null) {
            return null;
        }
        ArrayList<Chat> chatsArray = new ArrayList(chats);
        return chatsArray;
    }

    /**
     * This function will return one chats from database with chatId
     *
     * @param chatId        ID (primary key) of chat
     * @return chat with chatID or null if chat not exists
     */
    public Chat getChat(long chatId) {
        Chat chat = null;
        try {
            Long cId = chatId;
            //TODO: conversion may cause overrun!
            chat = getHelper().getChatDao().queryForId(cId.intValue());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return chat;
    }

    /**
     * This function will delete chat with chatName
     * from table
     *
     * @param chatName    the name of the chat
     */
    public void deleteChat(String chatName) {
        try {
            DeleteBuilder<Chat, Integer> deleteBuilder = getHelper().getChatDao().deleteBuilder();
            deleteBuilder.where().eq(DatabaseConstants.CHAT_NAME, chatName);
            deleteBuilder.delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * This function will delete a chat with chatId
     * from table
     *
     * @param chatId        ID (primary key) of chat
     */
    public void deleteChat(int chatId) {
        try {
            getHelper().getChatDao().deleteById(chatId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * This function will update a chat
     * in table
     *
     * @param chat    Chat
     */
    public void updateChat(Chat chat) {
        try {
            getHelper().getChatDao().update(chat);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
@@ -60,16 +60,45 @@
 Context;
+%0Aimport android.os.AsyncTask;
 %0A%0Aimport
@@ -705,25 +705,24 @@
       %7D%0A
-%0A
         
 long num
@@ -717,197 +717,102 @@
     
-long numberOfChats = 16L;%0A        Chat chat = null;%0A        for(long i = 0; i %3C numberOfChats; i++) %7B%0A            try %7B%0A                chat = ChatTask.getInstance().getInfoOfChat(i, 
+GetSeedTask getSeedTask = new GetSeedTask();%0A        getSeedTask.execute(Long.toString(
 userId
+)
 , ac
@@ -827,203 +827,8 @@
 n);%0A
-            %7D catch (RestServiceException e) %7B%0A                e.printStackTrace();%0A            %7D%0A            if(chat != null) %7B%0A                instance.addChat(chat);%0A            %7D%0A%0A        %7D%0A%0A
     
@@ -3682,9 +3682,760 @@
 %7D%0A    %7D%0A
+%0A%0A    public static class GetSeedTask extends AsyncTask%3CString, Void, Boolean%3E %7B%0A%0A        protected Boolean doInBackground(String... params) %7B%0A            ChatTask chatTask = ChatTask.getInstance();%0A%0A            long numberOfChats = 16L;%0A            Chat chat = null;%0A            for(long i = 0; i %3C numberOfChats; i++) %7B%0A                try %7B%0A                    chat = chatTask.getInfoOfChat(i, Long.parseLong(params%5B0%5D), params%5B1%5D);%0A                %7D catch (RestServiceException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A                if (chat != null) %7B%0A                    instance.addChat(chat);%0A                %7D else %7B%0A                    break;%0A                %7D%0A            %7D%0A            return true;%0A        %7D%0A    %7D%0A
 %7D

put chat seed from server in AsyncTask