import React from "react-native";
import StartDiscussion from "../views/StartDiscussion";
import Container from "./Container";
import Facebook from "../modules/Facebook";
import generate from "../lib/generate.browser";
import url from "../lib/url";

const {
	ToastAndroid
} = React;

const PERMISSION_PUBLISH_ACTIONS = "publish_actions";
const PERMISSION_PUBLISH_ERROR = "REQUEST_PERMISSION_ERROR";

class StartDiscussionContainer extends React.Component {
	_getPublishPermissions = async () => {
		const result = await Facebook.logInWithPublishPermissions([ PERMISSION_PUBLISH_ACTIONS ]);

		if (result.permissions_granted.indexOf(PERMISSION_PUBLISH_ACTIONS) === -1) {
			throw new Error(PERMISSION_PUBLISH_ERROR);
		}

		return result;
	};

	_isFacebookPermissionGranted = async () => {
		try {
			const token = await Facebook.getCurrentAccessToken();

			return token.permissions_granted.indexOf(PERMISSION_PUBLISH_ACTIONS) > -1;
		} catch (err) {
			return false;
		}
	};

	_requestFacebookPermissions = async () => {
		try {
			const granted = await this._isFacebookPermissionGranted();

			if (!granted) {
				await this._getPublishPermissions();
			}
		} catch (err) {
			if (err.message === PERMISSION_PUBLISH_ERROR) {
				throw err;
			} else {
				await this._getPublishPermissions();
			}
		}
	};

	_shareOnFacebook = async content => {
		try {
			const token = await Facebook.getCurrentAccessToken();

			if (token && token.user_id) {
				await Facebook.sendGraphRequest("POST", `/${token.user_id}/feed`, content);
			}

			ToastAndroid.show("Post shared on Facebook", ToastAndroid.SHORT);
		} catch (err) {
			ToastAndroid.show("Failed to share post on Facebook", ToastAndroid.SHORT);
		}
	};

	_postDiscussion = async ({ title, text, thread, image }, shareOnFacebook) => {
		const id = thread || generate.uid();

		const post = await this.dispatch("text", {
			id,
			text: text.trim(),
			title: title.trim(),
			thread: id,
			to: this.props.room,
			from: this.props.user
		});

		const content = {
			link: url.get("thread", post)
		};

		if (image) {
			content.image = image;
		}

		if (shareOnFacebook) {
			this._shareOnFacebook(content);
		}

		return post;
	};

	render() {
		return (
			<StartDiscussion
				{...this.props}
				postDiscussion={this._postDiscussion}
				requestFacebookPermissions={this._requestFacebookPermissions}
				isFacebookPermissionGranted={this._isFacebookPermissionGranted}
			/>
		);
	}
}

StartDiscussionContainer.propTypes = {
	room: React.PropTypes.string.isRequired,
	user: React.PropTypes.string.isRequired
};

export default Container(StartDiscussionContainer);

@@ -1011,32 +1011,50 @@
 ync () =%3E %7B%0A
+%09%09let requested;%0A%0A
 %09%09try %7B%0A
 %09%09%09const gra
@@ -1033,32 +1033,32 @@
 ested;%0A%0A%09%09try %7B%0A
-
 %09%09%09const granted
@@ -1119,24 +1119,46 @@
 !granted) %7B%0A
+%09%09%09%09requested = true;%0A
 %09%09%09%09await th
@@ -1220,48 +1220,17 @@
 if (
-err.message === PERMISSION_PUBLISH_ERROR
+requested
 ) %7B%0A

Fix requesting permission