import React from 'react'

let className = 'Duoshuo'

const Duoshuo = React.createClass({

    propTypes: {
        threadKey: React.PropTypes.string,
        shortName: React.PropTypes.string
    },

    componentDidMount(){
        var node = React.findDOMNode(this.refs.duoshuo)
        var rootDocument = node.ownerDocument
        var rootWindow = 'defaultView' in rootDocument ? rootDocument.defaultView : rootDocument.parentWindow

        rootWindow.duoshuoQuery = {
            short_name: this.props.shortName || "amazepage"
        }

        'DUOSHUO' in rootWindow ? rootWindow.DUOSHUO.init() : this._loadDSScript(rootDocument)
    },

    _loadDSScript(rootDocument){
        var ds = document.createElement('script');
        ds.type = 'text/javascript';
        ds.async = true;
        ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
        ds.charset = 'UTF-8';
        (rootDocument.getElementsByTagName('head')[0]
        || rootDocument.getElementsByTagName('body')[0]).appendChild(ds);
    },

    render() {
        return (
            <div ref="duoshuo" className="am-duoshuo am-duoshuo-default"
                 data-ds-short-name={this.props.shortName || 'amazepage'}>
                <div className="ds-thread" data-thread-key={this.props.threadKey || className}></div>
            </div>
        )
    },

    componentWillUnmount(){

    }
})

export default Duoshuo
