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

namespace hukpiWinApp
{
    public partial class BaseEditListViewForm : BaseForm
    {
        protected ListView parentListView;

        public BaseEditListViewForm()
        {
            InitializeComponent();

        }

        public BaseEditListViewForm(ListView parentListView)
        {
            InitializeComponent();

            this.parentListView = parentListView;

            setListViewColumns(this.parentListView.Columns);
            setListViewItems(this.parentListView.Items);
        }

        protected void saveLayout_SaveButtonClick()
        {
            setListViewOnParentForm(parentListView, listViewData.Items);
            this.Dispose();
        }

        protected void saveLayout_CancelButtonClick()
        {
            this.Dispose();
        }

        public void setListViewColumns(ListView.ColumnHeaderCollection columns)
        {
            foreach (ColumnHeader ch in columns)
            {
                this.listViewData.Columns.Add((ColumnHeader)ch.Clone());
            }
        }

        public void setListViewItems(ListView.ListViewItemCollection items)
        {
            foreach (ListViewItem item in items)
            {
                this.listViewData.Items.Add((ListViewItem)item.Clone());
            }
        }

        public void setListViewOnParentForm(ListView listViewOnParent, ListView.ListViewItemCollection newItemList)
        {
            listViewOnParent.Items.Clear();

            foreach (ListViewItem item in newItemList)
            {
                listViewOnParent.Items.Add((ListViewItem)item.Clone());
            }
        }

        private void buttonMoveUp_Click(object sender, EventArgs e)
        {
            if (this.listViewData.SelectedIndices.Count == 0)
            {
                return;
            }
            
            int selectedIndex = this.listViewData.SelectedIndices[0];

            if (selectedIndex == -1 || selectedIndex == 0)
            {
                this.listViewData.Focus();
                return;
            }

            ListViewItem selectedItem = listViewData.SelectedItems[0];

            this.listViewData.Items.RemoveAt(selectedIndex);
            this.listViewData.Items.Insert(selectedIndex - 1, selectedItem);
            this.listViewData.Focus();
            this.listViewData.Items[selectedIndex - 1].Selected = true;
            this.listViewData.Items[selectedIndex - 1].Focused = true;
        }

        private void buttonMoveDown_Click(object sender, EventArgs e)
        {
            if (this.listViewData.SelectedIndices.Count == 0)
            {
                return;
            }

            int selectedIndex = this.listViewData.SelectedIndices[0];

            if (selectedIndex == -1 || selectedIndex == this.listViewData.Items.Count - 1)
            {
                this.listViewData.Focus();
                return;
            }

            ListViewItem selectedItem = listViewData.SelectedItems[0];

            this.listViewData.Items.RemoveAt(selectedIndex);
            this.listViewData.Items.Insert(selectedIndex + 1, selectedItem);
            this.listViewData.Focus();
            this.listViewData.Items[selectedIndex + 1].Selected = true;
            this.listViewData.Items[selectedIndex + 1].Focused = true;
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            if (this.listViewData.SelectedIndices.Count == 0)
            {
                MessageBox.Show("Jelöljön ki egy elemet a törléshez!");
                return;
            }

            int selectedIndex = this.listViewData.SelectedIndices[0];

            if (selectedIndex == -1)
            {
                MessageBox.Show("Jelöljön ki egy elemet a törléshez!");
                return;
            }

            this.listViewData.Items.RemoveAt(selectedIndex);

            if (listViewData.Items.Count == 0)
            {
                return;
            }
            else
            {
                this.listViewData.Focus();
                //this.listViewData.Items[selectedIndex].Selected = true;
                //this.listViewData.Items[selectedIndex].Focused = true;
            }
        }

        

        
    }
}
