// A sample HTTP client application. S Manoharan.

using System;
using System.Windows.Forms;
using System.Resources;
using System.Globalization;
using System.Threading;
using System.Configuration;
using System.Reflection;
using System.IO;

namespace W3Client
{
   public partial class W3Client : Form
   {
      public W3Client()
      {
         InitializeComponent();

         // Do localization
         try
         {
            string cult = ConfigurationManager.AppSettings["CurrentUICulture"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(cult);
         }
         catch (Exception)
         {
         }

         Text = AssemblyTitle;
         resources = new ResourceManager("W3Client.Strings", typeof(W3Client).Assembly);
         LocalizeResources();

         openURLDialog = new OpenURL();
         clearToolStripMenuItem_Click(this, null);

         method.SelectedIndex = 0;
         httpVersion.SelectedIndex = 0;
      } // W3Client

      protected void LocalizeResources()
      {
         fileToolStripMenuItem.Text = resources.GetString("UI_File");
         clearToolStripMenuItem.Text = resources.GetString("UI_Clear");
         goToolStripMenuItem.Text = resources.GetString("UI_Go");
         exitToolStripMenuItem.Text = resources.GetString("UI_Exit");
         helpToolStripMenuItem.Text = resources.GetString("UI_Help");
         aboutToolStripMenuItem.Text = resources.GetString("UI_About")
               + " " + AssemblyTitle;
         groupBoxRequest.Text = resources.GetString("UI_Request");
         groupBoxResponse.Text = resources.GetString("UI_Response");
      } // LocalizeResources

      public static ResourceManager resources;

      private void exitToolStripMenuItem_Click(object sender, EventArgs e)
      {
         Application.Exit();
      } // exitToolStripMenuItem_Click

      private void clearToolStripMenuItem_Click(object sender, EventArgs e)
      {
         response.Text = "";
         PopulateUri(OpenURL.DEFAULT_URL);
         requestPayload = null;
      } // clearToolStripMenuItem_Click

      private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
      {
         string desc = resources.GetString("STR_AboutDescription");
         string copyright = resources.GetString("STR_Copyright");

         MessageBox.Show(desc + "\n"
            + copyright,
            AssemblyTitle,
            MessageBoxButtons.OK, MessageBoxIcon.Information);
      } // aboutToolStripMenuItem_Click

      private void goToolStripMenuItem_Click(object sender, EventArgs e)
      {
         Cursor = Cursors.WaitCursor;
         string httpVersionString = httpVersion.SelectedItem.ToString();
         string reqMethod = method.SelectedItem.ToString();
         string reqHdrs = requestHeaders.Text;
         var reqHandler = new RequestHandler(uri,
            httpVersionString, reqMethod, reqHdrs, requestPayload);
         reqHandler.DoRequest();
         response.Text = reqHandler.Response;
         responsePayload = reqHandler.ResponsePayload;
         Cursor = Cursors.Default;
      } // goToolStripMenuItem_Click

      private void openToolStripMenuItem_Click(object sender, EventArgs e)
      {
         if (openURLDialog.ShowDialog(this) == DialogResult.OK)
         {
            string uriString = openURLDialog.URL;
            PopulateUri(uriString);
         }
      } // openToolStripMenuItem_Click

      private void PopulateUri(string uriString)
      {
         uri = new Uri(uriString);
         hostName.Text = uri.Host;
         portNumber.Text = uri.Port.ToString(CultureInfo.InvariantCulture);
         resource.Text = uri.PathAndQuery;
         requestHeaders.Text = "Host: " + uri.Host;
      } // PopulateUri

      private string AssemblyTitle
      {
         get
         {
            // Get all Product attributes on this assembly
            object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
            // If there aren't any Product attributes, return an empty string
            if (attributes.Length == 0)
               return "";
            // If there is a Product attribute, return its value
            return ((AssemblyTitleAttribute)attributes[0]).Title;
         }
      } // AssemblyTitle

      private void dataToolStripMenuItem_Click(object sender, EventArgs e)
      {
         openFileDialog.Filter = "All files|*.*";
         openFileDialog.FileName = "";
         if (this.openFileDialog.ShowDialog() == DialogResult.OK)
         {
            string filename = openFileDialog.FileName;
            requestPayload = File.ReadAllBytes(filename);
            //requestHeaders.Text += "\r\nContent-Length: " + requestPayload.Length.ToString();
         }
      } // dataToolStripMenuItem_Click

      private void saveToolStripMenuItem_Click(object sender, EventArgs e)
      {
         if (responsePayload != null)
         {
            if (this.saveFileDialog.ShowDialog() == DialogResult.OK)
            {
               string filename = saveFileDialog.FileName;
               File.WriteAllBytes(filename, responsePayload);
            }
         }
      } // saveToolStripMenuItem_Click

      private Uri uri = null;
      private byte[] responsePayload;
      private byte[] requestPayload;
      private OpenURL openURLDialog;
   } // class
} // namespace