// ----------------------------------------------------------------------------------
// 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.Views
{
    using System.Collections.ObjectModel;
    using System.Windows.Forms;
    using Microsoft.ManagementConsole;
    using Microsoft.Samples.WindowsAzureMmc.Model.Storage;
    using Microsoft.Samples.WindowsAzureMmc.SnapIn.Forms;

    public class BlobContainerFormView : FormView
    {
        private Forms.BlobContainerControl control = null;
        private AzureManagerSnapIn parentSnapIn = null;

        public BlobContainerFormView()
        {
            parentSnapIn = (AzureManagerSnapIn)this.SnapIn;
        }

        public AsyncStatus OperationStatus { get; private set; }

        protected override void OnInitialize(AsyncStatus status)
        {
            base.OnInitialize(status);
            control = (Forms.BlobContainerControl)this.Control;
            control.SetupView(this);
            control.Dock = DockStyle.Fill;

            this.OperationStatus = status;
            this.OperationStatus.EnableManualCompletion();

            // Set handlers for the controls events
            control.GetBlobForContainer += new Forms.ActionNamedItemDelegate(control_GetBlobForContainer);
            control.UpdateBlobContainer += new Forms.ActionDelegate(control_UpdateBlobContainer);
            control.AddNewContainer += new Forms.ActionDelegate(control_AddNewContainer);
            control.UploadBlob += new Forms.ActionNamedItemDelegate(control_UploadBlob);
            control.UploadContainer += new Forms.ActionDelegate(control_UploadContainer);
            control.DownloadBlobs += new ActionNamedItemWithBlobsDelegate(control_DownloadBlob);
            control.DownloadContainer += new ActionNamedItemDelegate(control_DownloadContainer);
            control.DeleteBlob += new Forms.ActionNamedItemDelegate(control_DeleteBlob);
            control.DeleteContainer += new Forms.ActionNamedItemDelegate(control_DeleteContainer);
            control.ShowRightClick += new Forms.ActionShowRightClickDelegate(_control_ShowRightClick);
            
            if (parentSnapIn != null)
            {
                control.DisplayedBlobContainers = parentSnapIn.GetBlobContainers();
            }
        }

        void _control_ShowRightClick(System.Drawing.Point location)
        {
            this.ShowContextMenu(location, true);
        }

        void control_DeleteContainer(string containerName)
        {
            if (parentSnapIn != null)
            {
                parentSnapIn.DeleteContainer(containerName);
            }
            else
            {
                MessageBox.Show("No connection to server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        void control_DeleteBlob(string blobPath)
        {
            if (parentSnapIn != null)
            {
                parentSnapIn.DeleteBlob(blobPath);
            }
            else
            {
                MessageBox.Show("No connection to server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        void control_UploadBlob(string containerName)
        {
            if (parentSnapIn != null)
            {
                using (UploadBlobForm blobForm = new UploadBlobForm())
                {
                    blobForm.ContainerName = containerName;

                    // Show the blob container add form.
                    if (blobForm.ShowDialog() == DialogResult.OK)
                    {
                        // Upload the new blob to the container
                        parentSnapIn.UploadBlob(containerName, blobForm.BlobName, blobForm.FileName, blobForm.Overwrite);
                    }
                }
            }
            else
            {
                MessageBox.Show("No connection to server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        void control_UploadContainer()
        {
            if (parentSnapIn != null)
            {
                using (UploadContainerForm contForm = new UploadContainerForm())
                {
                    if (contForm.ShowDialog() == DialogResult.OK)
                    {
                        parentSnapIn.UploadContainer(contForm.Path, contForm.ContainerName, contForm.CreatePrivate);
                    }
                }
            }
            else
            {
                MessageBox.Show("No connection to server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void control_DownloadBlob(string containerName, Collection<AzureBlob> blobs)
        {
            if (parentSnapIn != null)
            {
                using (DownloadBlobForm blobForm = new DownloadBlobForm())
                {
                    if (blobForm.ShowDialog() == DialogResult.OK)
                    {
                        parentSnapIn.DownloadBlobs(containerName, blobs, blobForm.Path);
                    }
                }
            }
            else
            {
                MessageBox.Show("No connection to server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void control_DownloadContainer(string containerName)
        {
            if (parentSnapIn != null)
            {
                using (DownloadContainerForm contForm = new DownloadContainerForm())
                {
                    contForm.ContainerName = containerName;
                    if (contForm.ShowDialog() == DialogResult.OK)
                    {
                        parentSnapIn.DownloadContainer(containerName, contForm.Path);
                    }
                }
            }
            else
            {
                MessageBox.Show("No connection to server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void control_AddNewContainer()
        {
            if (parentSnapIn != null)
            {
                using (Forms.BlobContainerForm blobForm = new Forms.BlobContainerForm())
                {
                    // Show the blob container add form.
                    if (blobForm.ShowDialog() == DialogResult.OK)
                    {
                        // Add the new container
                        parentSnapIn.AddBlobContainer(blobForm.ContainerName, blobForm.CreatePrivate);
                    }
                }
            }
            else
            {
                MessageBox.Show("No connection to server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void control_UpdateBlobContainer()
        {
            if (parentSnapIn != null)
            {
                control.DisplayedBlobContainers = parentSnapIn.GetBlobContainers();
            }
            else
            {
                MessageBox.Show("No connection to server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void control_GetBlobForContainer(string path)
        {
            // Set the displayed blobs according to the snapin results
            if (parentSnapIn != null)
            {
                control.DisplayedBlobs = parentSnapIn.GetBlobsFromContainer(path);
            }
            else
            {
                MessageBox.Show("No connection to server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}