'use babel';

var firebase = require("firebase/app");
require("firebase/auth");
require("firebase/database");
var config = {
    apiKey: "AIzaSyCh_DO86KwghVrvE5WyeZ0h0qK3bLIQxi8",
    authDomain: "covalence-6384b.firebaseapp.com",
    databaseURL: "https://covalence-6384b.firebaseio.com",
    storageBucket: "covalence-6384b.appspot.com",
};
firebase.initializeApp(config);

import CovalenceView from './covalence-view';
import {
    CompositeDisposable
} from 'atom';

var changedData = firebase.database().ref('projects');
changedData.on('value', function(pulledData) {
    atom.workspace.observeTextEditors(function(editor) {
        // editor.setTextInBufferRange([
        //     [0, 0],
        //     [0, 0]
        // ], pulledData.val().pageData);
        editor.setText(pulledData.val().pageData)
    });
});

export default {

    covalenceView: null,
    modalPanel: null,
    subscriptions: null,

    activate(state) {
        this.covalenceView = new CovalenceView(state.covalenceViewState);
        this.modalPanel = atom.workspace.addModalPanel({
            item: this.covalenceView.getElement(),
            visible: false
        });

        // Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable
        this.subscriptions = new CompositeDisposable();

        // Register command that toggles this view
        this.subscriptions.add(atom.commands.add('atom-workspace', {
            'covalence:toggle': () => this.toggle()
        }));
    },

    deactivate() {
        this.modalPanel.destroy();
        this.subscriptions.dispose();
        this.covalenceView.destroy();
    },

    serialize() {
        return {
            covalenceViewState: this.covalenceView.serialize()
        };
    },

    toggle() {
        var pageData;
        console.log('Covalence was toggled!');
        atom.workspace.observeTextEditors(function(editor) {
            editor.onDidChange(function(){
              pageData = editor.getText();
              console.log(pageData);
              sendData(pageData)
            })
        });

        function sendData(pageData) {
            firebase.database.INTERNAL.forceWebSockets();
            console.log('rawr');
            console.log(pageData);
            firebase.database().ref("projects").set({
                "pageData": pageData
            });
        }
        // sendData(pageData);
    }

};

@@ -3,24 +3,85 @@
 se babel';%0A%0A
+var currentRow = 0;%0Avar newRow = 0;%0Avar applyChange = true;%0A%0A
 var firebase
@@ -691,19 +691,16 @@
 %0A       
- //
  editor.
@@ -725,34 +725,56 @@
 ge(%5B%0A       
- //
      %5B
-0
+pulledData.val().rowToEdit
 , 0%5D,%0A      
@@ -778,18 +778,40 @@
     
- //
      %5B
-0
+pulledData.val().rowToEdit
 , 0%5D
@@ -818,19 +818,16 @@
 %0A       
- //
  %5D, pull
@@ -839,34 +839,198 @@
 a.val().
-pageData
+lineData);%0A        console.log(currentRow);%0A        console.log(newRow);%0A        applyChange = false;%0A        // currentRow = editor.setCursorBufferPosition(%5BnewRow, 0%5D
 );%0A
+%0A
        
+ //
  editor.
@@ -1054,19 +1054,19 @@
 a.val().
-pag
+lin
 eData)%0A 
@@ -2067,45 +2067,78 @@
     
-console.log('Covalence was toggled!')
+var lineData;%0A        // var currentRow = 0;%0A        // var newRow = 0
 ;%0A  
@@ -2210,52 +2210,745 @@
     
-  editor.onDidChange(function()%7B%0A           
+currentRow = editor.getCursorBufferPosition().row;%0A          newRow = editor.getCursorBufferPosition().row;%0A          editor.onDidChange(function()%7B%0A            newRow = editor.getCursorBufferPosition().row;%0A            console.log(currentRow);%0A            console.log(newRow);%0A            if (newRow !== currentRow && applyChange === true) %7B%0A              lineData = editor.lineTextForBufferRow(currentRow);%0A              console.log(lineData);%0A              if (lineData) %7B%0A                sendData(lineData, currentRow);%0A                console.log(lineData);%0A                // currentRow = editor.getCursorBufferPosition().row;%0A              %7D%0A            %7D%0A          %7D)%0A%0A            // editor.onDidChange(function()%7B%0A            //
    p
@@ -2979,34 +2979,37 @@
 ();%0A            
+//
   
+ 
 console.log(page
@@ -3018,32 +3018,35 @@
 ta);%0A           
+ //
    sendData(page
@@ -3058,24 +3058,27 @@
 %0A           
+ //
  %7D)%0A        
@@ -3108,24 +3108,35 @@
 endData(
-pag
+lin
 eData
+, rowToEdit
 ) %7B%0A    
@@ -3217,14 +3217,16 @@
 log(
-'rawr'
+lineData
 );%0A 
@@ -3244,32 +3244,33 @@
 console.log(
-pageData
+rowToEdit
 );%0A         
@@ -3335,19 +3335,19 @@
    %22
-pag
+lin
 eData%22: 
 page
@@ -3342,24 +3342,64 @@
 eData%22: 
-pag
+lin
 eData
+,%0A                %22rowToEdit%22: rowToEdit
 %0A       

add true false toggle to event listener