<commit_before>import * as firebase from 'firebase'
import uuid from 'uuid'

import { dispatch, store } from '~/store'

var coordKey, coordsRef, sessionKey;

var config = {
	apiKey: "<KEY>",
	authDomain: "rooibusdev.firebaseapp.com",
	databaseURL: "https://rooibusdev.firebaseio.com",
	storageBucket: "rooibusdev.appspot.com",
	messagingSenderId: "212546233297"
}
firebase.initializeApp(config)

const DB = firebase.database()

firebase.auth().onAuthStateChanged(function(user) {
	if (user) {
		var isAnonymous = user.isAnonymous;
		var uid = user.uid;
		console.log('user in', uid)
	} else {
		console.log('user out')
	}
})

const test = (data) => {
	DB.ref('locations').once('value').then(blah => {
		console.log('work god damn it, ', blah)
	});
}

const pushCoords = (coords) => {
	console.log('pushCoords', coords)

	let uid = firebase.auth().currentUser.uid

	coordsRef = DB.ref(`locations/${uid}/coords`).push(coords).then(() => {
		console.log('pushCoords done')
	


	}).catch(() => {
		console.log('pushCoords error')
	})




}

const init = () => {
	let uid = firebase.auth().currentUser.uid
	var token = uuid.v4()

	DB.ref(`users/${uid}/locations`).set({
		[token]: 'self'
	})

	DB.ref(`locations/${uid}`).set({
		token
	})

	console.log('init done')
}

const signIn = async () => {
	// console.log('signIn start')
	return firebase.auth().signInAnonymously()

	// firebase.auth().signInAnonymously().then(u => {
	// 	console.log('signIn success uid', u.uid)
	// 	// debugger;
	// 	// let uid = firebase.auth().currentUser.uid
		

	// 	DB.ref(`locations/${u.uid}/coords`).on('value', snapshot => {
	// 		console.log('locations value', snapshot.val())
	// 	})
	// 	startLocationTracking()

	// }).catch(e => {
	// 	console.error('Sign in problem, tell Hai.')
	// 	console.log(JSON.stringify(e))
	// })
}



const getCurrentUser = () => {
	return firebase.auth().currentUser
}


const createSession = () => {
	DB.ref('locations').once()
}

const createSession2 = () => {

	let uid = firebase.auth().currentUser.uid
	var token = uuid.v4()


	
	DB.ref(`users/${uid}/locations`).set({
		[token]: 'self'
	})

	DB.ref(`locations/${uid}`).set({
		token
	})


	// coordsRef.set({ yo: 'yaaaaa'})

	DB.ref(`locations`).on('value', (stuff) => {
		console.log('locations value', stuff.val())
	})
		
	console.log('createSession2 done')

}

const onCoordsChange = (callback) => {
	console.log('yo')
	let u = firebase.auth().currentUser.uid
	DB.ref(`locations/${u}`).on('value', snapshot => {
		console.log('yooooo')
		callback(snapshot.val())
	})
}


// const sessions = db.ref('sessions')


export default { createSession, createSession2, getCurrentUser, init, onCoordsChange, pushCoords, signIn, test }


/*
users: {
	'$uid': {
		locations: {
			'$token': 'self'
			...
		}
	}
}


//non-sharing

//user A create token, stores in locations
//user A create coords object with token


this.listenTo(this.model, 'change:searchTopic')
this.set()
sessions: {
	'$session': {
		//read: user.shareToken == data.shareToken
		//write: user.shareToken == data.shareToken
		'token': 'xxx',
		'members': {
			'$member': {
				displayName: 'blah',
				coordKey: '$coord'
			},
			...
		}<commit_msg>[Dec22.0842] workflow for sharing complete<commit_after>import * as firebase from 'firebase'
import uuid from 'uuid'

import { dispatch, store } from '~/store'

var coordKey, coordsRef, locationRef, sessionKey;

var config = {
	apiKey: "<KEY>",
	authDomain: "rooibusdev.firebaseapp.com",
	databaseURL: "https://rooibusdev.firebaseio.com",
	storageBucket: "rooibusdev.appspot.com",
	messagingSenderId: "212546233297"
}
firebase.initializeApp(config)

const DB = firebase.database()

firebase.auth().onAuthStateChanged(function(user) {
	if (user) {
		var isAnonymous = user.isAnonymous;
		var uid = user.uid;
		console.log('user in', uid)
	} else {
		console.log('user out')
	}
})

const test = (data) => {
	DB.ref('locations').once('value').then(blah => {
		console.log('work god damn it, ', blah)
	});
}

const pushCoords = (coords) => {
	console.log('pushCoords', coords)
	DB.ref(`locations/${locationRef.key}/coords`).push(coords).then(() => {
		console.log('pushCoords done')
	}).catch(() => {
		console.log('pushCoords error')
	})
}

const init = () => {
	let uid = firebase.auth().currentUser.uid
	var token = uuid.v4()

	DB.ref(`users/${uid}/locations`).set({
		[token]: true
	})

	locationRef = DB.ref('locations').push()
	locationRef.set({
		token
	})

	

	console.log('init done')
}

const signIn = async () => {
	// console.log('signIn start')
	return firebase.auth().signInAnonymously()

	// firebase.auth().signInAnonymously().then(u => {
	// 	console.log('signIn success uid', u.uid)
	// 	// debugger;
	// 	// let uid = firebase.auth().currentUser.uid
		

	// 	DB.ref(`locations/${u.uid}/coords`).on('value', snapshot => {
	// 		console.log('locations value', snapshot.val())
	// 	})
	// 	startLocationTracking()

	// }).catch(e => {
	// 	console.error('Sign in problem, tell Hai.')
	// 	console.log(JSON.stringify(e))
	// })
}



const getCurrentUser = () => {
	return firebase.auth().currentUser
}


const createSession = () => {
	DB.ref('locations').once()
}

const createSession2 = () => {

	let uid = firebase.auth().currentUser.uid
	var token = uuid.v4()


	
	DB.ref(`users/${uid}/locations`).set({
		[token]: true
	})

	DB.ref(`locations/${uid}`).set({
		token
	})


	// coordsRef.set({ yo: 'yaaaaa'})

	DB.ref(`locations`).on('value', (stuff) => {
		console.log('locations value', stuff.val())
	})
		
	console.log('createSession2 done')

}

const onCoordsChange = (callback) => {
	console.log('yo')
	let u = firebase.auth().currentUser.uid
	DB.ref(`locations`).on('child_changed', snapshot => {
		console.log('yooooo')
		callback(snapshot.val())
	})
}


export default { createSession, createSession2, getCurrentUser, init, onCoordsChange, pushCoords, signIn, test }


/*
users: {
	'user1': {
		shares: {
			Uid: true
		}
	},
	'user2': {

	}
}


locations: {
	$location: {
		uid: 'user1',
		share: {
			'user2': true
		},
		.read: auth !== null && ( auth.uid === data.uid || data.share.user2.exists() )
		.write: auth !== null && auth.uid === data.uid
	}
}

//sharing begins

1. user A creates a session
	sessions: {
		'session1': {
			token: 'xxx',
			.read: auth !== null && user.sessionToken === data.token
			.write: auth !== null && user.sessionToken === data.token
		}
	}

2. user A sends url with session token
3. user B read and extract session token
4. user B writes session token
5. user B writes uid to session.accessRequests
	'session1': {
		accessRequests: {
			'request1': {
				uid: 'uid',
				status: 'IN_PROGRESS'
			}
		}
	}
6. user A sees request and writes to locations


//user A create token, stores in locations
//user A create coords object with token


this.listenTo(this.model, 'change:searchTopic')
this.set()
sessions: {
	'$session': {
		//read: user.shareToken == data.shareToken
		//write: user.shareToken == data.shareToken
		'token': 'xxx',
		'members': {
			'$member': {
				displayName: 'blah',
				coordKey: '$coord'
			},
			...
		}
{'fraction_non_alphanumeric': 0.1943697835620314, 'fraction_numerical': 0.0076389871268920636, 'mean_word_length': 3.6118721461187215, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6170751', 'n_tokens_mistral': 2775, 'n_tokens_neox': 2574, 'n_words': 505}