// ----------------------------------------------------------------------------------
// 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.Linq;
    using System.Windows.Forms;
    using Microsoft.Samples.WindowsAzureMmc.SnapIn.Configuration;

    public partial class SelectConnection : Form
    {
        public SelectConnection()
        {
            InitializeComponent();
        }

        public SelectConnection(ConfigurationData configData, bool deleteMode, Account defaultAccount)
            : this()
        {
            DeleteMode = deleteMode;
            Configuration = configData;
            SelectedAccount = defaultAccount;
        }

        #region properties
        /// <summary>
        /// The configration data supplies the accounts for the drop down list
        /// </summary>
        public ConfigurationData Configuration { get; private set; }
        /// <summary>
        /// The user selected account
        /// </summary>
        public Account SelectedAccount { get; private set; }
        /// <summary>
        /// Change the caption and behaviour if delete mode is set
        /// </summary>
        public bool DeleteMode { get; private set; }
        #endregion

        /// <summary>
        /// Load the form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Connection_Load(object sender, EventArgs e)
        {
            // sort the accounts before binding
            var accs = from acc in Configuration.Accounts
                       orderby acc.AccountName
                       select acc;

            comAccounts.DataSource = Configuration.Accounts;
            // preselect the default account
            if (SelectedAccount != null)
                comAccounts.SelectedItem = SelectedAccount;

            if (DeleteMode)
                this.Text = "Delete Account Connection";
        }

        /// <summary>
        /// Ok button clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OkButton_Click(object sender, EventArgs e)
        {
            // confirm deletion if that's what we are doing, otherwise just get out of here
            if (!DeleteMode || MessageBox.Show("Are you sure you wish to delete this account connection?", "Delete Connection", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                SelectedAccount = (Account)comAccounts.SelectedItem;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        /// <summary>
        /// Display the account details for the selected account
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comAccounts_SelectedIndexChanged(object sender, EventArgs e)
        {
            Account acc = comAccounts.SelectedItem as Account;
            if (acc != null)
            {
                AccountKeyText.Text = acc.AccountKey;
                BlobServiceText.Text = acc.Services["Blobs"].Url;
                QueueServiceText.Text = acc.Services["Queues"].Url;
            }
        }
    }
}