using System;
using System.Windows.Forms;

namespace AdvertsingProfitControl
{
    public partial class FrmAdSpecialRegister : Form
    {
        public FrmAdSpecialRegister()
        {
            InitializeComponent();
        }

        private void frmAdSpecialRegister_Load(object sender, EventArgs e)
        {
            var databaseTracker = new DatabaseTracker();
            var databaseReader = new DatabaseReader();
            var groupNameCollection = databaseReader.ReturnGroupNameList(databaseTracker.DatabaseConnectionString);

            adSpecialKeyWordsListBox.SelectedIndexChanged += AdSpecialKeyWordsListBox_SelectedIndexChanged;

            foreach (var groupName in groupNameCollection)
            {
                adSpecialKeyWordsListBox.Items.Add(groupName);
            }
        }

        private void AdSpecialKeyWordsListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            var listBox = (ListBox) sender;
            if (listBox.SelectedIndex == -1)
            {
                DELETE.Enabled = false;
                return;
            }
            DELETE.Enabled = true;
        }

        private void registerKeyWordButton_Click(object sender, EventArgs e)
        {
            var databaseTracker = new DatabaseTracker();
            var databaseReader = new DatabaseReader();
            var databaseWriter = new DatabaseWriter(databaseTracker.DatabaseConnectionString);
            int rowsEffected = 0;

            if (enterNewKeyWordTextBox.Text == "") return;
            rowsEffected = databaseWriter.RedundantlessInsertIntoGroupCategory(enterNewKeyWordTextBox.Text);

            if (rowsEffected == 1)
            {
                RowParsing.AdSpecialGroups.Add(enterNewKeyWordTextBox.Text);
                var groupNameCollection = databaseReader.ReturnGroupNameList(databaseTracker.DatabaseConnectionString);
                adSpecialKeyWordsListBox.Items.Clear();
                foreach (var groupName in groupNameCollection)
                {
                    adSpecialKeyWordsListBox.Items.Add(groupName);
                }
                enterNewKeyWordTextBox.Text = "";
                enterNewKeyWordTextBox.Focus();
            }
            else
            {
                MessageBox.Show(
                    "An error has occurred trying to write " + enterNewKeyWordTextBox.Text + " to the database.",
                    "Unknown Write Error");
            }
        }

        private void DELETE_Click(object sender, EventArgs e)
        {
            var databaseTracker = new DatabaseTracker();
            var databaseReader = new DatabaseReader();
            var databaseWriter = new DatabaseWriter(databaseTracker.DatabaseConnectionString);
            var adSpecial = adSpecialKeyWordsListBox.SelectedItem.ToString();
            var rowsEffected = databaseWriter.RemoveAdSpecialMember(adSpecial);

            if (rowsEffected == 0)
            {
                notificationLabel.Text = "Failed to delete " + adSpecial + " from the database.";
            }
            else if (rowsEffected > 0)
            {
                notificationLabel.Text = "Successfully deleted " + adSpecial + " from the database.";
            }
            adSpecialKeyWordsListBox.Items.Clear();
            var groupNameCollection = databaseReader.ReturnGroupNameList(databaseTracker.DatabaseConnectionString);
            foreach (var groupName in groupNameCollection)
            {
                adSpecialKeyWordsListBox.Items.Add(groupName);
            }
            DELETE.Enabled = false;
        }
    }
}
