#include <libfilezilla/glue/wx.hpp>
#include <wx/evtloop.h>

#include "serveradministrator.hpp"
#include "helpers.hpp"
#include "glue.hpp"

void ServerAdministrator::Dispatcher::operator()(administration::get_idp_user_info::response &&v)
{
	invoke_on_admin([this, v = std::move(v)] {
		if (server_admin_.rmp_loop_timer_) {
			server_admin_.rmp_loop_timer_->Stop();
		}

		if (!server_admin_.rmp_loop_) {
			return;
		}

		if (v) {
			std::tie(server_admin_.rmp_idp_user_info_) = std::move(v->tuple());
		}
		else {
			server_admin_.rmp_loop_error_ = fz::to_wxString(std::get<0>(v.error().v_));
		}

		server_admin_.rmp_loop_->Exit();
	});
}

void ServerAdministrator::Dispatcher::operator()(administration::get_idp_properties::response &&v)
{
	invoke_on_admin([this, v = std::move(v)] {
		if (server_admin_.rmp_loop_timer_) {
			server_admin_.rmp_loop_timer_->Stop();
		}

		if (!server_admin_.rmp_loop_) {
			return;
		}

		if (v) {
			std::tie(server_admin_.rmp_idp_user_info_.properties) = std::move(v->tuple());
		}
		else {
			server_admin_.rmp_loop_error_ = _S("Couldn't get Identity Profile properties from the server. Check the logs for more information.");
		}

		server_admin_.rmp_loop_->Exit();
	});
}

FZ_RMP_INSTANTIATE_HERE_DISPATCHING_FOR(administration::engine, ServerAdministrator::Dispatcher, administration::get_idp_user_info::response);
FZ_RMP_INSTANTIATE_HERE_DISPATCHING_FOR(administration::engine, ServerAdministrator::Dispatcher, administration::get_idp_properties::response);

