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 AddEditUserGroupForm : BaseForm
    {
        decimal groupId;
        bool isNewRecord;

        public AddEditUserGroupForm()
        {
            InitializeComponent();
            this.Text = "Csoport hozzáadása";

            this.checkBoxIsActive.Checked = true;
            this.isNewRecord = true;
        }

        public AddEditUserGroupForm(decimal groupId)
        {
            InitializeComponent();
            this.Text = "Csoport módosítása";

            this.groupId = groupId;
            this.isNewRecord = false;

            loadDataToForm();
        }

        private void loadDataToForm()
        {
            hukpiDatabaseDataSetTableAdapters.GroupsTableAdapter groupsAdapter = new hukpiWinApp.hukpiDatabaseDataSetTableAdapters.GroupsTableAdapter();
            hukpiDatabaseDataSet.GroupsDataTable groupsData = groupsAdapter.GetDataByGroupId(this.groupId);

            textBoxUserGroupName.Text = groupsData[0]["groupName"].ToString();
            textBoxUserGroupDetails.Text = groupsData[0]["details"].ToString();
            checkBoxIsActive.Checked = Utils.isCheckedFromDBDecimal(Convert.ToDecimal(groupsData[0]["isActive"]));

            groupsData.Dispose();
            groupsAdapter.Dispose();
        }

        private decimal saveNewGroup()
        {
            using (hukpiLINQDataContext linqContext = new hukpiLINQDataContext())
            {
                using (var transaction = new System.Transactions.TransactionScope())
                {
                    Group groupData = new Group();

                    groupData.groupName = textBoxUserGroupName.Text;
                    groupData.details = textBoxUserGroupDetails.Text;
                    groupData.isActive = 1;

                    linqContext.Groups.InsertOnSubmit(groupData);
                    linqContext.SubmitChanges();
                    transaction.Complete();

                    return groupData.groupId;
                }
            }
        }

        private decimal saveModifiedGroup()
        {
            using (hukpiLINQDataContext linqContext = new hukpiLINQDataContext())
            {
                using (var transaction = new System.Transactions.TransactionScope())
                {
                    Group groupData = linqContext.Groups.Single(p => p.groupId == this.groupId);

                    groupData.groupName = textBoxUserGroupName.Text;
                    groupData.details = textBoxUserGroupDetails.Text;
                    groupData.isActive = checkBoxIsActive.Checked ? 1 : 0;
                    
                    linqContext.SubmitChanges();
                    transaction.Complete();

                    return groupData.groupId;
                }
            }
        }

        private void saveGroup()
        {
            if (!validateForm())
            {
                MessageBox.Show(ErrorMessages.Default.invalidFormInputData);
                return;
            }
            else
            {
                if (this.isNewRecord)
                {
                    saveNewGroup();
                    MessageBox.Show("Sikeres mentés!");
                }
                else
                {
                    saveModifiedGroup();
                    MessageBox.Show("Sikeres mentés!");
                }

                ((MainForm)this.MdiParent).refreshDataInAllChildForm();
                this.Dispose();
            }
        }

        private void saveLayoutGroup_SaveButtonClick()
        {
            saveGroup();
        }

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

        private void textBoxUserGroupName_Validating(object sender, CancelEventArgs e)
        {
            Utils.validateMustNonEmpty(errorProviderAddEditUser, textBoxUserGroupName);
        }

        private bool validateForm()
        {
            bool isValidUserGroupName = Utils.validateMustNonEmpty(errorProviderAddEditUser, textBoxUserGroupName);

            return isValidUserGroupName;
        }

        
    }
}
