/*
  * Get mapping type for a static information, to detect if mapping
  * directly with lastest information from patient or just copy value
  */
 public string GetMappingType(CustomSnippet customSnippet)
 {
     var customSnippetFields = customSnippet.CustomSnippetFields.ToList();
     var mappingType = customSnippetFields.Where(x => x.FieldName == "mappingtype").FirstOrDefault();
     if (mappingType != null) {
         JArray mappingTypeList = JArray.Parse(mappingType.Value);
         foreach (dynamic type in mappingTypeList)
         {
             if (type.selected) return type.value;
         }
     }
     return "";
 }
        public string GetValueForSnippet(CustomSnippet customSnippet, int patientId, int medicalProfileId)
        {
            string value = "";

            //First, try to get data from CustomSnippetValue
            var customSnippetValue = _db.CustomSnippetValues.Where(
                        x => (x.CustomSnippetId == customSnippet.CustomSnippetId)
                        && (x.MedicalProfileId == medicalProfileId)).FirstOrDefault();
            if (customSnippetValue == null)
            {
                customSnippetValue = new CustomSnippetValue
                {
                    CustomSnippet = customSnippet,
                    MedicalProfileId = medicalProfileId
                };
                _db.CustomSnippetValues.Add(customSnippetValue);
                _db.SaveChanges();
            }
            value = customSnippetValue.Value;

            var user = _db.Users.Where(pa => pa.UserId == patientId).SingleOrDefault();
            var patient = _db.Patients.Where(pa => pa.UserId == patientId).SingleOrDefault();
            var personalHealthRecord = _db.PersonalHealthRecords.Where(pa => pa.PatientId == patientId).SingleOrDefault();
            //Use reflection to get binding data
            if (customSnippet.SnippetType != SnippetType.Custom && String.IsNullOrEmpty(value))
            {
                value = "";
                switch (customSnippet.SnippetType)
                {
                    case SnippetType.User:
                        Type type = typeof(User);
                        object valueProperty;
                        if (customSnippet.SnippetFieldName.Equals("Age"))
                        {
                            valueProperty = type.GetProperty
                                ("Birthday", BindingFlags.IgnoreCase
                                | BindingFlags.Public
                                | BindingFlags.Instance).GetValue(user, null);
                            if (valueProperty != null)
                            {
                                value = valueProperty.ToString();
                                var birthday = DateTime.Parse(value.ToString());
                                value = DateTimeUtils.CalculateAge(birthday).ToString();
                            }
                        }
                        else
                        {
                            valueProperty = type.GetProperty
                                (customSnippet.SnippetFieldName, BindingFlags.IgnoreCase
                                | BindingFlags.Public
                                | BindingFlags.Instance).GetValue(user, null);
                            if (valueProperty != null)
                            {
                                value = valueProperty.ToString();
                                if ("Gender".Equals(customSnippet.SnippetFieldName))
                                {
                                    value = "Nam".Equals(value) ? "Nam" : "Nữ";
                                }
                                if (customSnippet.SnippetFieldName.Equals("Birthday"))
                                {
                                    var birthday = DateTime.Parse(value.ToString());
                                    value = birthday.ToString("dd/MM/yyyy");
                                }
                            }
                        }
                        break;
                    case SnippetType.Patient:
                        type = typeof(Patient);
                        valueProperty = type.GetProperty
                            (customSnippet.SnippetFieldName, BindingFlags.IgnoreCase
                            | BindingFlags.Public
                            | BindingFlags.Instance).GetValue(patient, null);
                        if (valueProperty != null) {
                            value = valueProperty.ToString();
                        }
                        break;
                    case SnippetType.PersonalHealthRecord:
                        type = typeof(PersonalHealthRecord);
                        valueProperty = type.GetProperty
                            (customSnippet.SnippetFieldName, BindingFlags.IgnoreCase
                            | BindingFlags.Public
                            | BindingFlags.Instance).GetValue(personalHealthRecord, null);
                        if (valueProperty != null)
                        {
                            value = valueProperty.ToString();
                        }
                        break;
                }
            }
            return value;
        }
        public List<CustomSnippet> ConvertJsonStringToCustomSnippetList(string jsonString, MedicalProfileTemplate template)
        {
            List<CustomSnippet> resultCustomSnippetList = new List<CustomSnippet>();

            JArray listSnippets = JArray.Parse(jsonString) as JArray;
            int position = 0;
            foreach (dynamic snippet in listSnippets)
            {
                CustomSnippet customSnippet = new CustomSnippet { Title = snippet.title,
                    Name = snippet.name,
                    MedicalProfileTemplateId = template.MedicalProfileTemplateId };

                /*
                 * This is for mapping one-one attribute
                 * we don't need to track it
                 */
                if (snippet.title == "Static Text")
                {
                    var SnippetTypeDic = new Dictionary<string, SnippetType> {
                        { "Custom", SnippetType.Custom },
                        { "User", SnippetType.User },
                        { "Patient", SnippetType.Patient },
                        { "PersonalHealthRecord", SnippetType.PersonalHealthRecord }
                    };
                    string str = ((object)snippet.snippettype).ToString();
                    customSnippet.SnippetType = SnippetTypeDic[((object)snippet.snippettype).ToString()];
                    customSnippet.SnippetFieldName = snippet.fieldname;
                }
                position++;
                if (snippet.parentId != null)
                {
                    customSnippet.ParentId = snippet.parentId;
                    customSnippet.PositionInTable = snippet.positionInTable;
                }
                customSnippet.Position = position;
                resultCustomSnippetList.Add(customSnippet);
                customSnippet.CustomSnippetFields = new Collection<CustomSnippetField>();
                if (snippet.fields != null)
                {
                    foreach (dynamic snippetField in snippet.fields)
                    {
                        dynamic metadata = snippetField.Value;

                        if ("id".Equals(snippetField.Name))
                        {
                            if (metadata.value == null)
                                customSnippet.CustomSnippetId = 0;
                            else
                                customSnippet.CustomSnippetId = metadata.value;
                        }

                        CustomSnippetField customSnippetField = new CustomSnippetField
                        {
                            CustomSnippet = customSnippet,
                            FieldName = snippetField.Name,
                            Label = metadata.label,
                            Type = metadata.type,
                            Value = ((object)metadata.value).ToString(),
                            Name = metadata.name
                        };
                        customSnippet.CustomSnippetFields.Add(customSnippetField);
                    }
                }
            }
            return resultCustomSnippetList;
        }