package dp.mendelu.roman.diplomkaapp42;

import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.shephertz.app42.paas.sdk.android.App42API;
import com.shephertz.app42.paas.sdk.android.App42File;
import com.shephertz.app42.paas.sdk.android.storage.Query;
import com.shephertz.app42.paas.sdk.android.storage.QueryBuilder;
import com.shephertz.app42.paas.sdk.android.storage.Storage;
import com.shephertz.app42.paas.sdk.android.storage.StorageService;

import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

/**
 * Ukazkova trida pro synchronizaci dat. V aplikaci nepouzita.
 */
public class SyncCloud extends AsyncTask<URL, Void, ArrayList<Storage.JSONDocument>> {
    Activity mActivity;
    private static final String TAG = "SyncCloud";
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ss.SSS'Z'");
    Date date = new Date();

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

    @Override
    protected ArrayList<Storage.JSONDocument> doInBackground(URL... params) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ss.SSS'Z'");
        Date date = new Date();
        Log.i(TAG + " Start>", df.format(date));
        StorageService storageService = App42API.buildStorageService();
        ArrayList<Storage.JSONDocument> jsonDocs = storageService.findAllDocuments
                (Constants.CLOUD_DB_NAME, Constants.COLLECTION_NAME).getJsonDocList();
        //ArrayList<Storage.JSONDocument> jsonDocuments = storage.getJsonDocList();
        return jsonDocs;
    }

    @Override
    protected void onPostExecute(ArrayList<Storage.JSONDocument> jsonDocs) {
        super.onPostExecute(jsonDocs);
        saveOneToDatabase(jsonDocs);
    }

    public void saveOneToDatabase(ArrayList<Storage.JSONDocument> jsonDocs) {
        Log.i(TAG, "saveToDatabase");
        Log.i(TAG + " Start>", df.format(date));
        SQLiteDatabase db = (new DatabaseHelper(mActivity)).getWritableDatabase();
        ContentValues cv=new ContentValues();
        //Upgrade smaze celou tabulku!!!
        DatabaseHelper dq = new DatabaseHelper(mActivity);
        dq.onUpgrade(db, 1, 2);
        for(Storage.JSONDocument oneJsonDoc : jsonDocs) {
            cv.put(Constants.UPDATED_AT, oneJsonDoc.getUpdatedAt());
            cv.put(Constants.CREATED_AT, oneJsonDoc.getCreatedAt());
            cv.put(Constants.DOC_ID, oneJsonDoc.getDocId());
            cv.put(Constants.JSON, oneJsonDoc.getJsonDoc());
            if (oneJsonDoc.getLocation() == null) {
                Log.i(TAG, "Location NULL");
                cv.put(Constants.LATITUDE, (byte[]) null);
                cv.put(Constants.LONGITUDE, (byte[]) null);
            } else {
                cv.put(Constants.LATITUDE, oneJsonDoc.getLocation().getLat().floatValue());
                cv.put(Constants.LONGITUDE, oneJsonDoc.getLocation().getLng().floatValue());
            }
            db.insert(Constants.TABLE_NAME, Constants.JSON, cv);
            Log.i(TAG, "insert");
        }
        db.close();
        Log.i(TAG + " End>", df.format(date));
    }

    void parse() throws ParseException {
        String updatedAt = "2015-12-23T01:41:40.437Z";
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss:SSS");
        Date result = df.parse(updatedAt);
        Log.i(TAG, result + "");

    }
}
