package ca.carleton.gcrc.n2android_mobile1.connection;

import android.util.Log;

import org.json.JSONObject;

import java.util.List;
import java.util.Vector;

import ca.carleton.gcrc.couch.client.CouchDb;
import ca.carleton.gcrc.couch.client.CouchDesignDocument;
import ca.carleton.gcrc.couch.client.CouchQuery;
import ca.carleton.gcrc.couch.client.CouchQueryResults;
import ca.carleton.gcrc.n2android_mobile1.couchbase.CouchbaseDb;
import ca.carleton.gcrc.n2android_mobile1.couchbase.CouchbaseLiteService;

/**
 * Created by jpfiset on 3/21/16.
 */
public class ConnectionSyncProcess {

    protected final String TAG = this.getClass().getSimpleName();

    private Connection connection;
    private CouchbaseLiteService service;
    private CouchDb couchDb;
    private CouchDesignDocument atlasDesign;

    public ConnectionSyncProcess(CouchbaseLiteService service, Connection connection) throws Exception {
        this.service = service;
        this.connection = connection;

        couchDb = connection.getRemoteCouchDb();
        atlasDesign = couchDb.getDesignDocument("atlas");
    }

    public void synchronize() throws Exception {
        try {
            List<JSONObject> docs = getRemoteSkeletonDocs();
            for(JSONObject doc : docs){
                updateLocalSkeletonDocument(doc);
            }

            Log.v(TAG, "Number of skeleton docs: "+ docs.size());

        } catch(Exception e) {
            throw new Exception("Error while synchronizing connection",e);
        }
    }

    public List<String> getRemoteSkeletonDocIds() throws Exception {
        CouchQuery query = new CouchQuery();
        query.setViewName("skeleton-docs");
        query.setReduce(false);
        query.setIncludeDocs(false);

        CouchQueryResults results = atlasDesign.performQuery(query);

        List<String> docIds = new Vector<String>();
        List<JSONObject> rows = results.getRows();
        for(JSONObject row : rows){
            String docId = row.getString("id");
            docIds.add(docId);
        }
        return docIds;
    }

    public List<JSONObject> getRemoteSkeletonDocs() throws Exception {
        CouchQuery query = new CouchQuery();
        query.setViewName("skeleton-docs");
        query.setReduce(false);
        query.setIncludeDocs(true);

        CouchQueryResults results = atlasDesign.performQuery(query);

        List<JSONObject> docs = new Vector<JSONObject>();
        List<JSONObject> rows = results.getRows();
        for(JSONObject row : rows){
            JSONObject doc = row.getJSONObject("doc");
            docs.add(doc);
        }
        return docs;
    }

    public void updateLocalSkeletonDocument(JSONObject doc) throws Exception {
        try {
            String remoteRev = doc.optString("_rev",null);
            if( null != remoteRev ){
                doc.remove("_rev");
            }

            CouchbaseDb localDb = connection.getLocalDocumentDb();
            String docId = doc.getString("_id");
            if( localDb.documentExists(docId) ) {
                JSONObject existingDoc = localDb.getDocument(docId);
                String existingRev = existingDoc.optString("_rev",null);
                if( null != existingRev ){
                    doc.put("_rev",existingRev);
                }
                localDb.updateDocument(doc);
            } else {
                localDb.createDocument(doc);
            }

        } catch(Exception e) {
            throw new Exception("Unable to update local skeleton documents",e);
        }
    }
}

@@ -799,16 +799,86 @@
 sDesign;
+%0A    private DocumentDb documentDb;%0A    private RevisionDb revisionDb;
 %0A%0A    pu
@@ -1149,24 +1149,133 @@
 nt(%22atlas%22);
+%0A%0A        documentDb = connection.getLocalDocumentDb();%0A        revisionDb = connection.getLocalRevisionDb();
 %0A    %7D%0A%0A    
@@ -2894,32 +2894,81 @@
 %7B%0A        try %7B%0A
+            String docId = doc.getString(%22_id%22);%0A
             Stri
@@ -2995,39 +2995,41 @@
 ptString(%22_rev%22,
+ 
 null);%0A
+%0A
             if( 
@@ -3116,133 +3116,20 @@
     
-CouchbaseDb localDb = connection.getLocalDocumentDb();%0A            String docId = doc.getString(%22_id%22);%0A            if( local
+if( document
 Db.d
@@ -3194,21 +3194,24 @@
 ngDoc = 
-local
+document
 Db.getDo
@@ -3424,21 +3424,24 @@
         
-local
+document
 Db.updat
@@ -3493,21 +3493,24 @@
         
-local
+document
 Db.creat

Work in synchronization