// ----------------------------------------------------------------------------------
// 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.Collections.ObjectModel;
    using System.Globalization;
    using System.Windows.Forms;

    public partial class PendingOperationsControl : UserControl
    {
        private static ListViewColumnSorter.Types[] operationsTypes = new ListViewColumnSorter.Types[] { ListViewColumnSorter.Types.String, ListViewColumnSorter.Types.Date, ListViewColumnSorter.Types.String, ListViewColumnSorter.Types.String };
        private OperationsDictionary displayedOperations;

        public PendingOperationsControl()
        {
            this.InitializeComponent();
            this.operationsListView.ListViewItemSorter = new ListViewColumnSorter();
            this.displayedOperations = new OperationsDictionary();
        }

        public OperationsDictionary DisplayedOperations 
        {
            get { return this.displayedOperations; }
        }

        public void ShowPendingOperations()
        {
            this.operationsListView.Items.Clear();

            foreach (OperationInfo operation in this.displayedOperations.Values)
            {
                ListViewItem li = this.operationsListView.Items.Add(new ListViewItem(new string[] { string.Empty, operation.Time.ToString(), operation.Name, operation.Description }));
                li.ImageKey = operation.Status.ToString().ToUpperInvariant();
                li.Tag = operation;
            }

            this.operationsListView.Refresh();
        }

        private void OperationsListView_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            // Get the sorter that is being used.
            ListViewColumnSorter columnSorter = (ListViewColumnSorter)this.operationsListView.ListViewItemSorter;

            // Determine if clicked column is already the column that is being sorted.
            if (e.Column == columnSorter.SortColumn)
            {
                // Reverse the current sort direction for this column.
                if (columnSorter.Order == SortOrder.Ascending)
                {
                    columnSorter.Order = SortOrder.Descending;
                }
                else
                {
                    columnSorter.Order = SortOrder.Ascending;
                }
            }
            else
            {
                // Set the column number that is to be sorted; default to ascending.
                columnSorter.SortColumn = e.Column;
                columnSorter.Order = SortOrder.Ascending;
                
                // Set the column type for the column that is sorted.
                columnSorter.SortColumnType = operationsTypes[e.Column];
            }

            // Perform the sort with these new sort options.
            this.operationsListView.Sort();
        }
    }
}