/*
Copyright 2016 OpenMarket Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import React from 'react';
import classnames from 'classnames';

export default class DirectorySearchBox extends React.Component {
    constructor() {
        super();
        this.collectInput = this.collectInput.bind(this);
        this.onClearClick = this.onClearClick.bind(this);
        this.onChange = this.onChange.bind(this);
        this.onKeyUp = this.onKeyUp.bind(this);
        this.onJoinButtonClick = this.onJoinButtonClick.bind(this);

        this.input = null;
    }

    collectInput(e) {
        this.input = e;
    }

    onClearClick() {
        if (this.input) {
            this.input.value = '';
            this.input.focus();

            if (this.props.onClear) {
                this.props.onClear();
            }
        }
    }

    onChange() {
        if (!this.input) return;

        if (this.props.onChange) {
            this.props.onChange(this.input.value);
        }
    }

    onKeyUp(ev) {
        if (ev.key == 'Enter') {
            if (this.props.onJoinClick) {
                this.props.onJoinClick(this.input.value);
            }
        }
    }

    onJoinButtonClick() {
    }

    _contentLooksLikeAlias() {
        return true;
    }

    render() {
        const searchbox_classes = {
            mx_DirectorySearchBox: true,
        };
        searchbox_classes[this.props.className] = true;

        let join_button;
        if (this._contentLooksLikeAlias()) {
            join_button = <span className="mx_DirectorySearchBox_joinButton"
                onClick={this.onJoinButtonClick}
            >
                Join
            </span>;
        }

        return <span className={classnames(searchbox_classes)}>
            <div className="mx_DirectorySearchBox_container">
                <input type="text" size="64"
                    className="mx_DirectorySearchBox_input"
                    ref={this.collectInput}
                    onChange={this.onChange} onKeyUp={this.onKeyUp}
                    placeholder="Find a room by keyword or room ID (#matrix:matrix.org)"
                />
                {join_button}
                <span className="mx_DirectorySearchBox_clear_wrapper">
                    <span className="mx_DirectorySearchBox_clear" onClick={this.onClearClick} />
                </span>
            </div>
        </span>;
    }
}

DirectorySearchBox.propTypes = {
    className: React.PropTypes.string,
    onChange: React.PropTypes.func,
    onClear: React.PropTypes.func,
    onJoinClick: React.PropTypes.func,
};

@@ -1032,16 +1032,74 @@
  = null;
+%0A%0A        this.state = %7B%0A            value: '',%0A        %7D;
 %0A    %7D%0A%0A
@@ -1184,37 +1184,45 @@
     
-if (
 this.
-input) %7B%0A   
+setState(%7Bvalue: ''%7D);%0A%0A
         
  thi
@@ -1209,33 +1209,36 @@
  ''%7D);%0A%0A        
- 
+if (
 this.input.value
@@ -1235,20 +1235,11 @@
 nput
-.value = '';
+) %7B
 %0A   
@@ -1388,16 +1388,18 @@
 nChange(
+ev
 ) %7B%0A    
@@ -1426,16 +1426,65 @@
  return;
+%0A        this.setState(%7Bvalue: ev.target.value%7D);
 %0A%0A      
@@ -1545,29 +1545,29 @@
 Change(this.
-input
+state
 .value);%0A   
@@ -1717,21 +1717,21 @@
 ck(this.
-input
+state
 .value);
@@ -1796,61 +1796,293 @@
     
-%7D%0A%0A    _contentLooksLikeAlias() %7B%0A        return tr
+    if (this.props.onJoinClick) %7B%0A            this.props.onJoinClick(this.state.value);%0A        %7D%0A    %7D%0A%0A    _contentLooksLikeAlias() %7B%0A        if (!this.input) return false;%0A%0A        // liberal test for things that look like room aliases%0A        return /#.+:.+/.test(this.state.val
 ue
+)
 ;%0A  
@@ -2674,17 +2674,32 @@
 xt%22 
-size=%2264%22
+value=%7Bthis.state.value%7D
 %0A   

Implement join button appearing