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.Admin
{
    public partial class RolesForm : Form
    {
        List<decimal> needsToDeleteGrantedModuls;

        public RolesForm()
        {
            InitializeComponent();

            this.labelTitle.Text = "Csoportok jogosultságainak beállítása";
            fillComboBoxGroups();
        }

        private void showEditRolesForm()
        {
            if ((int)this.comboBoxGroup.SelectedValue == 0)
            {
                MessageBox.Show("Kérem előbb válasszon csoportot!");
                return;
            }

            BaseEditListViewWithTwoPanelForm editRoles = new BaseEditListViewWithTwoPanelForm(Utils.getModulesForListView(), listViewGrantedModules);
            editRoles.Text = Utils.getGroupName(Convert.ToDecimal(this.comboBoxGroup.SelectedValue)) + " csoport jogainak beállítása";
            editRoles.ShowDialog();
            this.Focus();
        }

        private void fillComboBoxGroups()
        {
            comboBoxGroup.DataSource = Utils.getUserGroupsForCombo("Válassz!", false);
            comboBoxGroup.DisplayMember = "Description";
            comboBoxGroup.ValueMember = "Id";

        }

        private void fillGrantedModulesListView(decimal groupId)
        {
            listViewGrantedModules.Items.Clear();
            needsToDeleteGrantedModuls = new List<decimal>();

            if (groupId == 0)
            {
                return;
            }
            else
            {
                needsToDeleteGrantedModuls.Clear();
                
                foreach (ListViewItem item in Utils.getGrantedModulesForListView(groupId))
                {
                    listViewGrantedModules.Items.Add(item);
                    needsToDeleteGrantedModuls.Add(Convert.ToDecimal(item.Tag));
                }
            }
        }

        private void comboBoxGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.IsHandleCreated)
            {
                fillGrantedModulesListView(Convert.ToDecimal(comboBoxGroup.SelectedValue));
            }
        }

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

        private bool saveGrantedModules()
        {
            using (hukpiLINQDataContext linqContext = new hukpiLINQDataContext())
            {
                using (var transaction = new System.Transactions.TransactionScope())
                {
                    GrantedModule grantedModule;

                    foreach (ListViewItem item in listViewGrantedModules.Items)
                    {
                        if (item.Tag == null || "".Equals(item.Tag.ToString().Trim()))
                        {
                            grantedModule = new GrantedModule();
                            grantedModule.groupId = Convert.ToDecimal(this.comboBoxGroup.SelectedValue);
                            grantedModule.ProgramModulesId = Convert.ToDecimal(item.SubItems[1].Tag);
                            linqContext.GrantedModules.InsertOnSubmit(grantedModule);
                        }
                        else
                        {
                            needsToDeleteGrantedModuls.Remove(Convert.ToDecimal(item.Tag));

                            grantedModule = linqContext.GrantedModules.Single(p => p.grantedModulesId == Convert.ToDecimal(item.Tag));
                            grantedModule.groupId = Convert.ToDecimal(this.comboBoxGroup.SelectedValue);
                            grantedModule.ProgramModulesId = Convert.ToDecimal(item.SubItems[1].Tag);
                        }

                        linqContext.SubmitChanges();
                    }

                    foreach (decimal needsToDeleteItem in needsToDeleteGrantedModuls)
                    {
                        grantedModule = linqContext.GrantedModules.Single(p => p.grantedModulesId == needsToDeleteItem);
                        linqContext.GrantedModules.DeleteOnSubmit(grantedModule);
                        linqContext.SubmitChanges();
                    }

                    transaction.Complete();

                }
            }
            return true;
        }

        private void saveLayout_SaveButtonClick()
        {
            if (saveGrantedModules())
            {
                MessageBox.Show("Sikeres mentés!");
            }
            this.Dispose();
        }

        private void listViewGrantedModules_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            showEditRolesForm();
        }

        private void listViewGrantedModules_MouseClick(object sender, MouseEventArgs e)
        {
            showEditRolesForm();
        }
    }
}
