Imports System.Collections.Generic
Imports CharterDotCom.CommonEntities.Library
Imports System.Data

Partial Class CharterFileManagement_Default
    Inherits AdminPage

    Private Sub LoadBinaryFileList()

        With fileListBox
            .Items.Clear()
            Dim Files() As String
            Dim fileList As List(Of String) = New List(Of String)
            Files = CMSContentManager.Instance.GetBinaryFiles(ddlBinaryFileType.SelectedValue)
            If Not Files Is Nothing Then
                fileList.AddRange(Files)
            End If
            .DataSource = fileList
            .DataBind()
        End With

    End Sub

    Private Sub UpdateControlLabels(ByVal binaryFileType As String)

        'set the labels appropriately
        fileListLabel.Text = "Existing " & binaryFileType & " Files"
        uploadLabel.Text = "Add New " & binaryFileType & " File"
        deleteButton.Text = "Delete Selected " & binaryFileType & " File"
        uploadButton.Text = "Add New " & binaryFileType & " File"
        lblModifyExistingFile.Text = "Modify Existing " & binaryFileType & " File"
        btnModifyExistingFile.Text = "Modify Existing " & binaryFileType & " File"

    End Sub

    Private Function GetHandlerFileExtension(ByVal binaryFileType As String) As String
        Select Case binaryFileType
            Case "Flash"
                Return ".ccswf"
            Case "XML"
                Return ".ccxml"
            Case "FLV"
                Return ".ccflv"
            Case Else
                Return ""
        End Select
    End Function

    Private Function GetFileExtension(ByVal binaryFileType As String) As String
        Select Case binaryFileType
            Case "Flash"
                Return ".swf"
            Case "XML"
                Return ".xml"
            Case "FLV"
                Return ".flv"
            Case Else
                Return ""
        End Select
    End Function

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

        If Not IsPostBack Then
            LoadBinaryFileList()
            UpdateControlLabels(ddlBinaryFileType.SelectedValue)
        End If

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

        lblViewSelectedFile.Text = ""
    End Sub

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

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

            LoadBinaryFileList()

        Else
            '##################################################
            'THERE IS A PROBLEM WITH FILE SELECTED IN LIST BOX
            '##################################################
            fileListBox.Focus()
            lblStatusMessage.Text = "Please select " & ddlBinaryFileType.SelectedValue & " file 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)


                If Not fileExtension Is Nothing Then
                    If fileExtension.Trim().ToLower().Contains(".flv") Then
                        fileUpload.Focus()
                        lblStatusMessage.Text = "FLV files are not allowed to be uploaded."
                        lblStatusMessage.ForeColor = Drawing.Color.Red
                        Exit Sub
                    End If
                End If

                Dim fileBytes As Byte() = FileUpload.FileBytes

                Dim lContentProviderManager As New ContentProvider.ProviderManager
                lContentProviderManager.AddBinaryFile(1, Now, fileBytes, Nothing, fileName, fileName, fileExtension, ddlBinaryFileType.SelectedValue)

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

                LoadBinaryFileList()
            Else
                '#################################################
                'A PROBLEM: NOTIFY THE USER THAT A FILE WAS NOT UPLOADED.
                '#################################################
                FileUpload.Focus()
                lblStatusMessage.Text = "Please select a local " & ddlBinaryFileType.SelectedValue & " file 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 btnModifyExistingFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnModifyExistingFile.Click
        Try
            If (fuExistingFile.HasFile) Then
                If Not (fileListBox.SelectedValue Is Nothing) And fileListBox.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(fileListBox.SelectedValue)
                    Dim lContentProviderManager As New ContentProvider.ProviderManager
                    Dim fileBytes As Byte() = fuExistingFile.FileBytes

                    lContentProviderManager.UpdateBinaryFile(fileListBox.SelectedValue, GetFileExtension(ddlBinaryFileType.SelectedValue), fileBytes)

                    lblStatusMessage.Text = "Modification of " & fileListBox.SelectedValue & " was successful!"
                    lblStatusMessage.ForeColor = Drawing.Color.Green
                Else
                    '##################################################
                    'THERE IS A PROBLEM WITH FILE SELECTED IN LIST BOX
                    '##################################################
                    lblStatusMessage.Text = "Please select " & ddlBinaryFileType.SelectedValue & " file 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 " & ddlBinaryFileType.SelectedValue & " file 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 fileListBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles fileListBox.SelectedIndexChanged

        Dim lstrHandlerExt As String = GetHandlerFileExtension(ddlBinaryFileType.SelectedValue)

        Select Case Environment.MachineName.ToLower
            Case "kstlmcc2p01"
                '#######
                'PROD WEB
                '#######
                lblViewSelectedFile.Text = "<a href='http://www.Charter.com/Visitors/_CMSImages/" & fileListBox.SelectedValue & lstrHandlerExt & "' target='_blank'>Click Here To View " & ddlBinaryFileType.SelectedValue & " File</a>"
            Case "kstlmcc2p02"
                '#######
                'PROD WEB 
                '#######
                lblViewSelectedFile.Text = "<a href='http://www.Charter.com/Visitors/_CMSImages/" & fileListBox.SelectedValue & lstrHandlerExt & "' target='_blank'>Click Here To View " & ddlBinaryFileType.SelectedValue & " File</a>"
            Case "kstlmweb66"
                '#######
                'UAT WEB 
                '#######
                lblViewSelectedFile.Text = "<a href='http://CC2UATW/Visitors/_CMSImages/" & fileListBox.SelectedValue & lstrHandlerExt & "' target='_blank'>Click Here To View " & ddlBinaryFileType.SelectedValue & " File</a>"
            Case "kstlmweb67"
                '#######
                'UAT WEB 
                '#######
                lblViewSelectedFile.Text = "<a href='http://CC2UATW/Visitors/_CMSImages/" & fileListBox.SelectedValue & lstrHandlerExt & "' target='_blank'>Click Here To View " & ddlBinaryFileType.SelectedValue & " File</a>"
            Case "kstlmcc2p03"
                '#######
                'PROD APP 
                '#######
                lblViewSelectedFile.Text = "<a href='http://www.Charter.com/Visitors/_CMSImages/" & fileListBox.SelectedValue & lstrHandlerExt & "' target='_blank'>Click Here To View " & ddlBinaryFileType.SelectedValue & " File</a>"
            Case "kstlmcc2p04"
                '#######
                'PROD APP 
                '#######
                lblViewSelectedFile.Text = "<a href='http://www.Charter.com/Visitors/_CMSImages/" & fileListBox.SelectedValue & lstrHandlerExt & "' target='_blank'>Click Here To View " & ddlBinaryFileType.SelectedValue & " File</a>"
            Case "kstlmweb68"
                '#######
                'UAT APP 
                '#######
                lblViewSelectedFile.Text = "<a href='http://CC2UATW/Visitors/_CMSImages/" & fileListBox.SelectedValue & lstrHandlerExt & "' target='_blank'>Click Here To View " & ddlBinaryFileType.SelectedValue & " File</a>"
            Case "kstlmweb69"
                '#######
                'UAT APP
                '#######
                lblViewSelectedFile.Text = "<a href='http://CC2UATW/Visitors/_CMSImages/" & fileListBox.SelectedValue & lstrHandlerExt & "' target='_blank'>Click Here To View " & ddlBinaryFileType.SelectedValue & " File</a>"
            Case "kstlmweb65"
                '#######
                'DEV WEB AND APP
                '#######
                'lblViewSelectedFile.Text = "<a href='http://KSTLMWEB65/Visitors/_CMSImages/" & fileListBox.SelectedValue & lstrHandlerExt & "' target='_blank'>Click Here To View " & ddlBinaryFileType.SelectedValue & " File</a>"
                lblViewSelectedFile.Text = "<a href='ViewFile.aspx?file=" & fileListBox.SelectedValue & lstrHandlerExt & "' target='_blank'>Click Here To View " & ddlBinaryFileType.SelectedValue & " File</a>"
            Case Else
                '#####################################
                'THIS SHOULD ONLY BE LOCALHOST STUFF HERE.
                '#####################################
                'lblViewSelectedFile.Text = "<a href='http://LOCALHOST/UIv3/Visitors/_CMSImages/" & fileListBox.SelectedValue & lstrHandlerExt & "' target='_blank'>Click Here To View " & ddlBinaryFileType.SelectedValue & " File</a>"
                lblViewSelectedFile.Text = "<a href='ViewFile.aspx?file=" & fileListBox.SelectedValue & lstrHandlerExt & "' target='_blank'>Click Here To View " & ddlBinaryFileType.SelectedValue & " File</a>"
        End Select

    End Sub

    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
                    lblViewSelectedFile.Text = lblViewSelectedFile.Text = "Reset Cache on Web Server: " & str & " - Succeeded" & "</BR>"
                Else
                    lblViewSelectedFile.Text = lblViewSelectedFile.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

    Protected Sub ddlBinaryFileType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlBinaryFileType.SelectedIndexChanged

        UpdateControlLabels(ddlBinaryFileType.SelectedValue)
        LoadBinaryFileList()

    End Sub

End Class
