#!/usr/bin/perl

use HTTP::Tiny;
use XML::Simple;
use Data::Dumper;
use HTML::Entities;
use utf8;

$base="https://saasa.kennys.nl/brightsight/data/";

MAIN: {
	my @todo = ("telefoonlijst","verjaardagen");

	foreach (@todo) {
		unlink($_) if -e $_;
		my $res=HTTP::Tiny->new->get($base.$_."-popup.xml");
		if ($res->{success}) {
			open(DOC,">$_") or die("Can not write file $_\n");
			print DOC $res->{content};
			close(DOC);
		}
	}

	#For now, only use 'telefoonlijst', we might in the future use some extra data from verjaardagen
	$xml = new XML::Simple;
	$data = $xml->XMLin("telefoonlijst");

	my %rooms;
	$a=$data->{'Werknemer'};

	print "KAMR\tLBL_FIRSTNAME_01\tLBL_UNIQNAME_01\tLBL_LASTNAME_01\tLBL_FUNCTION_01\tLBL_DEPARTMENT_01\tLBL_FIRSTNAME_02\tLBL_UNIQNAME_02\tLBL_LASTNAME_02\tLBL_FUNCTION_02\tLBL_DEPARTMENT_02\tLBL_FIRSTNAME_03\tLBL_UNIQNAME_03\tLBL_LASTNAME_03\tLBL_FUNCTION_03\tLBL_DEPARTMENT_03\tLBL_FIRSTNAME_04\tLBL_UNIQNAME_04\tLBL_LASTNAME_04\tLBL_FUNCTION_04\tLBL_DEPARTMENT_04\tLBL_FIRSTNAME_05\tLBL_UNIQNAME_05\tLBL_LASTNAME_05\tLBL_FUNCTION_05\tLBL_DEPARTMENT_05\tLBL_FIRSTNAME_06\tLBL_UNIQNAME_06\tLBL_LASTNAME_06\tLBL_FUNCTION_06\tLBL_DEPARTMENT_06\n";

	#Lets do the rooms not mentioned in Prinsys first
	print while(<DATA>);

	foreach (@$a) {
		my $line="";

		#If we have a name and a room, then store (append) the data in the rooms hash
		$voornaam= $_->{Roepnaam};
		$kamernummer = $_->{kamernummer};

                next if(ref($voornaam) eq 'HASH');
		next if(ref($kamernummer) eq 'HASH');
		#Roomnumber needs to have a dot
		next if(length($kamernummer)<2);
		next if($kamernummer =~ /^\d/);
		$voornaam =~ s/,//;
		#$voornaam = encode_entities($voornaam);
		#Store firstname
		$line .= "$voornaam\t";
		$naam = $_->{Naam};
		$naam =~ s/, ([A-Z]\.)*/, /;
		$naam =~ s/(.*),\s+(.*)$/$2 $1/;
		$naam =~ s/\s+/ /g;
		$naam =~ s/^\s+|\s+$//g;
		#$naam = encode_entities($naam);

		#Duplicate names get special treatment (DIRTY HACK)
		# TODO: Loop over the file 1st time to get duplicate names ?
		if($voornaam eq 'Rob' or $voornaam eq 'Ben' or $voornaam eq 'Thomas' or $voornaam eq 'Gerard') {
			if(index($naam,"van") == 0) {
				$voornaam .= " v. ".substr($naam,4,1).".";
			} else {
				$voornaam .= " ".substr($naam,0,1).".";
			}
		}
		#Store fixed firstname again and rest of info
		$line .= $voornaam."\t";
		$line .= "$naam\t";
		$line .=  $_->{Functie};
		$line .= "\t".$_->{Afdeling};

		#Add it to the room, delimiter here is :
		$rooms{$kamernummer}.=":" if(defined($rooms{$kamernummer}));
		$rooms{$kamernummer}.=$line;

		#So, this is one persons data processed, loop for the next
	}

	#Sort all firstnames on a sorted list of rooms
	print $_."\t".join("\t",sort(split(":",$rooms{$_})))."\n" foreach(sort(keys(%rooms))); 
}

__DATA__
A8.01	
A8.02	HCE Lab	HCE Lab
A8.03	Mechanical Lab	Mechanical Lab
A8.04	Chemical Lab	Chemical Lab
A8.05	Ventilation	Ventilation
A8.06	Solder Lab	Solder Lab
A8.07	IT Lab 1	IT Lab 1
A8.08	IT Lab 2	IT Lab 2
A8.09	Patch room	Patch room
A6.01	Mixed Lab	Mixed Lab
A6.02	SCA Lab	SCA Lab
A6.03	Fault injection attacks	Fault injection attacks
A6.04	Patch room	Patch room
A5.07	Patch room	Patch room
B5.11	BSLB work room	BSLB work room
B5.13	Storage	Storage
B5.15	Interns	Interns
A4.00	Patch room	Patch room
A4.09	Meeting room	Meeting room
B4.11	Storage	Storage
B4.12	E.S.A.	E.S.A.
B4.13	No access	No access
B4.19	Meeting room	Meeting room
A3.00	Patch room	Patch room
A3.09	Meeting room	Meeting room
B3.11	Meeting room	Meeting room
A2.08	Meeting room	Meeting room
A2.06	Quiet room	Quiet room
A2.05	Rene	Rene
B2.16	Meeting room	Meeting room
A1.06	Storage	Storage
B1.11	Meeting room	Meeting room
A1.04	Storage	Storage
B6	Meeting room	Meeting room
B5	Storage	Storage
B4	Meeting room	Meeting room
A0.03	Meeting room	Meeting room
