using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;

namespace AdvertsingProfitControl
{
    public partial class FrmManageAdItems : Form
    {
        public FrmManageAdItems()
        {
            InitializeComponent();
            MaximumSize = new Size(550, 505);
            MinimumSize = new Size(525, 480);
        }

        private void frmManageAdItems_Load(object sender, EventArgs e)
        {
            FillAdItemComboBoxFilter();
            adItemFilterComboBox.SelectedIndexChanged += UpdateFiltering;
            adItemListView.SelectedIndexChanged += OnListItemSelectionChanged;
            adItemTextBox.TextChanged += NewAdItemTextChanged;
        }

        private void NewAdItemTextChanged(object sender, EventArgs e)
        {
            if(adItemTextBox.TextLength == 0)
            {
                addItemButton.Enabled = false;
                return;
            }
            else
            {
                addItemButton.Enabled = true;
            }
        }

        private void OnListItemSelectionChanged(object sender, EventArgs e)
        {
            if(adItemListView.SelectedItems.Count > 0)
            {
                deleteSelectedItem.Enabled = true;
            }
            else
            {
                deleteSelectedItem.Enabled = false;
            }
        }

        private void UpdateFiltering(object sender, EventArgs e)
        {
            var databaseTracker = new DatabaseTracker();
            var databaseReader = new DatabaseReader();
            var itemList = new List<string>();
            adItemListView.Items.Clear();
            if(adItemFilterComboBox.Text == "All Items")
            {
                itemList = databaseReader.RetrieveAdItemListByLetter(databaseTracker.DatabaseConnectionString);
            }
            else
            {
                itemList = databaseReader.RetrieveAdItemListByLetter(databaseTracker.DatabaseConnectionString, adItemFilterComboBox.Text);
            }
            foreach (var item in itemList)
            {
                var listViewItem = new ListViewItem(item);
                adItemListView.Items.Add(listViewItem);
            }
        }

        private void deleteSelectedItem_Click(object sender, EventArgs e)
        {
            if(adItemListView.SelectedItems.Count == 0)
            {
                return;
            }
            var databaseTracker = new DatabaseTracker();
            var databaseWriter = new DatabaseWriter(databaseTracker.DatabaseConnectionString);

            if (databaseWriter.RemoveAdItem(adItemListView.SelectedItems[0].Text))
            {
                notificationLabel.Text = "Successfully removed " + adItemListView.SelectedItems[0].Text + "\n from the database.";
                adItemListView.Items.Remove(adItemListView.SelectedItems[0]);
            }

            deleteSelectedItem.Enabled = false;
        }

        private void addItemButton_Click(object sender, EventArgs e)
        {
            var databaseTracker = new DatabaseTracker();
            var databaseWriter = new DatabaseWriter(databaseTracker.DatabaseConnectionString);
            if (adItemTextBox.Text == "") return;
            TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
            //Clean up the input string.
            //Pretty up the ad item's name by uppercasing the name.
            var adItemText = adItemTextBox.Text;
            adItemText = textInfo.ToTitleCase(adItemText);
            //Eliminate the upper cased pound abbreviation ("Lb") with a standard "lb".
            adItemText = adItemText.Replace("Lb", "lb");
            adItemText = adItemText.Replace("LB", "lb");
            adItemText = adItemText.Replace("lB", "lb");
            adItemText = adItemText.Replace(":", "");
            adItemText = adItemText.Replace(";", "");
            adItemText = adItemText.Replace("/", "");
            adItemText = adItemText.Replace("\\", "");

            if (databaseWriter.AddNewItem(adItemText))
            {
                notificationLabel.Text = "Successfully inserted " + adItemText + "\n into the database.";
                if (adItemFilterComboBox.SelectedIndex == 0 || adItemFilterComboBox.SelectedItem.ToString()[0] == adItemText[0])
                {
                    adItemListView.Items.Add(adItemText);
                }
                if (!adItemFilterComboBox.Items.Contains(adItemText[0]))
                {
                    adItemFilterComboBox.Items.Add(adItemText[0]);
                }
            }
            addItemLabel.Select();
            adItemTextBox.Text = "";
            addItemButton.Enabled = false;
        }

        private void FillAdItemComboBoxFilter()
        {
            var databaseTracker = new DatabaseTracker();
            var databaseReader = new DatabaseReader();
            var columnHeader = new ColumnHeader();
            columnHeader.Text = "Ad Items";
            columnHeader.Width = 300;
            adItemListView.Columns.Add(columnHeader);
            var itemList = databaseReader.RetrieveAdItemListByLetter(databaseTracker.DatabaseConnectionString);
            foreach (var item in itemList)
            {
                var listViewItem = new ListViewItem(item);
                if (!adItemFilterComboBox.Items.Contains(item[0]))
                {
                    adItemFilterComboBox.Items.Add(item[0]);
                }
                adItemListView.Items.Add(listViewItem);
            }
            adItemFilterComboBox.Items.Insert(0, "All Items");
            columnHeader.Width = -1;
            adItemFilterComboBox.SelectedIndex = 0;
        }

        private void closeFormFileMainMenu_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
