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 AppointmentsUi.Views
{
    public partial class SalutationsList : Form
    {
        public SalutationsList()
        {
            InitializeComponent();

            var salutations = AppointmentsLib.Models.Salutation.GetSalutations();

            for (int i = 0; i < salutations.Count(); i++)
            {
                var salutation = salutations.ElementAt(i);

                var labelSalutationId = new Label() { Text = salutation.SalutationId.ToString(), AutoSize = true, TextAlign = ContentAlignment.MiddleCenter };
                var textboxSalutationLabel = new TextBox() { Text = salutation.Label, AutoSize = true };
                var buttonApplyChanges = new Button() { Text = "Apply" };
                var buttonDelete = new Button() { Text = "Delete" };

                buttonApplyChanges.Click += new EventHandler(delegate (object sender, EventArgs e) {
                    salutation.Save();
                    Program.mainForm.EmbedForm<SalutationsList>();
                });
                buttonDelete.Click += new EventHandler(delegate (object sender, EventArgs e) {
                    var res = MessageBox.Show($"Are you sure to delete the salutation\n{salutation.Label}\n?", "Delete salutation?", MessageBoxButtons.YesNo);
                    if (res == DialogResult.Yes)
                    {
                        salutation.Delete();
                        Program.mainForm.EmbedForm<SalutationsList>();
                    }
                });
                textboxSalutationLabel.TextChanged += new EventHandler(delegate (object sender, EventArgs e) {
                    salutation.Label = textboxSalutationLabel.Text;
                });

                tableLayoutPanelSalutations.Controls.Add(labelSalutationId, 0, 1 + i);
                tableLayoutPanelSalutations.Controls.Add(textboxSalutationLabel, 1, 1 + i);
                tableLayoutPanelSalutations.Controls.Add(buttonApplyChanges, 2, 1 + i);
                tableLayoutPanelSalutations.Controls.Add(buttonDelete, 3, 1 + i);
            }
        }

        internal void addSalutation()
        {
            AppointmentsLib.Models.Salutation.Create("New salutation");
            Program.mainForm.EmbedForm<SalutationsList>();
        }

        private void buttonAddNewSalutation_Click(object sender, EventArgs e)
        {
            addSalutation();
        }
    }
}