/**
 *  ---------
 * |.##> <##.|  Open Smart Card Development Platform (www.openscdp.org)
 * |#       #|
 * |#       #|  Copyright (c) 2017 CardContact Systems GmbH
 * |'##> <##'|  Schuelerweg 38, 32429 Minden, Germany (www.cardcontact.de)
 *  ---------
 *
 *  This file is part of OpenSCDP.
 *
 *  OpenSCDP is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  OpenSCDP 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpenSCDP; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @fileoverview Controller for Person and System subjects
 */

var MenuModel = require('pki-as-a-service/ui/MenuModel').MenuModel;
var Person = require('pki-as-a-service/subjects/Person').Person;



function BaseSubjectController() {
	GPSystem.log(GPSystem.DEBUG, module.id, "new BaseSubjectController()");
}

exports.BaseSubjectController = BaseSubjectController;



BaseSubjectController.prototype.getView = function(req, res, subjectController, subject) {
	default xml namespace = "http://www.w3.org/1999/xhtml";

	var homeURL = subjectController.ui.homeURL(req.url);

	var roleDAO = subjectController.service.daof.getRoleDAO();

	var div = <div />;

	var subjectDetails =
		<div>
			<h2>Subject Details</h2>
			<table class="pure-table pure-table-horizontal form-table"/>
		</div>;

	var table = subjectDetails..table;

	if (subject.getName()) {
		table.tr +=
			<tr>
				<th>Name</th>
				<td>{subject.getName()}</td>
			</tr>;
	}

	if (subject.getEmail()) {
		table.tr +=
			<tr>
				<th>Email</th>
				<td>{subject.getEmail()}</td>
			</tr>;
	}

	table.tr +=
		<tr>
			<th>Assurance Level</th>
			<td>{subject.bo.assuranceLevel}</td>
		</tr>;

	if (subject.bo.serviceRequestId) {
		var url = homeURL + "/sr/" + subject.bo.serviceRequestId;

		table.tr +=
			<tr>
				<th>Service Request</th>
				<td><a href={url}>{subject.bo.serviceRequestId}</a></td>
			</tr>;
	}

	if (subject.bo.managedByRoleId != null) {
		var managingRole = roleDAO.getRoleById(subject.bo.managedByRoleId);
		var url = homeURL + "/role/" + managingRole.id;
		var table = subjectDetails..table;
		table.tr +=
			<tr>
				<th>Managed By</th>
				<td><a href={url}>{managingRole.name}</a></td>
			</tr>;
	}

	div.appendChild(subjectDetails);

	var holderViews = subjectController.renderHolderViews(subject, homeURL);
	if (holderViews.div.length() > 0) {
		div.appendChild( <h2>Holder of</h2> );
		div.appendChild(holderViews);
	}

	var certTable = subjectController.renderCertificate(subject, homeURL);
	if (certTable) {
		div.appendChild( <h2>Certificates</h2> );
		div.appendChild(certTable);
	}

	div.appendChild( <h2>Token</h2> );
	div.appendChild(subjectController.renderToken(subject, homeURL));

	var assignedRoles = subjectController.renderAssignedRoles(subject, homeURL);
	if (assignedRoles) {
		div.appendChild(assignedRoles);
	}

	return div;
}



BaseSubjectController.prototype.getMenuModel = function(req, subjectController, subject) {
	default xml namespace = "http://www.w3.org/1999/xhtml";

	var homeURL = subjectController.ui.homeURL(req.url);
	var menuModel = subjectController.getMenuModelFromRegistry("Subject", subject, homeURL);

	if (!menuModel.hasChildren()) {
		return null;
	}

	return menuModel;
}
