'use strict';

var React = require('react-native');

var {
  WebView,
  Component,
  NativeModules: {
    WebViewManager
  }
} = React;

var WEB_VIEW_BRIDGE_REF = 'WEBVIEW_BRIDGE';

class WebViewBridge extends Component {
  constructor(props) {
    super(props);
  }

 /*
  * returns internal handler id
  */
  getWebViewBridgeHandler() {
    var ref = this.refs[WEB_VIEW_BRIDGE_REF];
    return ref.getWebWiewHandle();
  }

 /*
  * inject script into webView
  */
  injectBridgeScript() {
    WebViewManager.injectBridgeScript(this.getWebViewBridgeHandler());
  }

  onMessage(cb) {
    WebViewManager.onMessage(this.getWebViewBridgeHandler(), (messages) => {
      messages.forEach((message) => {
        cb(message);
      });

      //re-register the callback again
      this.onMessage(cb);
    });
  }

  evalScript(value) {
    WebViewManager.eval(this.getWebViewBridgeHandler(), value);
  }

  send(message) {
    if (typeof message !== 'string') {
      message = JSON.stringify(message);
    }

    WebViewManager.send(this.getWebViewBridgeHandler(), message);
  }

  componentDidMount() {
    //setup the internal variables of webview bridge
    WebViewManager.bridgeSetup(this.getWebViewBridgeHandler());
  }

  componentWillUnmount() {
    //removed the internal variables from objective-c side related to
    //handler id
    WebViewManager.callbackCleanup(this.getWebViewBridgeHandler());
  }

  render() {
    return (
      <WebView
        ref={WEB_VIEW_BRIDGE_REF}
        {...this.props}/>
    );
  }
}

module.exports = WebViewBridge;

@@ -67,21 +67,8 @@
 ew,%0A
-  Component,%0A
   Na
@@ -82,16 +82,16 @@
 ules: %7B%0A
+
     WebV
@@ -121,53 +121,8 @@
 t;%0A%0A
-var WEB_VIEW_BRIDGE_REF = 'WEBVIEW_BRIDGE';%0A%0A
 clas
@@ -149,17 +149,15 @@
 nds 
-Component
+WebView
  %7B%0A 
@@ -280,49 +280,51 @@
     
-var ref = this.refs%5BWEB_VIEW_BRIDGE_REF%5D;
+//this method defines in WebView component.
 %0A   
@@ -331,19 +331,20 @@
  return 
-ref
+this
 .getWebW
@@ -1032,24 +1032,98 @@
 idMount() %7B%0A
+    if (super.componentDidMount) %7B%0A      super.componentDidMount();%0A    %7D%0A
     //setup 
@@ -1255,24 +1255,102 @@
 Unmount() %7B%0A
+    if (super.componentWillUnmount) %7B%0A      super.componentWillMount();%0A    %7D%0A
     //remove
@@ -1424,16 +1424,16 @@
 dler id%0A
+
     WebV
@@ -1499,121 +1499,8 @@
 %0A  %7D
-%0A%0A  render() %7B%0A    return (%0A      %3CWebView%0A        ref=%7BWEB_VIEW_BRIDGE_REF%7D%0A        %7B...this.props%7D/%3E%0A    );%0A  %7D
 %0A%7D%0A%0A

use inheritance instead of higher order