/*        public static Domain.ClientCollection GetClientsByPhysicianId(int physicianId)
        {
        #if MONGO
            return PhysicianClientGatewayMongo.GetClientsByPhysicianId(physicianId);
        #else
            Domain.ClientCollection result = new Domain.ClientCollection();

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select c.* " +
               "from tblClient c " +
               "join tblPhysicianClient pc on c.ClientId = pc.ClientId " +
               "where pc.PhysicianId = @PhysicianId ";
            cmd.Parameters.Add("@PhysicianId", SqlDbType.Int).Value = physicianId;
            cmd.CommandType = CommandType.Text;

            using (SqlConnection cn = new SqlConnection(YellowstonePathology.Business.BaseData.SqlConnectionString))
            {
                cn.Open();
                cmd.Connection = cn;
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        YellowstonePathology.Business.Client.Model.Client client = new YellowstonePathology.Business.Client.Model.Client();
                        YellowstonePathology.Business.Persistence.SqlDataReaderPropertyWriter sqlDataReaderPropertyWriter = new Persistence.SqlDataReaderPropertyWriter(client, dr);
                        sqlDataReaderPropertyWriter.WriteProperties();
                        result.Add(client);
                    }
                }
            }
            return result;
        #endif
        }*/
        /*        public static Domain.PhysicianClient GetPhysicianClient(int physicianId, int clientId)
        {
        #if MONGO
            return PhysicianClientGatewayMongo.GetPhysicianClient(physicianId, clientId);
        #else
            Domain.PhysicianClient result = null;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Select * from tblPhysicianClient where PhysicianId = @PhysicianId and ClientId = @ClientId";
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@PhysicianId", SqlDbType.Int).Value = physicianId;
            cmd.Parameters.Add("@ClientId", SqlDbType.Int).Value = clientId;

            using (SqlConnection cn = new SqlConnection(YellowstonePathology.Business.BaseData.SqlConnectionString))
            {
                cn.Open();
                cmd.Connection = cn;
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        result = new Domain.PhysicianClient();
                        YellowstonePathology.Business.Persistence.SqlDataReaderPropertyWriter sqlDataReaderPropertyWriter = new Persistence.SqlDataReaderPropertyWriter(result, dr);
                        sqlDataReaderPropertyWriter.WriteProperties();
                    }
                }
            }

            return result;
        #endif
        }

        public static View.ClientPhysicianView GetClientPhysicianViewByClientId(int clientId)
        {
        #if MONGO
            return PhysicianClientGatewayMongo.GetClientPhysicianViewByClientId(clientId);
        #else
            View.ClientPhysicianView result = null;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "with phys as(select p.* from tblPhysician p join tblPhysicianClient pc on p.PhysicianId = pc.PhysicianId where pc.ClientId = @ClientId) " +
                "select c.*," +
                " ( select phys.*" +
                "   from phys order by phys.FirstName for xml Path('Physician'), type) Physicians" +
                " from tblClient c where c.ClientId = @ClientId for xml Path('Client'), root('ClientPhysicianView')";
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@ClientId", SqlDbType.Int).Value = clientId;
            XElement resultElement = PhysicianClientGateway.GetXElementFromCommand(cmd);
            if (resultElement != null)
            {
                result = PhysicianClientGateway.BuildClientPhysicianView(resultElement);
            }
            return result;
        #endif
        }*/
        private static View.ClientPhysicianView BuildClientPhysicianView(XElement sourceElement)
        {
            View.ClientPhysicianView result = new View.ClientPhysicianView();
            XElement clientElement = sourceElement.Element("Client");
            YellowstonePathology.Business.Persistence.XmlPropertyWriter xmlPropertyWriter = new Persistence.XmlPropertyWriter(clientElement, result);
            xmlPropertyWriter.Write();

            XElement physiciansElement = clientElement.Element("Physicians");
            if(physiciansElement != null)
            {
                List<XElement> physicianElements = physiciansElement.Elements("Physician").ToList<XElement>();
                foreach (XElement physicianElement in physicianElements)
                {
                    Domain.Physician physician = new Domain.Physician();
                    YellowstonePathology.Business.Persistence.XmlPropertyWriter xmlPropertyWriter1 = new Persistence.XmlPropertyWriter(physicianElement, physician);
                    xmlPropertyWriter1.Write();
                    result.Physicians.Add(physician);
                }
            }
            return result;
        }
        public static View.ClientPhysicianView GetClientPhysicianViewByClientIdV2(int clientId)
        {
            /*View.ClientPhysicianView result = null;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "with phys as(select p.* from tblPhysician p join tblPhysicianClient pc on p.ObjectId = pc.ProviderId where pc.ClientId = @ClientId) " +
                "select c.*," +
                " ( select phys.*" +
                "   from phys order by phys.FirstName for xml Path('Physician'), type) Physicians" +
                " from tblClient c where c.ClientId = @ClientId for xml Path('Client'), root('ClientPhysicianView')";
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@ClientId", SqlDbType.Int).Value = clientId;
            XElement resultElement = PhysicianClientGateway.GetXElementFromCommand(cmd);
            if (resultElement != null)
            {
                result = PhysicianClientGateway.BuildClientPhysicianView(resultElement);
            }
            return result;*/

            View.ClientPhysicianView result = null;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select * from tblClient c where c.ClientId = @ClientId " +
                "select p.*from tblPhysician p join tblPhysicianClient pc on p.ObjectId = pc.ProviderId where pc.ClientId = @ClientId order by p.FirstName";
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@ClientId", SqlDbType.Int).Value = clientId;
            using (SqlConnection cn = new SqlConnection(YellowstonePathology.Properties.Settings.Default.CurrentConnectionString))
            {
                cn.Open();
                cmd.Connection = cn;
                using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.KeyInfo))
                {
                    while (dr.Read())
                    {
                        result = new View.ClientPhysicianView();
                        Persistence.SqlDataReaderPropertyWriter sqlDataReaderPropertyWriter = new Persistence.SqlDataReaderPropertyWriter(result, dr);
                        sqlDataReaderPropertyWriter.WriteProperties();
                    }
                    if (dr.IsClosed == false)
                    {
                        dr.NextResult();
                        while (dr.Read())
                        {
                            Domain.Physician physician = new Domain.Physician();
                            Persistence.SqlDataReaderPropertyWriter sqlDataReaderPropertyWriter = new Persistence.SqlDataReaderPropertyWriter(physician, dr);
                            sqlDataReaderPropertyWriter.WriteProperties();
                            result.Physicians.Add(physician);
                        }
                    }
                }
            }
            return result;
        }