Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports System.Xml
Imports System.Data
Imports System.Collections.Generic
Imports system.io

Partial Class Controls_TopBanner
    Inherits System.Web.UI.UserControl

    Protected Sub btnLogout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogout.Click
        Session.Remove("roles")
        Session.RemoveAll()
        Response.Redirect(getUrl() + "/login.aspx")
    End Sub

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

        '#####################################################
        'Added host name in the top banner so user knows whether 
        'they are using dev, uat or prod etc.
        'Part of Issue: 2362
        'NAQIB 10/9/08    
        '#####################################################
        Dim myHost As String = System.Net.Dns.GetHostName
        Dim myIPs As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(myHost)

        Dim sessionVar As Object = Session("user")
        If Not sessionVar Is Nothing Then
            Dim user As DAL.UsersRow = CType(sessionVar, DAL.UsersRow)
            Me.lblSiteHeading.Text = String.Format("Charter.com Administration Tool (Logged in as {0}) | Host Name: {1}", user.UserName, myIPs.HostName())
        End If

        linkHome.NavigateUrl = getUrl() + "/default.aspx"
    End Sub

    Protected Sub btnResetCache_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnResetCache.Click
        Dim WebServerServices As New CharterDotComServices.CharterDotComServices

        Dim obj As Object = Application(ApplicationKey.WebServers)
        Dim ls As List(Of String) = obj
        lblResetCache.Text = String.Empty

        If ls Is Nothing Then
            Dim ServerConfigService As New Service()
            ServerConfigService.GetServerConfig()
        End If
        For Each str As String In ls
            Try
                Dim lURL As String = "http://" & str & "/_SITESETTINGS_WS/CharterDotComServices.asmx"
                WebServerServices.Url = lURL
                'WebServerServices.Url = "http://localhost/uiv3/_SITESETTINGS_WS/CharterDotComServices.asmx"
                If WebServerServices.ResetCacheBy(ConfigurationManager.AppSettings("SiteSettings"), "ALL") Then
                    btnResetCache.BackColor = Drawing.Color.Green
                    lblResetCache.Text += "</BR>" & "Reset Cache on Webserver:" & str & " - succeeded" & "</BR>"
                    'debug purpose
                    'SaveTextToFile("RESET Cache --> " & str, "C:\CHARTERDOTCOM\Cache.txt", "")

                Else
                    btnResetCache.BackColor = Drawing.Color.Red
                    lblResetCache.Text += "</BR>" & "Reset Cache on Web server:" & str & " - failed</BR>"
                End If
            Catch ex As Exception
                lblResetCache.Text += "</BR>" & "Reset Cache on Web server:" & str & " - failed" & ex.Message.ToString() & "</BR>"
            End Try

        Next

    End Sub


    'Naqib- this is a func for debug purpose
    Public Function SaveTextToFile(ByVal strData As String, ByVal FullPath As String, Optional ByVal ErrInfo As String = "") As Boolean

        Dim bAns As Boolean = False
        Dim objStreamWriter As StreamWriter
        Try

            objStreamWriter = File.AppendText(FullPath)
            objStreamWriter.WriteLine(HttpContext.Current.Request.Url.ToString() & " --> " & strData)
            objStreamWriter.Close()

            bAns = True
        Catch Ex As Exception
            ErrInfo = Ex.Message

        End Try
        Return bAns
    End Function


    Public Function getUrl() As String
        Dim url As String
        '#####################################################
        'KEYWORDS. Issue: 2362
        'NAQIB 10/9/08    
        '#####################################################

        Dim machine As String = Environment.MachineName.ToLower.ToString()
        Select Case machine
            Case "kstlintcdce0514"  'naqib's machine
                url = "http://localhost/BL.REMOTINGHOST"
            Case Else
                url = ""
        End Select

        Return url
    End Function
End Class


