/*
Copyright 2015 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.
*/

'use strict';

var React = require('react');
var MatrixClientPeg = require('matrix-react-sdk/lib/MatrixClientPeg');
var IncomingCallBoxController = require(
    "matrix-react-sdk/lib/controllers/molecules/voip/IncomingCallBox"
);

module.exports = React.createClass({
    displayName: 'IncomingCallBox',
    mixins: [IncomingCallBoxController],

    getRingAudio: function() {
        return this.refs.ringAudio.getDOMNode();
    },

    render: function() {
        if (!this.state.incomingCall || !this.state.incomingCall.roomId) {
            return (
                <div>
                    <audio ref="ringAudio" loop>
                        <source src="media/ring.ogg" type="audio/ogg" />
                        <source src="media/ring.mp3" type="audio/mpeg" />
                    </audio>
                </div>
            );
        }
        var caller = MatrixClientPeg.get().getRoom(this.state.incomingCall.roomId).name;
        return (
            <div className="mx_IncomingCallBox">
                <img className="mx_IncomingCallBox_chevron" src="img/chevron-left.png" width="9" height="16" />
                <audio ref="ringAudio" loop>
                    <source src="media/ring.ogg" type="audio/ogg" />
                    <source src="media/ring.mp3" type="audio/mpeg" />
                </audio>
                <div className="mx_IncomingCallBox_title">
                    Incoming { this.state.incomingCall ? this.state.incomingCall.type : '' } call from { caller }
                </div>
                <div className="mx_IncomingCallBox_buttons">
                    <div className="mx_IncomingCallBox_buttons_cell">
                        <div className="mx_IncomingCallBox_buttons_decline" onClick={this.onRejectClick}>
                            Decline
                        </div>
                    </div>
                    <div className="mx_IncomingCallBox_buttons_cell">
                        <div className="mx_IncomingCallBox_buttons_accept" onClick={this.onAnswerClick}>
                            Accept
                        </div>
                    </div>
                </div>
            </div>
        );
    }
});

@@ -1014,24 +1014,25 @@
 ion() %7B%0A
+%0A
         
 if (!thi
@@ -1027,126 +1027,258 @@
     
-if (!this.state.incomingCall %7C%7C !this.state.incomingCall.roomId) %7B%0A            return (%0A                %3Cdiv%3E%0A        
+// NB: This block MUST be the first thing inside the %3Cdiv%3E else react won't%0A        // know that they refer to the same thing and so will clobber them between%0A        // in-call / not-in-call resulting in no inbound audio.%0A        var audioBlock = (%0A
     
@@ -1310,32 +1310,24 @@
 udio%22 loop%3E%0A
-        
             
@@ -1391,32 +1391,24 @@
             
-        
 %3Csource src=
@@ -1457,32 +1457,24 @@
         
-        
 %3C/audio%3E
 %0A       
@@ -1457,32 +1457,195 @@
         %3C/audio%3E
+%0A        );%0A%0A        if (!this.state.incomingCall %7C%7C !this.state.incomingCall.roomId) %7B%0A            return (%0A                %3Cdiv%3E%0A                    %7BaudioBlock%7D
 %0A               
@@ -1832,16 +1832,45 @@
 llBox%22%3E%0A
+                %7BaudioBlock%7D%0A
         
@@ -1977,217 +1977,8 @@
  /%3E%0A
-                %3Caudio ref=%22ringAudio%22 loop%3E%0A                    %3Csource src=%22media/ring.ogg%22 type=%22audio/ogg%22 /%3E%0A                    %3Csource src=%22media/ring.mp3%22 type=%22audio/mpeg%22 /%3E%0A                %3C/audio%3E%0A
     

Fix inbound audio