. $("$libFolder\sharepoint\CommonConstants.ps1")
. $("$libFolder\sharepoint\CommonMsgConstants.ps1")
#. $("$execFolder\MsgConstants.ps1")

cls

$sPAdminTool="$env:CommonProgramFiles$STSADM_RELATIVE_PATH"
$executingScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent

$solutionNameCollection = @()
$sandboxedSolutionNameCollection = @()
$siteFeatureNameCollection = @()
$webFeatureNameCollection = @()
$webApplicationFeatureNameCollection = @()
$listDataCollection = @()
#$listDataCollection = @{}

Write-Host $MSG_SPSNAPIN_LOADING
if(!(Get-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction:SilentlyContinue))
{
    Add-PSSnapin Microsoft.SharePoint.Powershell
}

function CheckArgumentCount($argumentList, $count)
{
	if ($argumentList.Length -ne $count)
	{
	    Write-Host $($MSG_MISSING_ARGUMENT_COUNT -f $MSG_ARGUMENT_LIST, $MSG_SCRIPT_USAGE, $MSG_COMMAND_EXAMPLE) -ForegroundColor Magenta
		return $false
	}
	return $true
}

function CheckArgumentCountRange($argumentList, $minCount, $maxCount)
{
	if (!($argumentList.Length -ge $minCount -and $argumentList.Length -le $maxCount))
	{
	    Write-Host $($MSG_MISSING_ARGUMENT_COUNT -f $MSG_ARGUMENT_LIST, $MSG_SCRIPT_USAGE, $MSG_COMMAND_EXAMPLE) -ForegroundColor Magenta
		return $false
	}
	return $true
}


function GetSettingsMsgFromConfigFile($configuration)
{
	$settingsMsg = New-Object PSObject
	Add-Member -InputObject $settingsMsg -MemberType NoteProperty -Name title -Value $configuration.messageList.title
#	Add-Member -InputObject $settingsMsg -MemberType NoteProperty -Name argumentList -Value $configuration.messageList.argumentList
#	Add-Member -InputObject $settingsMsg -MemberType NoteProperty -Name scriptUsage -Value $configuration.messageList.scriptUsage
#	Add-Member -InputObject $settingsMsg -MemberType NoteProperty -Name commandExample -Value $configuration.messageList.commandExample

	return $settingsMsg
}

function GetSettingsCollectionFromConfigFile($configuration)
{
	foreach($solution in $configuration.solutionList.solution)
	{ 
		$solutionNameCollection += $solution
	} 

	foreach($solution in $configuration.sandboxedSolutionList.solution)
	{ 
		$sandboxedSolutionNameCollection += $solution
	} 

	foreach($feature in $configuration.siteFeatureList.feature)
	{ 
		$siteFeatureNameCollection += $feature
	} 	

	foreach($feature in $configuration.webFeatureList.feature)
	{ 
		$webFeatureNameCollection += $feature
	} 

	foreach($feature in $configuration.webApplicationFeatureList.feature)
	{ 
		$webApplicationFeatureNameCollection += $feature
	} 

	$i = 0;
	foreach($folderName in $configuration.listDataFolderNameColection.folderName)
	{ 
		#$listDataCollection += $folderName;
		$listDataCollection = $listDataCollection + $folderName;
		
		#$listDataCollection[$i] = $folderName;
		#$i++;
		#$listDataCollection.Add($folderName)
	} 
	
	Write-Host "Folder count:" $listDataCollection.Count
	
	$settingsColl = New-Object PSObject
	Add-Member -InputObject $settingsColl -MemberType NoteProperty -Name solutionNameCollection -Value $solutionNameCollection
	Add-Member -InputObject $settingsColl -MemberType NoteProperty -Name sandboxedSolutionNameCollection -Value $sandboxedSolutionNameCollection	
	Add-Member -InputObject $settingsColl -MemberType NoteProperty -Name siteFeatureNameCollection -Value $siteFeatureNameCollection
	Add-Member -InputObject $settingsColl -MemberType NoteProperty -Name webFeatureNameCollection -Value $webFeatureNameCollection
	Add-Member -InputObject $settingsColl -MemberType NoteProperty -Name webApplicationFeatureNameCollection -Value $webApplicationFeatureNameCollection
	Add-Member -InputObject $settingsColl -MemberType NoteProperty -Name listDataCollection -Value $listDataCollection
	
	return $settingsColl
}

function GetSettingsFromConfigFile($modeFromComandString, $configFullPath)
{
	if ($configFullPath.Length -eq 0)
	{
		$configFullPath = "$execFolder\$CONFIG_FILE_NAME"
	}

	[xml]$config = Get-Content $configFullPath
	$configuration = $config.configuration

	#todo: check values
	$webUrl = $configuration.destUrl
	#Write-Host "webUrl: $webUrl"
	
	$mode = $configuration.mode
	if ($modeFromComandString.Length -ne 0)
	{
		$mode = $modeFromComandString
	}
	#Write-Host "mode: $mode"

	$settingsCollection = GetSettingsCollectionFromConfigFile $configuration
	$settingsMsg = GetSettingsMsgFromConfigFile $configuration
	
	$settings = New-Object PSObject
	Add-Member -InputObject $settings -MemberType NoteProperty -Name webUrl -Value $webUrl
	Add-Member -InputObject $settings -MemberType NoteProperty -Name mode -Value $mode

	Add-Member -InputObject $settings -MemberType NoteProperty -Name solutionNameCollection -Value $settingsCollection.solutionNameCollection
	Add-Member -InputObject $settings -MemberType NoteProperty -Name sandboxedSolutionNameCollection -Value $settingsCollection.sandboxedSolutionNameCollection
	Add-Member -InputObject $settings -MemberType NoteProperty -Name siteFeatureNameCollection -Value $settingsCollection.siteFeatureNameCollection
	Add-Member -InputObject $settings -MemberType NoteProperty -Name webFeatureNameCollection -Value $settingsCollection.webFeatureNameCollection
	Add-Member -InputObject $settings -MemberType NoteProperty -Name webApplicationFeatureNameCollection -Value $settingsCollection.webApplicationFeatureNameCollection
	Add-Member -InputObject $settings -MemberType NoteProperty -Name listDataCollection -Value $settingsCollection.listDataCollection
	Add-Member -InputObject $settings -MemberType NoteProperty -Name message -Value $settingsMsg
	
	Write-Host $($MSG_HELLO -f $settingsMsg.title)
	Write-Host $MSG_LINE
	
	return $settings
}

function Test()
{
    Write-Host $TEST
}