<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/master.Master" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<%@ Import Namespace="MKInsight.Online.Core.Security" %>
<%@ Import Namespace="AuditVision.Online.DataServices" %>
<%@ Import Namespace="MKInsight.Online.Shared" %>
<%@ Import Namespace="MKInsight.Online.Login" %>
<%@ Import Namespace="MK.Core" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="AuditVision.Online.DataServices.Compression" %>
<%@ Import Namespace="System.Web.Hosting" %>
<%@ Import Namespace="MK.Core.PasswordEncoding" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
        <style type="text/css">
         span {
            color:white;
        }

        .box
        {
            position: fixed;
            margin-left: -150px;
            margin-top: -150px;
            top: 50%;
            left: 48.1%;
        }
    </style>

     <script runat="server" language="vb">

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

             If Not Page.IsPostBack Then
                 txt_AccountName.Focus()
                 StringTable.Start()
                 Localise()
             End If

         End Sub

         Private Sub Localise()
             Dim st As StringTableDelegate = StringTable.CreateDelegate

             lbl_AcountName.Text = st("AccountName:")
             lbl_AccountPassword.Text = st("AccountPassword_Colon")
             lbl_UploadFile.Text = st("LogoFile")
             lbl_Title.Text = st("LogoUpdatePageTitle")
             lbl_FileInfo.Text = st("LogoFileInfo")
             btn_Update.Text = st("Update")

             RAccount.ErrorMessage = st("InvalidAccountIDcheckagain")
             RPassword.ErrorMessage = st("InvalidPasswordPleaseCheckYourEntryandTryAgain")

         End Sub

         Protected Sub OnUpdateLogo(sender As Object, e As EventArgs) Handles btn_Update.Click
             Try

                 If Not ValidateAccount() Then
                     Helper.AlertMessage(dv_Alert, Helper.AlertType.Warning, StringTable.Text("LogoUpdateValidateAccountMessage"))

                     Return
                 End If

                 If Not ValidateLogoFileName() Then
                     Helper.AlertMessage(dv_Alert, Helper.AlertType.Warning, StringTable.Text("EnterValidLogoFile"))

                     Return
                 End If

                 UpdateOLogoFile()

                 Helper.AlertMessage(dv_Alert, Helper.AlertType.OK, StringTable.Text("LogoFileSuccessfully"))


             Catch ioex As IOException
                 Helper.AlertMessage(dv_Alert, Helper.AlertType.Warning, ioex.ToString())

             Catch uex As UnauthorizedAccessException
                 Helper.AlertMessage(dv_Alert, Helper.AlertType.Warning, uex.ToString())

             Catch ex As Exception
                 Helper.AlertMessage(dv_Alert, Helper.AlertType.Warning, ex.ToString())
             End Try
         End Sub

         Private Sub UpdateOLogoFile()

             Try
                 Dim folder As String = ServerConfig.FindResourcePath("logoPathRoot", subFolder:="overrides\images")

                 Dim fullPath As String = Path.Combine(folder, txt_AccountName.Text)

                 If Not Directory.Exists(fullPath) Then Directory.CreateDirectory(fullPath)

                 u_File.SaveAs(Path.Combine(fullPath, New FileInfo(u_File.PostedFile.FileName).Name))

             Catch uex As UnauthorizedAccessException
                 Throw
             Catch ex As Exception
                 Throw
             End Try

         End Sub

         Private Function ValidateAccount() As Boolean

             Dim hashPassword As String = CreateSHA512(txt_AccountPassword.Text)

             Dim configObj As ServerConfig = ServerConfig.Instance

             If IsNothing(configObj.Accounts.Item(txt_AccountName.Text)) Then Return False

             If TestSHA512(configObj.Accounts.Item(txt_AccountName.Text).PasswordHash, hashPassword) Then Return False

             Return True
         End Function

         Private Function ValidateLogoFileName() As Boolean
             Dim fInfo As FileInfo = Nothing

             Try

                 If Not u_File.HasFile Then Return False

                 Dim fileName As String = u_File.PostedFile.FileName
                 fInfo = New FileInfo(fileName)

                 If Not fInfo.Name.Equals("logo.png") Then
                     Helper.AlertMessage(dv_Alert, Helper.AlertType.Warning, StringTable.Text("EnterValidLogoFile"))
                     Return False
                 End If

                 Dim size As Double = u_File.PostedFile.ContentLength / 1024

                 If size > 50 Then
                     Helper.AlertMessage(dv_Alert, Helper.AlertType.Warning, StringTable.Text("EnterValidLogoFile"))
                     Return False
                 End If

             Catch uex As UnauthorizedAccessException
                 Throw

             Catch ex As Exception
                 Throw
             End Try

             'Note:Check image dimensions
             Try
                 Using b As New Bitmap(u_File.PostedFile.InputStream)
                     If b.Width > 400 OrElse b.Height > 200 Then
                         Helper.AlertMessage(dv_Alert, Helper.AlertType.Warning, StringTable.Text("EnterValidLogoFile"))
                     End If
                 End Using
             Catch ex As Exception
                 Throw
             End Try

             Return True
         End Function

         Private Function Encode(buffer As Byte()) As String
             Dim hexPairs As String = ""

             For Each b As Byte In buffer
                 hexPairs += Convert.ToString(b, 16).PadLeft(2, "0"c)
             Next

             Return hexPairs.ToUpperInvariant()

         End Function

         Private Function MapRelativePath(relativePath As String) As String
             Return Path.Combine(HostingEnvironment.ApplicationPhysicalPath, relativePath.Replace("/"c, "\"c).Trim("\"c))

         End Function
     </script>

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="cm" runat="server">
 
       <div role="alert" id="dv_Alert" runat="server" style="margin-top: 5px;">
    </div>

    <div style="padding-left:20px;">

    
    <div>
        <h2>
            <asp:Label ID="lbl_Title" runat="server"></asp:Label></h2>
    </div>
    <br />

    <table>
        <tr>
            <td>
                <asp:Label ID="lbl_AcountName" runat="server"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_AccountName" runat="server" Width="300px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lbl_AccountPassword" runat="server"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_AccountPassword" runat="server" TextMode="Password" Width="300px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lbl_UploadFile" runat="server"></asp:Label>
            </td>
            <td>
                <asp:FileUpload runat="server" ID="u_File" Width="300px" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <br />

                <asp:Label ID="lbl_FileInfo" runat="server" Font-Italic="True"  ForeColor="black" Font-Bold="true"></asp:Label>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <hr />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="btn_Update" runat="server" />
            </td>
        </tr>
    </table>

    <cc1:ValidatorCalloutExtender ID="v_Account" runat="server" TargetControlID="RAccount">
    </cc1:ValidatorCalloutExtender>
    <asp:RequiredFieldValidator ID="RAccount" runat="server" ControlToValidate="txt_AccountName"
        Display="None" SetFocusOnError="True"></asp:RequiredFieldValidator>

    <cc1:ValidatorCalloutExtender ID="v_Password" runat="server" TargetControlID="RPassword">
    </cc1:ValidatorCalloutExtender>
    <asp:RequiredFieldValidator ID="RPassword" runat="server" ControlToValidate="txt_AccountPassword"
        Display="None" SetFocusOnError="True"></asp:RequiredFieldValidator>
        </div>
</asp:Content>
