/*
 * Copyright (c) 2010-2022 Belledonne Communications SARL.
 *
 * This file is part of Liblinphone
 * (see https://gitlab.linphone.org/BC/public/liblinphone).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "conference.h"

using namespace std;

ConferenceCommand::ConferenceCommand()
    : DaemonCommand(
          "conference",
          "conference add|rm|leave|enter <call_id>",
          "Create and manage an audio conference.\n"
          "You can:\n"
          "- add   : join the call with id 'call id' into the audio conference. Creates new one if none exists.\n"
          "- rm    : remove the call with id 'call id' from the audio conference.\n"
          "- leave : temporarily leave the current conference.\n"
          "- enter : re-join the conference after leaving it.") {
	addExample(make_unique<DaemonCommandExample>("conference add 1", "Status: Ok\n\n"
	                                                                 "Call Id: 1\n"
	                                                                 "Conference: add OK"));
	addExample(make_unique<DaemonCommandExample>("conference leave 1", "Status: Ok\n\n"
	                                                                   "Call Id: 1\n"
	                                                                   "Conference: leave OK"));
	addExample(make_unique<DaemonCommandExample>("conference azerty 1", "Status: Error\n\n"
	                                                                    "Reason: Invalid command format"));
	addExample(make_unique<DaemonCommandExample>("conference leave 2", "Status: Error\n\n"
	                                                                   "Reason: No call with such id."));
}

void ConferenceCommand::exec(Daemon *app, const string &args) {
	LinphoneCore *lc = app->getCore();
	int id;
	string subcommand;
	int ret;
	istringstream ist(args);
	ist >> subcommand;
	ist >> id;
	if (ist.fail()) {
		app->sendResponse(Response("Invalid command format.", Response::Error));
		return;
	}

	LinphoneCall *call = app->findCall(id);
	if (call == NULL) {
		app->sendResponse(Response("No call with such id.", Response::Error));
		return;
	}

	if (subcommand.compare("add") == 0) {
		ret = linphone_core_add_to_conference(lc, call);
	} else if (subcommand.compare("rm") == 0) {
		ret = linphone_core_remove_from_conference(lc, call);
	} else if (subcommand.compare("enter") == 0) {
		ret = linphone_core_enter_conference(lc);
	} else if (subcommand.compare("leave") == 0) {
		ret = linphone_core_leave_conference(lc);
	} else {
		app->sendResponse(Response("Invalid command format.", Response::Error));
		return;
	}

	if (ret == 0) {
		ostringstream ostr;
		ostr << "Call ID: " << id << "\n";
		ostr << "Conference: " << subcommand << " OK"
		     << "\n";
		app->sendResponse(Response(ostr.str(), Response::Ok));
		return;
	}

	app->sendResponse(Response("Command failed", Response::Error));
}
