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

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


        private void frmAdSpecialRegister_Load(object sender, EventArgs e)
        {
            var dbReader = new RetrieveFromDatabase();
            var groupNameCollection = dbReader.ReturnGroupNameList();

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

        private void registerKeyWordButton_Click(object sender, EventArgs e)
        {
            var dbReader = new RetrieveFromDatabase();
            var tracker = new DatabaseTracker();
            var dbWriter = new DatabaseWriter(tracker.DatabaseConnectionString);
            int rowsEffected = 0;

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

            if (rowsEffected == 1)
            {
                var groupNameCollection = dbReader.ReturnGroupNameList();
                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");
            }

        }

    }
}
