package dp.mendelu.roman.diplomkaapp42;

import android.app.Activity;
import android.os.AsyncTask;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.shephertz.app42.paas.sdk.android.App42API;
import com.shephertz.app42.paas.sdk.android.App42CallBack;
import com.shephertz.app42.paas.sdk.android.storage.Storage;
import com.shephertz.app42.paas.sdk.android.storage.StorageService;
import com.shephertz.app42.paas.sdk.android.user.User;
import com.shephertz.app42.paas.sdk.android.user.UserService;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.sql.Date;
import java.util.ArrayList;

/**
 * getAllUsers() from cloud
 * v aplikaci nepouzita
 */
public class AsyncLoad extends AsyncTask<URL, Integer, ArrayList<User>>{

    Activity mActivity;

    public AsyncLoad(Activity activity){
        mActivity = activity;
    }


    @Override
    protected ArrayList<User> doInBackground(URL... params) {
        Log.i("doBackground", "OK");

        final UserService userService = App42API.buildUserService();
        ArrayList<User> listUsers;

        listUsers = userService.getAllUsers();
        for (User object : userService.getAllUsers()) {
            listUsers.add(object);
            Log.i("AllUsers: ", object.getUserName() + " " + object.getEmail() + " " + object.getSessionId());
        }
        return listUsers;
    }

    protected void onPostExecute(ArrayList<User> users){
        TextView controlCloudText  = (TextView)mActivity.findViewById(R.id.controlCloudText);

        if (users.isEmpty()){
            Log.i("onPostExecute", "empty");
        } else {
            Log.i("TextView", "" + users.size());
            Log.i("TextView", "nastav");

            String pom = "";
            for(User user : users){
                pom = user.getEmail() +
                        user.getUserName() +
                        user.getSessionId();
                Log.i("Pom", pom);
            }
            controlCloudText.setText(pom);
        }
    }
}

