/*
Copyright 2018 New Vector 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.
*/

const request = require('request-promise-native');
const RestRoom = require('./room');
const {approveConsent} = require('./consent');

module.exports = class RestSession {
    constructor(credentials) {
        this.credentials = credentials;
    }

    userId() {
        return this.credentials.userId;
    }

    userName() {
        return this.credentials.userId.split(":")[0].substr(1);
    }

    async setDisplayName(displayName) {
        await this._put(`/profile/${this.credentials.userId}/displayname`, {
            displayname: displayName
        });
    }

    async join(roomId) {
        const {room_id} = await this._post(`/rooms/${roomId}/join`);
        return new RestRoom(this, room_id);
    }

    async createRoom(name, options) {
        const body = {
            name,
        };
        if (options.invite) {
            body.invite = options.invite;
        }
        if (options.public) {
            body.visibility = "public";
        } else {
            body.visibility = "private";
        }
        if (options.dm) {
            body.is_direct = true;
        }
        if (options.topic) {
            body.topic = options.topic;
        }

        const {room_id} = await this._post(`/createRoom`, body);
        return new RestRoom(this, room_id);
    }

    _post(csApiPath, body) {
        return this._request("POST", csApiPath, body);
    }

    _put(csApiPath, body) {
        return this._request("PUT", csApiPath, body);
    }

    async _request(method, csApiPath, body) {
        try {
            const responseBody = await request({
                url: `${this.credentials.hsUrl}/_matrix/client/r0${csApiPath}`,
                method,
                headers: {
                    "Authorization": `Bearer ${this.credentials.accessToken}`
                },
                json: true,
                body
            });
            return responseBody;

        } catch(err) {
            const responseBody = err.response.body;
            if (responseBody.errcode === 'M_CONSENT_NOT_GIVEN') {
                await approveConsent(responseBody.consent_uri);
                return this._request(method, csApiPath, body);
            } else if(responseBody && responseBody.error) {
                throw new Error(`${method} ${csApiPath}: ${responseBody.error}`);
            } else {
                throw new Error(`${method} ${csApiPath}: ${err.response.statusCode}`);
            }
        }
    }
}

@@ -1153,16 +1153,23 @@
 n(roomId
+OrAlias
 ) %7B%0A    
@@ -1213,28 +1213,49 @@
 t(%60/
-rooms/$%7BroomId%7D/join
+join/$%7BencodeURIComponent(roomIdOrAlias)%7D
 %60);%0A

support joining with a room alias for rest session