﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestIp.aspx.cs" %>
<%@ Import Namespace="Sitecore.CES.GeoIp.Core.Lookups" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="Newtonsoft.Json" %>
<%@ Import Namespace="Sitecore.CES.Client" %>
<%@ Import Namespace="Sitecore.CES.Configuration" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
    <script runat="server">
        private static string _url = "https://discovery-ces.cloud.sitecore.net/connectionstrings";

        private static string _nexusHeader = new AuthenticationHeader().GetAuthenticationHeaderValue();
        private static string _apiVersion = Settings.ScSApiVersion;

        protected void Button1_Click(object sender, EventArgs e)
        {
            var w = LookupManager.GetWhoIsInformationByIp(TextBox1.Text);
            ListBox1.Items.Clear();
            H2.Visible = false;
            ListBox1.Items.Add("IP: " + TextBox1.Text);
            ListBox1.Items.Add("AreaCode: " + w.AreaCode);
            ListBox1.Items.Add("BusinessName: " + w.BusinessName);
            ListBox1.Items.Add("City: " + w.City);
            ListBox1.Items.Add("Country: " + w.Country);
            ListBox1.Items.Add("Dns: " + w.Dns);
            ListBox1.Items.Add("Isp: " + w.Isp);
            ListBox1.Items.Add("Latitude: " + w.Latitude);
            ListBox1.Items.Add("Longitude: " + w.Longitude);
            ListBox1.Items.Add("MetroCode: " + w.MetroCode);
            ListBox1.Items.Add("PostalCode: " + w.PostalCode);
            ListBox1.Items.Add("Region: " + w.Region);
            ListBox1.Items.Add("Url: " + w.Url);

            if (w.AreaCode == "N/A" & w.BusinessName == "N/A" & w.City =="N/A" & w.Country =="N/A" & w.Dns == "N/A" & w.Isp == "N/A" & w.Latitude.ToString() == "" & w.Longitude.ToString() == "" & w.MetroCode == "N/A" & w.PostalCode == "N/A" & w.Region == "N/A" & w.Url == "N/A")
            {
                H2.Visible = true;
            }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            TextBox2.Text = string.Empty;
            H3.Visible = false;
            var request = (HttpWebRequest)WebRequest.Create(_url);
            request.Headers["X-ScS-Nexus-Auth"] = _nexusHeader;
            request.Headers["X-ScS-Api-Version"] = _apiVersion;

            using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
            {
                using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
                {

                    var result = streamReader.ReadToEnd();
                    TextBox2.Text = ("Response status code: " + response.StatusCode + "\nResponse text: " + result);
                }
                if (response.StatusCode.ToString() != "OK")
                {
                    H3.Visible = true;
                }
            }


        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            ListBox1.Items.Clear();
            TextBox2.Text = string.Empty;
            H2.Visible = false;
            H3.Visible = false;
        }
    </script>
<body>
    <form id="form1" runat="server">
    <div>
        <h4 ID="H1" runat="server">Test IP address</h4>
        <br />
        <asp:TextBox ID="TextBox1" runat="server" Width="229px"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="GetInformartionByIP" Width="234px" />
    
        <br />
        <asp:ListBox ID="ListBox1" runat="server" Height="314px" Width="557px"></asp:ListBox>
        <h4 ID="H2" runat="server" visible="false" style="color:orangered" >The IP address is not resolved. Please check whether the service is activated for your current license</h4>
    
    </div>
        <div>
    
        <h4 ID="Text" runat="server" Width="229px">Test Discovery Connection</h4>
        <br />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Test Discovery Connection" Width="234px" />
    
        <br />
        <asp:textbox ID="TextBox2" runat="server" Height="200px" Width="557px" TextMode="MultiLine"> </asp:textbox>

        <h4 ID="H3" runat="server" visible="false" style="color:orangered" >Cannot access to discovery. Please check your firewall according to the following article: <a href="https://doc.sitecore.com/xp/en/developers/100/sitecore-experience-manager/set-up-sitecore-ip-geolocation.html#configure-a-firewall">Set up Sitecore IP Geolocation</a></h4>
       
    
    </div>
         <div>
            <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Refresh" Width="234px" />
        </div>
    </form>
</body>
</html>
