using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using Network;

namespace Windows.UI
{
    [ToolboxBitmap(typeof(HostList), "HostList.HostList.BMP")]
    public partial class HostList : ListView
    {
        #region Events

        public delegate void HostSelected(string HostName);

        [Description("")]
        public event HostSelected OnHostSelected;

        #endregion
        #region .ctor

        public HostList()
        {
            InitializeComponent();
            this.View = View.Details;
            this.StateImageList = imageList;
            this.FullRowSelect = true;
            ColumnHeader column = new ColumnHeader();
            column.Text = "Computer";
            column.Width = 250;
            this.Columns.Add(column);
            this.ItemSelectionChanged += new ListViewItemSelectionChangedEventHandler(HostList_ItemSelectionChanged);
        }

        void HostList_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (!e.IsSelected) { return; }
            if (OnHostSelected != null)
                OnHostSelected(e.Item.Text);
        }

        #endregion

        #region Private members

        private void FillImageList()
        {
            if (imageList.Images.Count > 0) { return; }
            imageList.Images.Add(ResourceImage.Computer);
        }

         private void FillHostList()
        {
            HostEnum servers = null;
            servers = new HostEnum(ResourceScope.RESOURCE_CONTEXT,
                ResourceType.RESOURCETYPE_DISK,
                ResourceUsage.RESOURCEUSAGE_ALL,
                ResourceDisplayType.RESOURCEDISPLAYTYPE_SERVER);
            ListViewItem item;
            foreach (string s in servers)
            {
                item = this.Items.Add(s);
                item.StateImageIndex = 0;
            }
        }

        #endregion

        #region Protected members
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }
        #endregion

        #region Public members

        public void Initialize()
        {
            FillImageList();
            FillHostList();
        }

        public void Initialize(string HostName)
        {
            throw new NotImplementedException("");
            //Initialize();
            //foreach (ListViewItem item in this.Items)
            //{
            //    if (item.Text.ToUpper().Trim() == HostName.ToUpper().Trim())
            //    {
            //        item.Selected = true;
            //    }
            //}
        }


        public new void Refresh()
        {
            this.Clear();
            Initialize();
        }

        public string[] SelectedHosts
        {
            get
            {
                ArrayList list = new ArrayList();
                foreach (ListViewItem item in this.SelectedItems)
                {
                    list.Add(item.Text);
                }
                return (string[])list.ToArray(typeof(string));
            }
        }

        #endregion
    }
}
