Imports System.Collections.Generic
Imports CharterDotCom.CommonEntities.Library
Imports System.Data
Imports System.Drawing
Imports System.Web.UI.WebControls.Image

Partial Class CharterImageManagement_Default
    Inherits AdminPage


    Private Sub LoadImageList()

        With imageListBox
            .Items.Clear()
            Dim Images() As String
            Dim imageList As List(Of String) = Nothing
            Images = CMSContentManager.Instance.GetImages()
            If Not Images Is Nothing Then
                imageList = New List(Of String)
                imageList.AddRange(Images)
            End If
            imageList.Sort()
            .DataSource = imageList
            .DataBind()
        End With



    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            LoadImageList()
        End If

        '##############
        'CLEAR VALUES
        '##############
        lblStatusMessage.Text = ""
        lblStatusMessage.ForeColor = Drawing.Color.Black
        lblViewSelectedImage.Text = ""

        '#################################################### 
        'If no items in search list. do not display it
        'NAQIB 10/9/08    
        '#################################################### 
        If imageSearchListBox.Items.Count <= 0 Then
            imageSearchListBox.Visible = False
        End If

    End Sub

    Protected Sub deleteButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles deleteButton.Click
        If Not (imageListBox.SelectedValue Is Nothing) And imageListBox.SelectedValue.Trim() <> "" Then
            Dim lContentProviderManager As New ContentProvider.ProviderManager
            lContentProviderManager.DeleteImageByImageName(imageListBox.SelectedValue, Nothing)

            lblStatusMessage.Text = "Deletion of " & imageListBox.SelectedValue & " was successful!"
            lblStatusMessage.ForeColor = Drawing.Color.Green

            LoadImageList()

        Else
            '##################################################
            'THERE IS A PROBLEM WITH FILE SELECTED IN LIST BOX
            '##################################################
            imageListBox.Focus()
            lblStatusMessage.Text = "Please select image in list box to continue"
            lblStatusMessage.ForeColor = Drawing.Color.Red
        End If
    End Sub

    Protected Sub uploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uploadButton.Click
        Try
            If FileUpload.HasFile Then
                Dim fileName As String = FileUpload.FileName.Trim()
                Dim fileExtension As String

                Dim lastDotPosition As Integer = fileName.LastIndexOf(".")
                fileExtension = fileName.Substring(lastDotPosition)
                fileName = fileName.Substring(0, lastDotPosition)
                Dim fileBytes As Byte() = FileUpload.FileBytes


                Dim lContentProviderManager As New ContentProvider.ProviderManager
                lContentProviderManager.AddImage(1, Now, fileBytes, Nothing, fileName, fileName)

                lblStatusMessage.Text = "Addition of file named '" & fileName & "' was successful!"
                lblStatusMessage.ForeColor = Drawing.Color.Green

                LoadImageList()
            Else
                '#################################################
                'A PROBLEM: NOTIFY THE USER THAT A FILE WAS NOT UPLOADED.
                '#################################################
                FileUpload.Focus()
                lblStatusMessage.Text = "Please select a local image by clicking the Browse button"
                lblStatusMessage.ForeColor = Drawing.Color.Red
            End If


        Catch ex As Exception
            lblStatusMessage.Text = ex.Message
            lblStatusMessage.ForeColor = Drawing.Color.Red
        End Try
    End Sub

    Protected Sub btnModifyExistingImage_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnModifyExistingImage.Click
        Try
            If (fuExistingFile.HasFile) Then
                If Not (imageListBox.SelectedValue Is Nothing) And imageListBox.SelectedValue.Trim() <> "" Then
                    '###################################################################################
                    'MODIFY THE FILES BY UPLOADING THEM TO THE DATABASE AND REPLACING THE EXISTING FILE
                    '###################################################################################
                    Dim lImage As CMS.IMAGE = CMSContentManager.Instance.GetImage(imageListBox.SelectedValue)
                    Dim lContentProviderManager As New ContentProvider.ProviderManager
                    Dim fileBytes As Byte() = fuExistingFile.FileBytes

                    lContentProviderManager.UpdateImage(imageListBox.SelectedValue, fileBytes)


                    lblStatusMessage.Text = "Modification of " & imageListBox.SelectedValue & " was successful!"
                    lblStatusMessage.ForeColor = Drawing.Color.Green
                Else
                    '##################################################
                    'THERE IS A PROBLEM WITH FILE SELECTED IN LIST BOX
                    '##################################################
                    lblStatusMessage.Text = "Please select image in list box to continue"
                    lblStatusMessage.ForeColor = Drawing.Color.Red
                End If
            Else
                '#################################################
                'A PROBLEM: NOTIFY THE USER THAT A FILE WAS NOT UPLOADED.
                '#################################################
                fuExistingFile.Focus()
                lblStatusMessage.Text = "Please select a local image by clicking the Browse button"
                lblStatusMessage.ForeColor = Drawing.Color.Red
            End If
        Catch ex As Exception
            lblStatusMessage.Text = ex.Message
            lblStatusMessage.ForeColor = Drawing.Color.Red
        End Try


    End Sub

    '#####################################################
    'THIS IS USED FOR THE SEARCH CAPABILITY BASED ON 
    'KEYWORDS. Issue: 2362
    'NAQIB  
    '#####################################################
    Protected Sub btnDelSearchImage_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelSearchImage.Click
        If Not (imageSearchListBox.SelectedValue Is Nothing) And imageSearchListBox.SelectedValue.Trim() <> "" Then
            Dim lContentProviderManager As New ContentProvider.ProviderManager
            lContentProviderManager.DeleteImageByImageName(imageSearchListBox.SelectedValue, Nothing)

            lblStatusMessage.Text = "Deletion of " & imageSearchListBox.SelectedValue & " was successful!"
            lblStatusMessage.ForeColor = Drawing.Color.Green

            ListImages(searchText.Text)
            imageSearchListBox.Focus()
            imageSearchListBox.ClearSelection()

            imageView.Visible = False
            imageFileName.Text = String.Empty

            LoadImageList()

        End If
    End Sub

    '#####################################################
    'THIS IS USED FOR THE SEARCH CAPABILITY BASED ON 
    'KEYWORDS. Issue: 2362
    'NAQIB 10/9/08    
    '#####################################################
    Protected Sub imageSearchListBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles imageSearchListBox.SelectedIndexChanged

        Dim url As String = getImageUrl(Environment.MachineName.ToLower.ToString(), imageSearchListBox)
        lblViewSelectedImage.Text = "<a href='" & url & "' target='_blank'>View this image in a new window</a><br />"

        imageView.ImageUrl = url
        imageFileName.Text = imageSearchListBox.SelectedValue.ToString()
        imageView.Visible = True
        btnDelSearchImage.Visible = True

        imageListBox.ClearSelection()
    End Sub

    Protected Sub imageListBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles imageListBox.SelectedIndexChanged
        '#####################################################
        'Issue: 2362
        'NAQIB 10/9/08    
        '#####################################################
        Dim url As String = getImageUrl(Environment.MachineName.ToLower.ToString(), imageListBox)
        lblViewSelectedImage.Text = "<a href='" & url & "' target='_blank'>View this image in a new window</a><br />"
        imageView.ImageUrl = url
        imageFileName.Text = imageListBox.SelectedValue.ToString()
        imageView.Visible = True
        imageSearchListBox.ClearSelection()

    End Sub

    Public Function getImageUrl(ByVal machine As String, ByVal imageListBox As ListBox) As String
        Dim url As String
        '#####################################################
        'THIS IS USED FOR THE SEARCH CAPABILITY BASED ON 
        'KEYWORDS. Issue: 2362
        'NAQIB 10/9/08    
        '#####################################################

        Select Case machine
            Case "kstlmcc2p01"
                '#######
                'PROD WEB
                '#######
                url = "http://www.Charter.com/Visitors/_CMSImages/" & imageListBox.SelectedValue & ".ccom"
            Case "kstlmcc2p02"
                '#######
                'PROD WEB 
                '#######
                url = "http://www.Charter.com/Visitors/_CMSImages/" & imageListBox.SelectedValue & ".ccom"
            Case "kstlmweb66"
                '#######
                'UAT WEB 
                '#######
                url = "http://CC2UATW/Visitors/_CMSImages/" & imageListBox.SelectedValue & ".ccom"
            Case "kstlmweb67"
                '#######
                'UAT WEB 
                '#######
                url = "http://CC2UATW/Visitors/_CMSImages/" & imageListBox.SelectedValue & ".ccom"
            Case "kstlmcc2p03"
                '#######
                'PROD APP 
                '#######
                url = "http://www.Charter.com/Visitors/_CMSImages/" & imageListBox.SelectedValue & ".ccom"
            Case "kstlmcc2p04"
                '#######
                'PROD APP 
                '#######
                url = "http://www.Charter.com/Visitors/_CMSImages/" & imageListBox.SelectedValue & ".ccom"
            Case "kstlmweb68"
                '#######
                'UAT APP 
                '#######
                url = "http://CC2UATW/Visitors/_CMSImages/" & imageListBox.SelectedValue & ".ccom"
            Case "kstlmweb69"
                '#######
                'UAT APP
                '#######                
                url = "http://CC2UATW/Visitors/_CMSImages/" & imageListBox.SelectedValue & ".ccom"
            Case "kstlmweb65"
                '#######
                'DEV WEB AND APP
                '#######                
                url = "http://KSTLMWEB65/Visitors/_CMSImages/" & imageListBox.SelectedValue & ".ccom"
            Case "kstlintcdce0514"
                '#######
                'DEV WEB AND APP <naqib>
                '#######                
                url = "http://kstlintcdce0514/UI/Visitors/_CMSImages/" & imageListBox.SelectedValue & ".ccom"
            Case Else
                '#####################################
                'THIS SHOULD ONLY BE LOCALHOST STUFF HERE.
                '#####################################                
                url = "http://LOCALHOST/UI/Visitors/_CMSImages/" & imageListBox.SelectedValue & ".ccom"
        End Select

        Return url
    End Function

    'Protected Sub btnSiteSettings_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSiteSettings.Click
    '    Dim lConfigProvider As New Service
    '    Dim setting As String = ConfigurationManager.AppSettings("ConfigLocation")
    '    Dim dt As DataTable = lConfigProvider.GetProviderSettings(setting)
    '    Dim ds As New Data.DataSet

    '    Try
    '        ds = lConfigProvider.GetWebConfig(setting)
    '        For Each row As Data.DataRow In ds.Tables(0).Rows
    '            Application.Lock()
    '            Application.Remove(row.Item("Key"))
    '            Application.Add(row.Item("Key"), row.Item("XMLData"))
    '            Application.UnLock()
    '        Next
    '        If dt IsNot Nothing Then
    '            Application.Lock()
    '            For Each dr As DataRow In dt.Rows
    '                Application.Remove(dr.Item(0))
    '                Application.Add(dr.Item(0), dr.Item(1))
    '            Next
    '            Application.UnLock()
    '        End If





    '        Dim WebServerServices As New CharterDotComServices.CharterDotComServices
    '        Dim obj As Object = Application(ApplicationKey.WebServers)
    '        Dim ls As List(Of String) = obj
    '        If ls Is Nothing Then
    '            Dim ServerConfigService As New Service()
    '            ServerConfigService.GetServerConfig()
    '        End If
    '        For Each str As String In ls
    '            Dim lURL As String = "http://" & str & "/_SITESETTINGS_WS/CharterDotComServices.asmx"
    '            WebServerServices.Url = lURL
    '            'WebServerServices.Url = "http://localhost/ui/_SITESETTINGS_WS/CharterDotComServices.asmx"


    '            If WebServerServices.ChangeSiteSettings(ConfigurationManager.AppSettings("SiteSettings")) Then
    '                lblViewSelectedImage.Text = lblViewSelectedImage.Text = "Reset Cache on Webserver:" & str & " - succeeded" & "</BR>"
    '            Else
    '                lblViewSelectedImage.Text = lblViewSelectedImage.Text = "Reset Cache on Web server:" & str & " - failed"
    '            End If
    '        Next



    '    Catch ex As Exception
    '        lblStatusMessage.Text = ex.Message
    '        lblStatusMessage.ForeColor = Drawing.Color.Red
    '    End Try
    'End Sub


    '#####################################################
    'THIS IS USED FOR THE SEARCH CAPABILITY BASED ON 
    'KEYWORDS. Issue: 2362
    'NAQIB 10/9/08    
    '#####################################################
    Protected Sub SearchButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchButton.Click
        ListImages(searchText.Text)
    End Sub

    '#####################################################
    'THIS IS USED FOR THE SEARCH CAPABILITY BASED ON 
    'KEYWORDS. Issue: 2362
    'NAQIB 10/9/08    
    '#####################################################
    Public Sub ListImages(ByVal searchText As String)

        With imageSearchListBox
            .Items.Clear()
            Dim Images() As String
            Dim imageList As List(Of String) = Nothing

            Images = CMSContentManager.Instance.GetImagesByKeyword(searchText)


            'Images = CMSContentManager.Instance.GetImages()
            If Not Images Is Nothing Then
                imageList = New List(Of String)
                imageList.AddRange(Images)
            End If
            .DataSource = imageList
            .DataBind()
        End With

        If imageSearchListBox.Items.Count <= 0 Then
            imageSearchListBox.Items.Add("Not found!")
            btnDelSearchImage.Visible = False
        End If

        imageSearchListBox.Visible = True

    End Sub
End Class
