using System;
using System.Linq;
using System.Windows.Forms;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Report;
using BeWo.Report.ReportObjects;

using BS.Shared.Core;
using BS.Shared.Extensions;

namespace BeWo.Fortis
{
    public partial class Klientenstammblatt : DevExpress.XtraReports.UI.XtraReport, IBeWoReport<CustomerDataRO>
    {
        public Klientenstammblatt()
        {
            InitializeComponent();
        }

        public void SetReportDataSource(CustomerDataRO pRO)
        {
            var c = DAOFactory.GenericDAO.LoadByID<Customer>(pRO.CustomerOid);

            foreach (var faufau in c.VarFieldValueList)
            {
                var varFieldValue = Utils.XMLDeserializeFromString(faufau.SerializedValue, Type.GetType(faufau.TypeName));
                if (varFieldValue != null)
                {
                    var vValue = varFieldValue.ToString();

                    switch (faufau.VarFieldDefOid)
                    {
                        case 32:
                            geburtsortTableCell.Text = vValue;
                            break;
                        case 33:
                            geburtsnameTableCell.Text = vValue;
                            break;
                        case 34:
                            staatsangehoerigkeitsTableCell.Text = vValue;
                            break;
                        case 35:
                            arbeitsplatzTableCell.Text = vValue;
                            break;
                        case 36:
                            aufnahmedatumTableCell.Text = ((DateTime) varFieldValue).ToShortDateString();
                            break;
                        case 37:
                            letzterWohnsitzTableCell.Text = vValue;
                            break;
                        case 38:
                            hbgTableCell.Text = vValue;
                            break;
                        case 39:
                            behindertCB.Checked = (bool) varFieldValue;
                            break;
                        case 40:
                            GDBTableCell.Text = vValue + "%";
                            break;
                        case 41:
                            behindertenAGueltigBisTableCell.Text = vValue;
                            break;
                        case 42:
                            befrZuzhlgBisTableCell.Text = ((DateTime)varFieldValue).ToShortDateString();
                            break;
                        case 43:
                            rentenTableCell.Text = vValue + " €";
                            break;
                        case 44:
                            aktualisiertAmTableCell.Text = ((DateTime)varFieldValue).ToShortDateString();
                            break;
                        case 46:
                            wkGesundheitCB.Checked = (bool) varFieldValue;
                            break;
                        case 47:
                            wkVermoegenCB.Checked = (bool) varFieldValue;
                            break;
                        case 48:
                            wkEinwilligungsvorbehaltCB.Checked = (bool) varFieldValue;
                            break;
                        case 50:
                            wkSonstigeCB.Checked = true;
                            sonstigeWirkungskreiseTableCell.Text = vValue;
                            break;
                        case 51:
                            ansteckendeKrankheitenTableCell.Text = vValue;
                            break;
                        case 52:
                            unvertraeglichkeitenTableCell.Text = vValue;
                            break;
                        case 53:
                            letzterKlinikaufenthaltTableCell.Text = ((DateTime)varFieldValue).ToShortDateString();
                            break;
                        case 54:
                            suizidVersucheTableCell.Text = vValue;
                            break;
                        case 55:
                            krisenanzeichenTableCell.Text = vValue;
                            break;
                        case 56:
                            krisenabsprachenTableCell.Text = vValue;
                            break;
                        case 57:
                            krisenSonstigesTableCell.Text = vValue;
                            break;
                        case 58:
                            kontoinhaberTableCell.Text = vValue;
                            break;
                        case 59:
                            bankTableCell.Text = vValue;
                            break;
                        case 60:
                            ibanTableCell.Text = vValue;
                            break;
                        case 61:
                            bicTableCell.Text = vValue;
                            break;
                        case 62:
                            einkommensTableCell.Text = vValue + " €";
                            break;
                    }
                }
            }

			var x = pRO.EnvironmentPersons.OrderByDescending(i => i.Customer2PersonOid).FirstOrDefault(f => !String.IsNullOrEmpty(f.RelationRole.TypeDescription) && f.RelationRole.TypeDescription.Equals("Hausärzt/in"));
			var y = pRO.EnvironmentPersons.OrderByDescending(i => i.Customer2PersonOid).FirstOrDefault(f => !String.IsNullOrEmpty(f.RelationRole.TypeDescription) && f.RelationRole.TypeDescription.Equals("Fachärzt/in"));

            if (x != null)
            {
                var xt = !string.IsNullOrEmpty(x.Person.Title) ? x.Person.Title + " " : "";
                hausarztTableCell.Text = xt + x.Person.FullName;
                hausarztAdresseTableCell.Text = x.Person.AddressString;
            }

            if (y != null)
            {
                var yt = !string.IsNullOrEmpty(y.Person.Title) ? y.Person.Title + " " : "";
                facharztTableCell.Text = yt + y.Person.FullName;
                facharztAdresseTableCell.Text = y.Person.AddressString;
            }

			var b = pRO.EnvironmentPersons.FirstOrDefault(f => !String.IsNullOrEmpty(f.RelationRole.TypeDescription) && f.RelationRole.TypeDescription.Equals("Gesetzlicher Betreuer"));
            if (b != null)
            {
                var bt = !string.IsNullOrEmpty(b.Person.Title) ? b.Person.Title + " " : "";
                gesetzBetrTableCell.Text = bt + b.Person.FullName;
                gesetzBetrAdresseTableCell.Text = b.Person.AddressString;
            }

			var k = pRO.EnvironmentOrganisations.FirstOrDefault(f => !String.IsNullOrEmpty(f.RelationRole.TypeDescription) && f.RelationRole.TypeDescription.Equals("Krankenkasse"));
            if (k != null)
            {
                //var fod = pRO.CostBearers.FirstOrDefault(f => f.CostBearer.OrganisationOid.Equals(k.Organisation.OrganisationOid));
	            //var test = fod.ReferenceNumber;

                kvNRTableCell.Text = k.Customer2OrganisationNotice;
                krankenkasseTableCell.Text = k.Organisation.Name;
            }

            if(c.TerminationDate.HasValue)
                xrTableCell100.Text = c.TerminationDate.Value.ToShortDateString();

            
			
			
			var bp = pRO.RelatedEmployees.FirstOrDefault(f => f.RelationRole.TypeDescription.Equals("Erste Bezugsperson"));
			if (bp != null)
			{
				var emp = DAOFactory.GenericDAO.LoadByID<Employee>(bp.Employee.EmployeeOid);
                bezugspersonTableCell.Text = emp.Person.FirstNameLastName;
            }

			bp = pRO.RelatedEmployees.FirstOrDefault(f => f.RelationRole.TypeDescription.Equals("Zweite Bezugsperson"));
            if (bp != null)
            {
				var emp = DAOFactory.GenericDAO.LoadByID<Employee>(bp.Employee.EmployeeOid);
				vertretungTableCell.Text = emp.Person.FirstNameLastName;
            }



            diagnosenTableCell.Text = c.Diagnosis2CustomerList.Select(s => s.ICD10DiagnosisCode).ToSeparatedString(", ");
            aktuelleMedikationTableCell1.Text = c.Medication;

            var kontaktPersonen = pRO.EnvironmentPersons.Where(w => w.RelationRole.TypeDescription.Equals("Kontaktperson")).ToArray();

            for (var i = 0; i < kontaktPersonen.Length; i++)
            {
                var t = string.IsNullOrEmpty(kontaktPersonen[i].Person.Title) ? "" : kontaktPersonen[i].Person.Title + " ";
                var fn = t + kontaktPersonen[i].Person.FullName;
                var addr = kontaktPersonen[i].Person.AddressString;

                if (i == 0)
                {
                    kontakt1ATableCell.Text = fn;
                    kontakt1BTableCell.Text = addr;
                }
                else if (i == 1)
                {
                    kontakt2ATableCell.Text = fn;
                    kontakt2BTableCell.Text = addr;
                }
                else if (i == 2)
                {
                    kontakt3ATableCell.Text = fn;
                    kontakt3BTableCell.Text = addr;
                }
                else if (i == 3)
                {
                    kontakt4ATableCell.Text = fn;
                    kontakt4ATableCell.Text += Environment.NewLine + addr;
                }
                else
                {
                    kontakt4ATableCell.Text += Environment.NewLine + fn;
                    kontakt4ATableCell.Text += Environment.NewLine + addr;
                }
            }

            var cb = pRO.CostBearers.FirstOrDefault();
            if (cb != null)
            {
                leistungstraegerTableCell.Text = cb.CostBearer.Name;
                aktenzeichenTableCell.Text = cb.ReferenceNumber;
            }

            bindingSource1.DataSource = pRO;
        }
    }
}
