$RootPath = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent
$directory_ListContent = $RootPath + "\AnywhereNow\CSV"

function FeedLists 
{
    try 
	{     
        Info "List Content..."
		$ctx_newSite = Get-PnPContext

        # Businesshours
        $listTitle = "Businesshours"
		$listItems = Get-PnPListItem -List $listTitle
		if ($listItems.Count -eq 0) 
		{
			$importFile = "$directory_ListContent\Businesshours.csv"   
			$data = Import-Csv $importFile -Delimiter ";"
			foreach ($row in $data) 
			{
				AddPnPListItem $listTitle @{"wsp_ucc_day" = $row.wsp_ucc_day; "wsp_ucc_Start" = $row.wsp_ucc_Start; "wsp_ucc_End" = $row.wsp_ucc_End}
			}
		}
		else 
		{
            Info "List $listTitle is not empty. No data imported."
        }

        # Version
        $listTitle = "Version"
		$listItems = Get-PnPListItem -List $listTitle
		if ($listItems.Count -eq 0) 
		{
            $importFile = "$directory_ListContent\Version.csv"
			$data = Import-Csv $importFile -Delimiter ";"
            foreach ($row in $data) 
			{
                 AddPnPListItem $listTitle @{"Title" = $row.Title}
            }
        }
		else 
		{
            Info "List $listTitle is not empty. No data imported."
        }
    }
    catch 
	{
        Error "Exception Message: $($_.Exception.Message)"
    }
}

function SetTranslations
{
	switch ($language) 
	{ 
		Danish { AddTranslations $language "da-DK" "Sider" } 
		Dutch { AddTranslations $language "nl-NL" "Paginas" } 
		English { AddTranslations $language "en-US" "Pages" } 
		French { AddTranslations $language "fr-FR" "Pages" } 
		German { AddTranslations $language "de-DE" "Seiten" } 
		Italian { AddTranslations $language "it-IT" "Pagine" }
		Norwegian { AddTranslations $language "nb-NO" "Sider" }
		Portuguese { AddTranslations $language "pt-PT" "Paginas" }
		Spanish { AddTranslations $language "es-ES" "Paginas" }
		Swedish { AddTranslations $language "sv-SE" "Sidor" }
		default { AddTranslations $language "en-US" "Pages" }
	}
}

function AddTranslations($language, $languageCode, $pagesLibraryName)
{
    $ctx_newSite = Get-PnPContext

	if ($languageCode -ne "en-US")
	{
		Warn "Importing $language translations to SharePoint lists..."
	}

	# settingsList
    $listTitle = "settingsList"
    $listItems = Get-PnPListItem -List $listTitle
	if ($listItems.Count -eq 0) 
	{
		$importFile = "$directory_ListContent\settingsList.csv" 
		if ($languageCode -eq "de-DE") 
		{
			$importFile = "$directory_ListContent\$language\settingsList.csv" 
		}

        $data = Import-Csv $importFile -Delimiter ";"
        foreach ($row in $data) 
		{
            AddPnPListItem $listTitle @{"Title" = $row.Title; "wsp_ucc_URL" = $row.wsp_ucc_URL; "wsp_ucc_HeaderTitle" = $row.wsp_ucc_HeaderTitle;
									   "wsp_ucc_sort" = $row.wsp_ucc_sort; "wsp_ucc_visible" = $row.wsp_ucc_visible}
        }
    }
	else 
	{
        Info "List $listTitle is not empty. No data imported."
    }

         
    # SettingValues
    $listTitle = "SettingValues"
    $listItems = Get-PnPListItem -List $listTitle
	if ($listItems.Count -eq 0) 
	{
		$importFile = "$directory_ListContent\$language\SettingValues.csv"
        $data = Import-Csv $importFile -Delimiter ";"
        foreach ($row in $data) 
		{
			AddPnPListItem $listTitle @{"Title" = $row.Title; "wsp_ucc_sv_type" = $row.wsp_ucc_sv_type; "wsp_ucc_sv_value" = $row.wsp_ucc_sv_value;
									   "wsp_ucc_sv_default" = $row.wsp_ucc_sv_default; "wsp_ucc_sv_description" = $row.wsp_ucc_sv_description; "wsp_ucc_sv_scope" = $row.wsp_ucc_sv_scope;
									   "wsp_ucc_sv_settinggroup" = $row.wsp_ucc_sv_settinggroup; "wsp_ucc_sv_readonly" = $row.wsp_ucc_sv_readonly; "wsp_ucc_sv_active" = $row.wsp_ucc_sv_active}
        }
    }
	else 
	{
        Info "List $listTitle is not empty. No data imported."
    }

    # Settings
    $listTitle = "Settings"
    $listItems = Get-PnPListItem -List $listTitle
	if ($listItems.Count -eq 0) 
	{
        $importFile = "$directory_ListContent\$language\Settings.csv" 
        $data = Import-Csv $importFile -Delimiter ";"
        foreach ($row in $data) 
		{
			AddPnPListItem $listTitle @{"wsp_ucc_Key" = $row.wsp_ucc_Key; "wsp_ucc_Value" = $row.wsp_ucc_Value; "wsp_ucc_Description" = $row.wsp_ucc_Description;
									   "wsp_ucc_scope" = $row.wsp_ucc_scope}
        }
    }
	else 
	{
        Info "List $listTitle is not empty. No data imported."
    }

	# CultureInfo
	if ($languageCode -ne "en-US")
	{
		$listTitle = "Settings"
		$item = Get-PnPListItem -List $listTitle -Query '<View><Query><Where><Eq><FieldRef Name="wsp_ucc_Key" /><Value Type="Text">CultureInfo</Value></Eq></Where></Query></View>'
		SetPnPListItem $listTitle $item @{"wsp_ucc_Value" = $languageCode} 
	}

    # IDRQuestions
    $listTitle = "IDRQuestions"
	$listItems = Get-PnPListItem -List $listTitle
	if ($listItems.Count -eq 0) 
	{
        $importFile = "$directory_ListContent\$language\IDRQuestions.csv" 
        $data = Import-Csv $importFile -Delimiter ";"
        foreach ($row in $data) 
		{
			AddPnPListItem $listTitle @{"Title" = $row.Title; "wsp_ucc_idr_question" = $row.wsp_ucc_idr_question; "wsp_ucc_idr_workflow" = $row.wsp_ucc_idr_workflow;
									   "wsp_ucc_idr_queue" = $row.wsp_ucc_idr_queue}
        }
    }
	else 
	{
        Info "List $listTitle is not empty. No data imported."
    }

    # IVRQuestions
    $listTitle = "IVRQuestions"
    $list = GetPnPList $listTitle -Includes
	$listItems = Get-PnPListItem -List $listTitle
	if ($listItems.Count -eq 0) 
	{
        $importFile = "$directory_ListContent\$language\IVRQuestions.csv" 
        $data = Import-Csv $importFile -Delimiter ";"
        foreach ($row in $data) 
		{
			$ct = $list.ContentTypes | Where { $_.Name -eq $row.ContentType }
			AddPnPListItem $listTitle @{"ContentTypeId" = $ct.Id; "Title" = $row.Title; "wsp_ucc_Question" = $row.wsp_ucc_Question; 
									   "wsp_ucc_ivr_choice" = $row.wsp_ucc_ivr_choice; "wsp_ucc_Answer" = $row.wsp_ucc_Answer; "wsp_ucc_ivr_description" = $row.wsp_ucc_ivr_description;
									   "wsp_ucc_ivr_order" = $row.wsp_ucc_ivr_order; "wsp_ucc_ivr_queue" = $row.wsp_ucc_ivr_queue; "wsp_ucc_ivr_Workflow" = $row.wsp_ucc_ivr_Workflow}
        }
    }
	else 
	{
        Info "List $listTitle is not empty. No data imported."
    }
}

function AddDefaultSettings 
{
	Info "Default Settings..."
	
	# Endpoints
	if ($sipDomain)
    {
        $listTitle = "Endpoints"
        $fields = "Id"
        $listItems = Get-PnPListItem -List $listTitle -Fields $fields
        if ($listItems -eq $null) 
		{
            $mainEndpoint = "sip:" + $uccName + "@" + $sipDomain
            $systemEndpoint1 = "sip:" + $uccName + "001" + "@" + $sipDomain
            $systemEndpoint2 = "sip:" + $uccName + "002" + "@" + $sipDomain
            $systemEndpoint3 = "sip:" + $uccName + "003" + "@" + $sipDomain
			AddEndpoint "MainEndpoint" $mainEndpoint
			AddEndpoint "SystemEndpoint" $systemEndpoint1
			AddEndpoint "SystemEndpoint" $systemEndpoint2
			AddEndpoint "SystemEndpoint" $systemEndpoint3
        }
		else 
		{
            Info "Endpoints list is not empty. No Items imported."
        }
    }

    # CDRConnectionString
	if ($cdrConnectionString)
    {
        $item = Get-PnPListItem -List "Settings" -Query "<View><Query><Where><Eq><FieldRef Name=""wsp_ucc_Key"" /><Value Type=""Text"">CDRConnectionString</Value></Eq></Where></Query></View>"
        SetPnPListItem "Settings" $item @{"wsp_ucc_Value" = $cdrConnectionString} 
    }

	# ApplicationId
    $applicationId = "urn:application:" + $uccName
    $item = Get-PnPListItem -List "Settings" -Query "<View><Query><Where><Eq><FieldRef Name=""wsp_ucc_Key"" /><Value Type=""Text"">ApplicationId</Value></Eq></Where></Query></View>"
    SetPnPListItem "Settings" $item @{"wsp_ucc_Value" = $applicationId} 
}

function AddEndpoint($endpointCT, $endpointTitle)
{
	try
	{
		AddPnPListItem "Endpoints" @{"Title" = $endpointTitle; "wsp_ucc_endpoints_type" = $endpointCT} $endpointCT
	}
	catch 
	{
		Error "Error creating endpoint: $endpointTitle"
	}
}

function SetListContent
{
	FeedLists
	SetTranslations
	AddDefaultSettings
}
