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
{
    public partial class AddEditPartnerForm : BaseForm
    {
        private bool isNewRecord = false;

        //private decimal partnerId;
        private decimal regionId;
        private decimal contractTypeId;

        public AddEditPartnerForm()
        {
            InitializeComponent();

            this.isNewRecord = true;

            this.Text = "Új partner felvitele";

            fillComboBoxRegion();
            fillComboBoxContractTypes();
        }

        public AddEditPartnerForm(decimal partnerId)
        {
            InitializeComponent();

            this.isNewRecord = false;
            this.partnerId = partnerId;

            this.Text = "Partner módosítása";

            hukpiDatabaseDataSetTableAdapters.PartnerDataViewTableAdapter PartnerDataAdapter = new hukpiWinApp.hukpiDatabaseDataSetTableAdapters.PartnerDataViewTableAdapter();
            hukpiDatabaseDataSet.PartnerDataViewDataTable partnerData = PartnerDataAdapter.GetDataByPartnerId(Convert.ToDecimal(partnerId));

            this.partnerId = Convert.ToDecimal(Convert.IsDBNull(partnerData[0]["PartnerId"]) ? 0 : partnerData[0]["PartnerId"]);
            this.regionId = Convert.ToDecimal(Convert.IsDBNull(partnerData[0]["RegionsId"]) ? 0 : partnerData[0]["RegionsId"]);
            this.contractTypeId = Convert.ToDecimal(Convert.IsDBNull(partnerData[0]["ContractTypesId"]) ? 0 : partnerData[0]["ContractTypesId"]);

            this.Text = Convert.ToString(partnerData[0]["PartnerNumber"]) + " - " + Convert.ToString(partnerData[0]["Name"]);

            fillComboBoxRegion();
            fillComboBoxContractTypes();

            loadPartnerDataToForm(partnerData);

            partnerData.Dispose();
            PartnerDataAdapter.Dispose();
        }

        /// <summary>
        ///     A megadott partnerData DataTable adatait betölti a PartnerForm-ba
        /// </summary>
        /// <param name="partnerData"></param>
        private void loadPartnerDataToForm(hukpiDatabaseDataSet.PartnerDataViewDataTable partnerData)
        {
            textBoxPartnerName.Text = partnerData[0]["Name"].ToString();
            textBoxPartnerNumber.Text = partnerData[0]["PartnerNumber"].ToString();
            textBoxOrderNumber.Text = partnerData[0]["OrderNumber"].ToString();
            textBoxPlace.Text = partnerData[0]["Place"].ToString();
            textBoxZipCode.Text = partnerData[0]["ZipCode"].ToString();
            textBoxAddress.Text = partnerData[0]["Address"].ToString();
            textBoxLetterAddress.Text = partnerData[0]["LetterAddress"].ToString();
            textBoxOwner.Text = partnerData[0]["Owner"].ToString();

            comboBoxContractType.SelectedValue = Utils.getIntValueFromDB(partnerData[0]["ContractTypesId"]);
            comboBoxRegions.SelectedValue = Utils.getIntValueFromDB(partnerData[0]["RegionsId"]);
            

        }

        private void fillComboBoxRegion()
        {
            comboBoxRegions.DataSource = Utils.getRegionsForCombo();
            comboBoxRegions.DisplayMember = "Description";
            comboBoxRegions.ValueMember = "Id";

        }

        private void fillComboBoxContractTypes()
        {
            comboBoxContractType.DataSource = Utils.getContractTypesForCombo();
            comboBoxContractType.DisplayMember = "Description";
            comboBoxContractType.ValueMember = "Id";

        }

       
        private void AddEditPartnerForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'hukpiDatabaseDataSet.ContractTypes' table. You can move, or remove it, as needed.
            this.contractTypesTableAdapter.Fill(this.hukpiDatabaseDataSet.ContractTypes);

        }

        private void AddEditPartnerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Utils.isCancelAddEditFormClose(this))
            {
                e.Cancel = true;
            }
        }

        private void savePartner()
        {
            if (this.isNewRecord)
            {
                decimal newPartnerId = saveNewPartner();
                MessageBox.Show("Sikeres mentés!");
                ((MainForm)this.MdiParent).openNewMDIChildForm(new PartnerForm(newPartnerId));
            }
            else
            {
                saveModifiedPartner();
                MessageBox.Show("Sikeres mentés!");
            }

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

        private decimal saveModifiedPartner()
        {
            decimal modifiedPartnerId;

            using (hukpiLINQDataContext linqContext = new hukpiLINQDataContext())
            {
                using (var transaction = new System.Transactions.TransactionScope())
                {

                    PartnerData partnerData = linqContext.PartnerDatas.Single(p => p.PartnerId == this.partnerId);

                    partnerData.Name = textBoxPartnerName.Text;
                    partnerData.PartnerNumber = textBoxPartnerNumber.Text;
                    partnerData.OrderNumber = textBoxOrderNumber.Text;
                    partnerData.Place = textBoxPlace.Text;
                    partnerData.ZipCode = textBoxZipCode.Text;
                    partnerData.Address = textBoxAddress.Text;
                    partnerData.LetterAddress = textBoxLetterAddress.Text;
                    partnerData.Owner = textBoxOwner.Text;
                    partnerData.StatusFlag = 'A';

                    if (!dateTimePickerValidStart.Checked)
                    {
                        partnerData.ValidStart = null;
                    }
                    else
                    {
                        partnerData.ValidStart = Convert.ToDateTime(dateTimePickerValidStart.Text);
                    }

                    if (!dateTimePickerValidEnd.Checked)
                    {
                        partnerData.ValidEnd = null;
                    }
                    else
                    {
                        partnerData.ValidEnd = Convert.ToDateTime(dateTimePickerValidEnd.Text);
                    }

                    if ((int)comboBoxContractType.SelectedValue == 0)
                    {
                        partnerData.ContractTypesId = null;
                    }
                    else
                    {
                        partnerData.ContractTypesId = Convert.ToDecimal(comboBoxContractType.SelectedValue);
                    
                    }

                    if ((int)comboBoxRegions.SelectedValue == 0)
                    {
                        partnerData.RegionsId = null;
                    }
                    else
                    {
                        partnerData.RegionsId = Convert.ToDecimal(comboBoxRegions.SelectedValue);

                    }

                    linqContext.SubmitChanges();
                    transaction.Complete();
                    modifiedPartnerId = Convert.ToDecimal(partnerData.PartnerId);
                }
            }
            return modifiedPartnerId;
        }

        private decimal saveNewPartner()
        {
            decimal newPartnerId;

            using (hukpiLINQDataContext linqContext = new hukpiLINQDataContext())
            {
                using (var transaction = new System.Transactions.TransactionScope())
                {
                    
                    PartnerData partnerData = new PartnerData();

                    partnerData.Name = textBoxPartnerName.Text;
                    partnerData.PartnerNumber = textBoxPartnerNumber.Text;
                    partnerData.OrderNumber = textBoxOrderNumber.Text;
                    partnerData.Place = textBoxPlace.Text;
                    partnerData.ZipCode = textBoxZipCode.Text;
                    partnerData.Address = textBoxAddress.Text;
                    partnerData.LetterAddress = textBoxLetterAddress.Text;
                    partnerData.Owner = textBoxOwner.Text;
                    partnerData.StatusFlag = 'A';

                    if (!dateTimePickerValidStart.Checked)
                    {
                        partnerData.ValidStart = null;
                    }
                    else
                    {
                        partnerData.ValidStart = Convert.ToDateTime(dateTimePickerValidStart.Text);
                    }

                    if (!dateTimePickerValidEnd.Checked)
                    {
                        partnerData.ValidEnd = null;
                    }
                    else
                    {
                        partnerData.ValidEnd = Convert.ToDateTime(dateTimePickerValidEnd.Text);
                    }

                    if ((int)comboBoxContractType.SelectedValue == 0)
                    {
                        partnerData.ContractTypesId = null;
                    }
                    else
                    {
                        partnerData.ContractTypesId = Convert.ToDecimal(comboBoxContractType.SelectedValue);

                    }

                    if ((int)comboBoxRegions.SelectedValue == 0)
                    {
                        partnerData.RegionsId = null;
                    }
                    else
                    {
                        partnerData.RegionsId = Convert.ToDecimal(comboBoxRegions.SelectedValue);

                    }

                    linqContext.PartnerDatas.InsertOnSubmit(partnerData);
                    linqContext.SubmitChanges();
                    transaction.Complete();

                    newPartnerId = Convert.ToDecimal(partnerData.PartnerId);
                }
            }
            return newPartnerId;
        }

        private void saveLayoutPartner_SaveButtonClick()
        {
            savePartner();
        }

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