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.Forms
{
    public partial class BaseEditListViewWithTwoPanelForm : BaseForm
    {
        protected ListView parentListView;
        protected List<ListViewItem> sourceItems;
        protected List<string> ignoredItems;
        
        public BaseEditListViewWithTwoPanelForm()
        {
            InitializeComponent();
        }

        public BaseEditListViewWithTwoPanelForm(List<ListViewItem> sourceItems, ListView parentListView)
        {
            InitializeComponent();

            this.parentListView = parentListView;
            this.sourceItems = sourceItems;

            setListViewColumns(this.listViewData ,this.parentListView.Columns);
            setListViewColumns(this.listViewSource, this.parentListView.Columns);
            ignoredItems = setListViewItemsAndGetAddedItemsText(this.listViewData, this.parentListView.Items);

            if (ignoredItems == null)
            {
                ignoredItems = new List<string>();
            }

            setListViewItemsExcludedWithIgnoredItems(this.listViewSource, sourceItems, this.ignoredItems);
        }

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

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

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

        public List<string> setListViewItemsAndGetAddedItemsText(ListView listView, ListView.ListViewItemCollection items)
        {
            List<string> addedItemsText = new List<string>();

            foreach (ListViewItem item in items)
            {
                listView.Items.Add((ListViewItem)item.Clone());
                addedItemsText.Add(item.Text);
            }

            if (addedItemsText.Count == 0)
            {
                return null;
            }
            else
            {
                return addedItemsText;
            }
        }

        public void setListViewItemsExcludedWithIgnoredItems(ListView listView, List<ListViewItem> items, List<string> ignoredItems)
        {
            foreach (ListViewItem item in items)
            {
                if (ignoredItems != null && ignoredItems.Contains(item.Text))
                {
                    continue;
                }
                else
                {
                    listView.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;
            }
        }

        private void moveOneToDestination()
        {
            if (listViewSource.SelectedItems.Count == 0)
            {
                return;
            }

            ListViewItem item = listViewSource.SelectedItems[0];
            int selectedIndex = listViewSource.SelectedIndices[0];
            item.Remove();
            listViewData.Items.Add(item);
            if (selectedIndex < listViewSource.Items.Count)
            {
                listViewSource.Items[selectedIndex].Selected = true;
            }
            else if (listViewSource.Items.Count > 0)
            {
                listViewSource.Items[selectedIndex - 1].Selected = true;
            }
            listViewSource.Focus();

            this.ignoredItems.Add(item.Text);
        }

        private void buttonMoveOneToDestination_Click(object sender, EventArgs e)
        {
            moveOneToDestination();
        }

        private void moveBackOne()
        {
            if (listViewData.SelectedItems.Count == 0)
            {
                return;
            }

            ListViewItem item = listViewData.SelectedItems[0];
            int selectedIndex = listViewData.SelectedIndices[0];
            item.Remove();

            if (selectedIndex < listViewData.Items.Count)
            {
                listViewData.Items[selectedIndex].Selected = true;
            }
            else if (listViewData.Items.Count > 0)
            {
                listViewData.Items[selectedIndex - 1].Selected = true;
            }
            listViewData.Focus();

            this.ignoredItems.Remove(item.Text);

            this.listViewSource.Items.Clear();
            setListViewItemsExcludedWithIgnoredItems(listViewSource, sourceItems, ignoredItems);
        }

        private void buttonMoveBackOne_Click(object sender, EventArgs e)
        {
            moveBackOne();
        }

        private void moveAllToDestination()
        {
            foreach (ListViewItem item in listViewSource.Items)
            {
                item.Remove();
                listViewData.Items.Add(item);
                this.ignoredItems.Add(item.Text);
            }
        }

        private void buttonMoveAllToDestination_Click(object sender, EventArgs e)
        {
            moveAllToDestination();
        }

        private void moveBackAll()
        {
            foreach (ListViewItem item in listViewData.Items)
            {
                item.Remove();
                this.ignoredItems.Remove(item.Text);
            }

            this.listViewSource.Items.Clear();
            setListViewItemsExcludedWithIgnoredItems(listViewSource, sourceItems, ignoredItems);
        }

        private void buttonMoveBackAll_Click(object sender, EventArgs e)
        {
            moveBackAll();
        }

        public void setLeftUpsideLabel(string text)
        {
            this.labelLeftUpside.Text = text;
        }

        public void setRightUpsideLabel(string text)
        {
            this.labelRightUpside.Text = text;
        }
        

     
    }
}
