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 AddEditPartnerContractForm : BaseForm
    {
        private decimal partnerId;
        private decimal partnerContractId;

        private bool isNewRecord;


        public AddEditPartnerContractForm(decimal partnerId)
        {
            InitializeComponent();

            this.partnerId = partnerId;
            this.isNewRecord = true;

            loadPartnerDataToHeader(partnerId);

            
        }
        
        public AddEditPartnerContractForm(decimal partnerId, decimal partnerContractId)
        {
            InitializeComponent();

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

            loadPartnerDataToHeader(partnerId);
            loadPartnerContractDataToForm(partnerContractId);
        }

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

        private decimal saveNewContract()
        {
            decimal newContractId;

            using (hukpiLINQDataContext linqContext = new hukpiLINQDataContext())
            {
                using (var transaction = new System.Transactions.TransactionScope())
                {
                    Contract contractData = new Contract();
                    contractData.BankAccount = textBoxBankAccountPart1.Text + "-" + textBoxBankAccountPart2.Text + "-" + textBoxBankAccountPart3.Text;
                    contractData.TradeRegister = textBoxTradeRegister.Text;
                    contractData.KSHNumber = textBoxKSHNumber.Text;
                    contractData.KUJNumber = textBoxKUJNumber.Text;
                    contractData.TaxNumber = textBoxTaxNumber.Text;
                    contractData.TaxIdentifier = textBoxTaxIdentifier.Text;
                    contractData.MotherName = textBoxMotherName.Text;
                    contractData.IdentityCardNumber = textBoxIdCardNumber.Text;
                    contractData.TownShipIdentifier = textBoxTownShipID.Text;
                    contractData.MVHNumber = textBoxMVHNumber.Text;
                    contractData.Address = textBoxAddress.Text;

                    linqContext.Contracts.InsertOnSubmit(contractData);
                    linqContext.SubmitChanges();

                    PartnerData partnerData = linqContext.PartnerDatas.Single(p => p.PartnerId == this.partnerId);
                    partnerData.Contract = contractData;
                    
                    linqContext.SubmitChanges();
                    transaction.Complete();

                    newContractId = Convert.ToDecimal(contractData.Contractid);
                }
            }

            return newContractId;
        }

        private decimal saveModifiedContract()
        {
            decimal modifiedContractId;

            using (hukpiLINQDataContext linqContext = new hukpiLINQDataContext())
            {
                using (var transaction = new System.Transactions.TransactionScope())
                {
                    Contract contractData = linqContext.Contracts.Single(p => p.Contractid == this.partnerContractId);
                    contractData.BankAccount = textBoxBankAccountPart1.Text + "-" + textBoxBankAccountPart2.Text + "-" + textBoxBankAccountPart3.Text;
                    contractData.TradeRegister = textBoxTradeRegister.Text;
                    contractData.KSHNumber = textBoxKSHNumber.Text;
                    contractData.KUJNumber = textBoxKUJNumber.Text;
                    contractData.TaxNumber = textBoxTaxNumber.Text;
                    contractData.TaxIdentifier = textBoxTaxIdentifier.Text;
                    contractData.MotherName = textBoxMotherName.Text;
                    contractData.IdentityCardNumber = textBoxIdCardNumber.Text;
                    contractData.TownShipIdentifier = textBoxTownShipID.Text;
                    contractData.MVHNumber = textBoxMVHNumber.Text;
                    contractData.Address = textBoxAddress.Text;

                    linqContext.SubmitChanges();
                    transaction.Complete();

                    modifiedContractId = Convert.ToDecimal(contractData.Contractid);
                }
            }

            return modifiedContractId;
        }

        private void saveContract()
        {
            if (isNewRecord)
            {
                decimal newContractId = saveNewContract();
                MessageBox.Show("Sikeres mentés!");

                ((MainForm)this.MdiParent).openNewMDIChildForm(new PartnerContractForm(this.partnerId, newContractId));
            }
            else
            {
                decimal modifieContractId = saveModifiedContract();
                MessageBox.Show("Sikeres mentés!");
            }

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

        private void saveLayoutAddEditPartnerContract_SaveButtonClick()
        {
            saveContract();
        }

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

        private void loadPartnerDataToHeader(decimal partnerId)
        {
            hukpiDatabaseDataSetTableAdapters.PartnerDataViewTableAdapter PartnerDataAdapter = new hukpiWinApp.hukpiDatabaseDataSetTableAdapters.PartnerDataViewTableAdapter();
            hukpiDatabaseDataSet.PartnerDataViewDataTable partnerData = PartnerDataAdapter.GetDataByPartnerId(partnerId);

            partnerHeaderControl.setUCTitle(partnerData[0]["Name"].ToString());
            partnerHeaderControl.setUCPartnerName(partnerData[0]["Name"].ToString());
            partnerHeaderControl.setUCPartnerNumber(partnerData[0]["PartnerNumber"].ToString());
            partnerHeaderControl.setUCOrderNumber(partnerData[0]["OrderNumber"].ToString());
            partnerHeaderControl.setUCPlace(partnerData[0]["Place"].ToString());
            partnerHeaderControl.setUCContractType(partnerData[0]["ContractTypeName"].ToString());
            partnerHeaderControl.setUCHeaderGroupBoxTitle("Partner adatok");

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

        private void loadPartnerContractDataToForm(decimal partnerContractId)
        {
            hukpiDatabaseDataSetTableAdapters.ContractTableAdapter contracts = new hukpiWinApp.hukpiDatabaseDataSetTableAdapters.ContractTableAdapter();
            hukpiDatabaseDataSet.ContractDataTable contractData = contracts.GetDataByContractId(partnerContractId);

            string[] bankAccParts = contractData[0]["BankAccount"].ToString().Split('-');

            if (bankAccParts.Length > 0)
            {
                textBoxBankAccountPart1.Text = bankAccParts[0];
            }
            if (bankAccParts.Length > 1)
            {
                textBoxBankAccountPart2.Text = bankAccParts[1];
            }
            if (bankAccParts.Length > 2)
            {
                textBoxBankAccountPart3.Text = bankAccParts[2];
            }

            textBoxTradeRegister.Text = contractData[0]["TradeRegister"].ToString();
            textBoxKSHNumber.Text = contractData[0]["KSHNumber"].ToString();
            textBoxKUJNumber.Text = contractData[0]["KUJNumber"].ToString();
            textBoxTaxNumber.Text = contractData[0]["TaxNumber"].ToString();
            textBoxTaxIdentifier.Text = contractData[0]["TaxIdentifier"].ToString();
            textBoxMotherName.Text = contractData[0]["MotherName"].ToString();
            textBoxIdCardNumber.Text = contractData[0]["IdentityCardNumber"].ToString();
            textBoxTownShipID.Text = contractData[0]["TownShipIdentifier"].ToString();
            textBoxMVHNumber.Text = contractData[0]["MVHNumber"].ToString();
            textBoxAddress.Text = contractData[0]["Address"].ToString();

            contractData.Dispose();
            contracts.Dispose();
        }
    }
}
