using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Linq;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Report;
using BS.Shared;
using BS.Shared.Core;
using DevExpress.XtraReports.UI;
using BeWo.Report.ReportObjects;

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

        public void SetReportDataSource(CustomerDataRO pRO)
        {
			SetVarFieldValues(pRO);
	        SetRelationFields(pRO);
            this.bindingSource1.DataSource = pRO;
        }

		private void SetRelationFields(CustomerDataRO pRO)
		{
			var x = pRO.EnvironmentPersons.FirstOrDefault(f => f.RelationRole.TypeDescription.ToLower().Equals("hausarzt"));
			
			if (x != null)
			{
				lblHausarztName.Text = x.Person.TitleFirstNameLastNameToString();
				lblHausarztStr.Text = x.Person.Street;
				lblHausarztOrt.Text = String.Format("{0} {1}", x.Person.PostalCode, x.Person.Town);
				lblHausarztTel.Text = x.Person.Communication1;
				lblHausarztFax.Text = x.Person.Communication4;
			}

			x = pRO.EnvironmentPersons.FirstOrDefault(f => f.RelationRole.TypeDescription.ToLower().Equals("facharzt (psychiater)"));

			if (x != null)
			{
				lblPsychaterName.Text = x.Person.TitleFirstNameLastNameToString();
				lblPsychaterStr.Text = x.Person.Street;
				lblPsychaterOrt.Text = String.Format("{0} {1}", x.Person.PostalCode, x.Person.Town);
				lblPsychaterTel.Text = x.Person.Communication1;
				lblPsychaterFax.Text = x.Person.Communication4;
			}

			x = pRO.EnvironmentPersons.FirstOrDefault(f => f.RelationRole.TypeDescription.ToLower().Equals("zahnarzt"));

			if (x != null)
			{
				lblZahnarztName.Text = x.Person.TitleFirstNameLastNameToString();
				lblZahnarztStr.Text = x.Person.Street;
				lblZahnarztOrt.Text = String.Format("{0} {1}", x.Person.PostalCode, x.Person.Town);
				lblZahnarztTel.Text = x.Person.Communication1;
				lblZahnarztFax.Text = x.Person.Communication4;
			}

			x = pRO.EnvironmentPersons.FirstOrDefault(f => f.RelationRole.TypeDescription.ToLower().Equals("gynäkologe")) ??
				pRO.EnvironmentPersons.FirstOrDefault(f => f.RelationRole.TypeDescription.ToLower().Equals("urologe"));
			if (x != null)
			{
				lblGynName.Text = x.Person.TitleFirstNameLastNameToString();
				lblGynStr.Text = x.Person.Street;
				lblGynOrt.Text = String.Format("{0} {1}", x.Person.PostalCode, x.Person.Town);
				lblGynTel.Text = x.Person.Communication1;
				lblGynFax.Text = x.Person.Communication4;
			}

			x = pRO.EnvironmentPersons.FirstOrDefault(f => f.RelationRole.TypeDescription.ToLower().Equals("gesetzliche betreuung"));

			if (x != null)
			{
				lblGesetzBetreuerName.Text = x.Person.TitleFirstNameLastNameToString();
				lblGesetzBetreuerStr.Text = x.Person.Street;
				lblGesetzBetreuerOrt.Text = String.Format("{0} {1}", x.Person.PostalCode, x.Person.Town);
				lblGesetzBetreuerTel.Text = x.Person.Communication1;
				lblGesetzBetreuerFax.Text = x.Person.Communication4;
			}



			var k = pRO.EnvironmentOrganisations.FirstOrDefault(f => f.RelationRole.TypeDescription.Equals("Krankenkasse"));
			if (k != null)
			{
				var fod = pRO.CostBearers.FirstOrDefault(f => f.CostBearer.OrganisationOid.Equals(k.Organisation.OrganisationOid));
				//kvNRTableCell.Text = fod.ReferenceNumber;
				//krankenkasseTableCell.Text = fod.CostBearer.Name;
			}

			var e = pRO.RelatedEmployees.FirstOrDefault(f => f.RelationRole.TypeDescription.ToLower().Equals("hauptbetreuung i"));

			if (e != null)
			{
				var emp = DAOFactory.GenericDAO.LoadByID<Employee>(e.Employee.EmployeeOid);
				lblHauptbetreuer1Name.Text = e.Employee.LastName;
				lblHauptbetreuer1Vorname.Text = e.Employee.FirstName;

				if (emp.Person.Contacts != null)
				{
					var contact = emp.Person.Contacts.FirstOrDefault(c => c.Type == ContactType.business_Phone);
					if (contact != null)
					{
						lblHauptbetreuer1Tel.Text = contact.Value;
					}
					contact = emp.Person.Contacts.FirstOrDefault(c => c.Type == ContactType.business_MobilePhone);
					if (contact != null)
					{
						lblHauptbetreuer1Handy.Text = contact.Value;
					}
				}
			}

			e = pRO.RelatedEmployees.FirstOrDefault(f => f.RelationRole.TypeDescription.ToLower().Equals("hauptbetreuung ii"));

			if (e != null)
			{
				var emp = DAOFactory.GenericDAO.LoadByID<Employee>(e.Employee.EmployeeOid);
				lblHauptbetreuer2Name.Text = e.Employee.LastName;
				lblHauptbetreuer2Vorname.Text = e.Employee.FirstName;

				if (emp.Person.Contacts != null)
				{
					var contact = emp.Person.Contacts.FirstOrDefault(c => c.Type == ContactType.business_Phone);
					if (contact != null)
					{
						lblHauptbetreuer2Tel.Text = contact.Value;
					}
					contact = emp.Person.Contacts.FirstOrDefault(c => c.Type == ContactType.business_MobilePhone);
					if (contact != null)
					{
						lblHauptbetreuer2Handy.Text = contact.Value;
					}
				}
			}
			//var gb = pRO.EnvironmentPersons.FirstOrDefault(f => f.RelationRole.TypeDescription.Equals("Gesetzlicher Vertreter"));
			//if (gb != null)
			//{
			//	var gbt = !string.IsNullOrEmpty(gb.Person.Title) ? gb.Person.Title + " " : "";
			//	vertretungTableCell.Text = gbt + gb.Person.FullName;
			//}

			//var bp = pRO.EnvironmentPersons.FirstOrDefault(f => f.RelationRole.TypeDescription.Equals("Bezugsperson"));
			//if (bp != null)
			//{
			//	var bpt = !string.IsNullOrEmpty(bp.Person.Title) ? bp.Person.Title + " " : "";
			//	bezugspersonTableCell.Text = bpt + bp.Person.FullName;
			//}

			
		}

		private void SetVarFieldValues(CustomerDataRO pRO)
		{
			String beruf = ""; // Oid = 1
			String arbeitgeber = ""; // Oid = 2
			String taetigkeit = ""; // Oid = 3
			String gruppenleiter = ""; // Oid = 4
			String leistungsart = ""; // Oid = 5
			String zuzahlungsbefreiung = ""; // Oid = 6
			String aufnahmedatum = ""; // Oid = 7
			String schwgueltig = ""; // Oid = 8
			String schwProzent = ""; // Oid = 9
			String merkzeichen = ""; // Oid = 10
			String sorgeGesundheit = ""; // Oid = 11
			String aufenthaltbetimmungr = ""; // Oid = 12
			String rechtsAntrags = ""; // Oid = 13
			String behoerden = ""; // Oid = 14
			String vermoegenssorge = ""; // Oid = 15

			var c = DAOFactory.GenericDAO.LoadByID<Customer>(pRO.CustomerOid);

			foreach (var vv in c.VarFieldValueList)
			{
				var varFieldValue = Utils.XMLDeserializeFromString(vv.SerializedValue, Type.GetType(vv.TypeName));
				if (varFieldValue != null)
				{
					if (vv.VarFieldDefOid.Value == 1)
					{
						beruf = varFieldValue.ToString();
					}
					else if (vv.VarFieldDefOid.Value == 2)
					{
						arbeitgeber = varFieldValue.ToString();
					}
					else if (vv.VarFieldDefOid.Value == 3)
					{
						taetigkeit = varFieldValue.ToString();
					}
					else if (vv.VarFieldDefOid.Value == 4)
					{
						gruppenleiter = varFieldValue.ToString();
					}
					else if (vv.VarFieldDefOid.Value == 5)
					{
						leistungsart = varFieldValue.ToString();
					}
					else if (vv.VarFieldDefOid.Value == 6)
					{
						zuzahlungsbefreiung = varFieldValue.ToString();
					}
					else if (vv.VarFieldDefOid.Value == 7)
					{
						aufnahmedatum = String.Format("{0:dd.MM.yyyy}", varFieldValue);
					}
					else if (vv.VarFieldDefOid.Value == 8)
					{
						schwgueltig = aufnahmedatum = String.Format("{0:dd.MM.yyyy}", varFieldValue);
					}
					else if (vv.VarFieldDefOid.Value == 9)
					{
						schwProzent = varFieldValue.ToString();
					}
					else if (vv.VarFieldDefOid.Value == 10)
					{
						merkzeichen = varFieldValue.ToString();
					}
					else if (vv.VarFieldDefOid.Value == 11)
					{
						sorgeGesundheit = varFieldValue.ToString() == "True" ? "x" : "";
					}
					else if (vv.VarFieldDefOid.Value == 12)
					{
						aufenthaltbetimmungr = varFieldValue.ToString() == "True" ? "x" : "";
					}
					else if (vv.VarFieldDefOid.Value == 13)
					{
						rechtsAntrags = varFieldValue.ToString() == "True" ? "x" : "";
					}
					else if (vv.VarFieldDefOid.Value == 14)
					{
						behoerden = varFieldValue.ToString() == "True" ? "x" : "";
					}
					else if (vv.VarFieldDefOid.Value == 15)
					{
						vermoegenssorge = varFieldValue.ToString() == "True" ? "x" : "";
					}
				}
			}

			lblBeruf.Text = beruf;
			lblArbeitg.Text = arbeitgeber;
			lblTaetigkeit.Text = taetigkeit;
			lblGruppenleiter.Text = gruppenleiter;
			lblLeistungsart.Text = leistungsart;
			lblZuzahlungsbefreiung.Text = zuzahlungsbefreiung;
			lblAufnahmeDatum.Text = aufnahmedatum;
			lblSchwGueltigBis.Text = schwgueltig;
			lblSchwProzent.Text = schwProzent;
			lblSchwMerkz.Text = merkzeichen;
			lblSorgeGesundheit.Text = sorgeGesundheit;
			lblAufenthaltbestimmung.Text = aufenthaltbetimmungr;
			lblRechtsAntrag.Text = rechtsAntrags;
			lblBehoerden.Text = behoerden;
			lblVermoegenssorge.Text = vermoegenssorge;

		}
    }
}
