// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

namespace Microsoft.Samples.WindowsAzureMmc.SnapIn.Forms
{
    using System;
    using System.ComponentModel;
    using System.IO;
    using System.Windows.Forms;

  /// <summary>
  /// Form used to upload a new blob
  /// </summary>
  public partial class UploadBlobForm : Form
  {
    /// <summary>
    /// Construct the form
    /// </summary>
    public UploadBlobForm()
    {
      InitializeComponent();
    }

    /// <summary>
    /// Let the caller set the container name displayed in the caption
    /// </summary>
    public string ContainerName { get; set; }

    /// <summary>
    /// Expose the file name entered
    /// </summary>
    public string FileName { get; set; }

    /// <summary>
    /// Expose the blob name entered
    /// </summary>
    public string BlobName { get; set; }

    /// <summary>
    /// Expose the overwrite option selected
    /// </summary>
    public bool Overwrite { get; set; }

    /// <summary>
    /// Load the form
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void UploadBlobForm_Load(object sender, EventArgs e)
    {
      this.Text = "Upload BLOB to " + ContainerName;
    }

    /// <summary>
    /// Browse button clicked
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void BrowseButton_Click(object sender, EventArgs e)
    {
      findBlobOFD.FileName = string.Empty;
      if (findBlobOFD.ShowDialog() == DialogResult.OK)
      {
        fileText.Text = findBlobOFD.FileName;
      }
    }

    /// <summary>
    /// Ok button clicked
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void OKButton_Click(object sender, EventArgs e)
    {

      bool validationErrors = false;
      if (string.IsNullOrEmpty(fileText.Text) || !File.Exists(fileText.Text))
      {
        epDlg.SetError(fileText, "Filename must be specified and it must exist");
        validationErrors = true;
      }
      else
      {
        epDlg.SetError(fileText, null);
      }


      if (string.IsNullOrEmpty(blobNameText.Text))
      {
        epDlg.SetError(blobNameText, "Name of the BLOB must be entered");
        validationErrors = true;
      }
      else
      {
        epDlg.SetError(blobNameText, null);
      }

      if (!validationErrors)
      {
        //Set properties to correct values
        FileName = fileText.Text;
        BlobName = blobNameText.Text;
        Overwrite = chkOverwrite.Checked;
        //Set dialog result to ok
        DialogResult = DialogResult.OK;
        this.Close();
      }
    }

    /// <summary>
    /// Update the name of the blob from the file name
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void fileText_TextChanged(object sender, EventArgs e)
    {
      if (string.IsNullOrEmpty(blobNameText.Text) && !string.IsNullOrEmpty(fileText.Text))
      {
        blobNameText.Text = Path.GetFileName(fileText.Text);
      }
    }


  }
}