#!/usr/bin/perl -w
#
# perl example on how to create a context in OXaaS including userAttributes
# see https://oxpedia.org/wiki/index.php?title=OX_as_a_Service_Provisioning_using_SOAP#userAttributes for
# further details
#
use strict;
use SOAP::Lite +trace => 'debug';
use Data::Dumper;

BEGIN { $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0 }

my $oxaashost    = "CHANGEME";
my $oxaasadmname = "oxaas.single";
my $oxaasadmpw   = "CHANGEME";

my $oxaascreds = SOAP::Data->type("Credentials")->value(
	\SOAP::Data->value(
		SOAP::Data->name( "login"    => "$oxaasadmname" ),
		SOAP::Data->name( "password" => "$oxaasadmpw" )
	)
);

my $soclt =
  SOAP::Lite->ns("http://soap.reseller.admin.openexchange.com")
  ->proxy( "https://$oxaashost" . "/webservices/OXResellerContextService" );

my $ctxadmin    = "oxadmin";
my $ctxadmpw    = "secret";
my $displayname = "Context Admin";
my $surname     = "Admin";
my $gname       = "Context";
my $email       = "oxadmin\@example.com";
my $lang        = "de_DE";
my $timezone    = "Europe/Berlin";

# context quota
my $ctxname  = "oxaas.single_111111";
my $ctxquota = 500;

my $themesettings = {
	framecolor => {
		setting => "io.ox/dynamic-theme//frameColor",
		value   => "framecolor"
	},
	iconcolor => {
		setting => "io.ox/dynamic-theme//iconColor",
		value   => "iconcolor"
	},
	selectioncolor => {
		setting => "io.ox/dynamic-theme//selectionColor",
		value   => "selectioncolor"
	},
	logowidth => {
		setting => "io.ox/dynamic-theme//logoWidth",
		value   => "logowidth"
	},
	logourl => {
		setting => "io.ox/dynamic-theme//logoURL",
		value   => "logourl"
	},
	logouturl => {
		setting => "io.ox/core//customLocations/logout",
		value   => "logouturl"
	}
};

my @entries;
foreach my $key ( keys %$themesettings ) {
	my $setting = $themesettings->{$key}->{setting};
	my $svalue  = $themesettings->{$key}->{value};
	if ( defined($svalue) ) {
		push @entries, \SOAP::Data->value(
			SOAP::Data->name( "key"   => $setting ),
			SOAP::Data->name( "value" => $svalue )
		);
	}
}
my @uattrs;
my $brandTaxonomy = SOAP::Data->name(
    "entries" => \SOAP::Data->value(
        SOAP::Data->name( "key"   => "types" ),
        SOAP::Data->name( "value" => $oxaasadmname )
    )
);
push @uattrs, \SOAP::Data->value(
                SOAP::Data->name( "key" => "taxonomy" ),
                SOAP::Data->name(
                    "value" => \SOAP::Data->value($brandTaxonomy)
                )
);
push @uattrs, \SOAP::Data->value(
	SOAP::Data->name( "key" => "config" ),
	SOAP::Data->name(
		"value" =>
		  \SOAP::Data->value( SOAP::Data->name( "entries" => @entries ) )
	)
);

# create a context SOAP object
my $context = SOAP::Data->type("Context")->value(
	\SOAP::Data->value(
		SOAP::Data->name( "name"     => $ctxname ),
		SOAP::Data->name( "maxQuota" => $ctxquota ),
		SOAP::Data->name(
			"userAttributes" =>
			  \SOAP::Data->value( SOAP::Data->name( "entries" => @uattrs ) )
		)
	)
);

my $result = $soclt->create(
	$context,
	SOAP::Data->value("User")->value(
		\SOAP::Data->value(
			SOAP::Data->name( "name"         => $ctxadmin ),
			SOAP::Data->name( "password"     => $ctxadmpw ),
			SOAP::Data->name( "display_name" => $displayname ),
			SOAP::Data->name( "sur_name"     => $surname ),
			SOAP::Data->name( "given_name"   => $gname ),
			SOAP::Data->name( "primaryEmail" => $email ),
			SOAP::Data->name( "email1"       => $email ),
			SOAP::Data->name( "language"     => $lang ),
			SOAP::Data->name( "timezone"     => $timezone )
		)
	),
	$oxaascreds
);

$result = $soclt->getData($context, $oxaascreds);

print Dumper($result->paramsall);

#$result = $soclt->delete($context, $oxaascreds);
