
/**
 * @constructor
**/
function Room(id) {
    /** @const @type {string} */
    this.id = id;
    /** @type {string} */
    this.name;
    /** @type {string} */
    this.created;
    /** @type {Chatter} */
    this.creator;
    /** @type {boolean} */
    this.archived;
    /** @type {boolean} */
    this.isMember;
    /** @type {boolean} */
    this.starred = false;
    /** @type {number} */
    this.lastRead = 0;
    /** @type {number} */
    this.lastMsg = 0;
    /** @type {Object.<string, Chatter>} */
    this.users = {};
    /** @type {string|undefined} */
    this.topic;
    /** @type {number|undefined} */
    this.topicTs;
    /** @type {Chatter|undefined} */
    this.topicCreator;
    /** @type {string|undefined} */
    this.purpose;
    /** @type {number|undefined} */
    this.purposeTs;
    /** @type {Chatter|undefined} */
    this.purposeCreator;
    /** @type {number} */
    this.version = 0;
    /** @type {boolean} */
    this.isPrivate;
};

/**
 * @param {number} lastMsg
 * @param {number} t
**/
Room.prototype.setLastMsg = function(lastMsg, t) {
    if (this.lastMsg < lastMsg) {
        this.lastMsg = lastMsg;
        this.version = t;
        return true;
    }
    return false;
};

/**
 * @param {number} t
**/
Room.prototype.toStatic = function(t) {
    if (t >= this.version)
        return null;
    var res = {
        "id": this.id
        ,"name": this.name
        ,"created": this.created
        ,"creator": this.creator ? this.creator.id : undefined
        ,"is_archived": this.archived
        ,"is_member": this.isMember
        ,"last_read": this.lastRead
        ,"last_msg": this.lastMsg
        ,"is_private": this.isPrivate
        ,"is_starred": this.starred || undefined
    };
    if (this.isMember) {
        res["members"] = this.users ? Object.keys(this.users) : [];
        res["topic"] = {
            "value": this.topic
            ,"creator": this.topicCreator ? this.topicCreator.id : null
            ,"last_set": this.topicTs
        }
        res["purpose"] = {
            "value": this.purpose
            ,"creator": this.purposeCreator ? this.purposeCreator.id : null
            ,"last_set": this.purposeTs
        }
    }
    return res;
};

/**
 * @param {*} chanData
 * @param {ChatContext} ctx
 * @param {number} t
**/
Room.prototype.update = function(chanData, ctx, t) {
    if (chanData["name"] !== undefined) this.name = chanData["name"];
    if (chanData["created"] !== undefined) this.created = chanData["created"];
    if (chanData["creator"] !== undefined) this.creator = ctx.users[chanData["creator"]];
    if (chanData["is_archived"] !== undefined) this.archived = chanData["is_archived"];
    if (chanData["is_member"] !== undefined) this.isMember = chanData["is_member"];
    if (chanData["last_read"] !== undefined) this.lastRead = Math.max(parseFloat(chanData["last_read"]), this.lastRead);
    if (chanData["last_msg"] !== undefined) this.lastMsg = parseFloat(chanData["last_msg"]);
    if (chanData["is_private"] !== undefined) this.isPrivate = chanData["is_private"];
    if (chanData["latest"]) this.lastMsg = parseFloat(chanData["latest"]["ts"]);
    if (chanData["is_starred"] !== undefined) this.starred = chanData["is_starred"];
    if (chanData["members"]) {
        this.users = {};
        if (chanData["members"]) for (var i =0, nbMembers = chanData["members"].length; i < nbMembers; i++) {
            var member = ctx.users[chanData["members"][i]];
            this.users[member.id] = member;
            member.channels[this.id] = this;
        }
    }
    if (chanData["topic"]) {
        this.topic = chanData["topic"]["value"];
        this.topicCreator = ctx.users[chanData["topic"]["creator"]];
        this.topicTs = chanData["topic"]["last_set"];
    }
    if (chanData["purpose"]) {
        this.purpose = chanData["purpose"]["value"];
        this.purposeCreator = ctx.users[chanData["purpose"]["creator"]];
        this.purposeTs = chanData["purpose"]["last_set"];
    }
    this.version = Math.max(this.version, t);
};

/**
 * @constructor
 * @extends {Room}
 * @param {string} id
 * @param {Chatter} user
**/
function PrivateMessageRoom(id, user) {
    Room.call(this, id);
    /** @const @type {Chatter} */
    this.user = user;
    this.name = this.user.name;
    this.isPrivate = true;
    user.privateRoom = this;
};
PrivateMessageRoom.prototype = Object.create(Room.prototype);
PrivateMessageRoom.prototype.constructor = PrivateMessageRoom;

/**
 * @param {number} t
**/
PrivateMessageRoom.prototype.toStatic = function(t) {
    return t >= this.version ? null: {
        "id": this.id
        ,"created": this.created
        ,"user": this.user.id
        ,"last_read": this.lastRead
        ,"last_msg": this.lastMsg
        ,"pv": true
        ,"is_starred": this.starred || undefined
    };
};

/** @suppress {undefinedVars,checkTypes} */
(function() {
    if (typeof module !== "undefined") {
        module.exports.Room = Room;
        module.exports.PrivateMessageRoom = PrivateMessageRoom;
    }
})();

