/// <summary>
        /// Initializes a new instance of the <see cref="AgentApiController"/> class. 
        /// Constructor
        /// </summary>
        /// <param name="gleanerContext">
        /// The <see cref="IGleanerContext"/>
        /// </param>
        public AgentApiController(IGleanerContext gleanerContext)
            : base(gleanerContext)
        {
            _agentService = GleanerContext.Services.AgentService;

            var settingService = GleanerContext.Services.SettingService as SettingService;
            Int32.TryParse(settingService.AllSettings["ItemsPerAutoComplete"].IfNotNull(x => x.Value), out _autoCompleteResultAmount);
            Int32.TryParse(settingService.AllSettings["ItemsPerPage"].IfNotNull(x => x.Value), out _itemsPerPage);
        }
        public void PostSaveForm(AgentProfileModel agentProfile)
        {
            //TODO: trim values before operating with null check
            var homeNode = Umbraco.TypedContentSingleAtXPath("/root/Data");
            var mu = Membership.GetUser();
            var um = Services.MemberService.GetByUsername(mu.UserName);
            var agentNumber = um.GetValue<string>("agentNumber");
            AgentView agent = new AgentService().GetAgent(agentNumber);
            string emailBody = string.Empty;
            string emailHeader = "<tr><th>Properties</th><th>Old Value</th><th>New value</th></tr>";

            var agentViewProperties = agent.GetType().GetTypeInfo().DeclaredProperties;

            string emailTo = homeNode.GetPropertyValue<string>("agentChangeEmail");
            var propsForDbModified = false;
            foreach (var propForDb in AgentProfileModel.PropsForDb)
            {
                var agentProp = agentViewProperties.First(x => x.Name.ToUpper() == propForDb.Name.ToUpper());
                var oldValue = Convert.ChangeType(agentProp.GetValue(agent), propForDb.PropertyType);
                if (oldValue != null && oldValue is string) oldValue = (oldValue as string).Trim();
                var newValue = propForDb.GetValue(agentProfile);
                if (newValue != null && newValue is string) newValue = (newValue as string).Trim();
                var equals = GetEquals(propForDb.PropertyType);
                if (equals != null && (bool)equals.Invoke(oldValue, new[] { newValue })) continue;
                emailBody += "<tr><td>" + propForDb.Name + "</td>" + "<td>" + oldValue + "</td>" + "<td>" + newValue + "</td></tr>";
                propsForDbModified = true;
            }
            var valuesForProfile = new Dictionary<string, object>();
            var propsForProfileModified = false;
            foreach (var propForProfile in AgentProfileModel.PropsForProfile)
            {
                if (!um.HasProperty(propForProfile.Name)) continue;
                object newValue;
                if (propForProfile.Name == "AgentProducts")
                {
                    //AgentProducts is the only array property, so it should be processed in specific way
                    IEnumerable<int> oldProducts = Enumerable.Empty<int>();
                    try
                    {
                        oldProducts = um.GetValue<string>(propForProfile.Name).Split(',').Select(Int32.Parse).Distinct().ToArray();
                    }
                    catch (ArgumentNullException)
                    { }
                    catch (FormatException)
                    { }
                    var newProducts = propForProfile.GetValue(agentProfile) as IEnumerable<int>;
                    if (newProducts == null) newProducts = Enumerable.Empty<int>();
                    else newProducts = newProducts .Distinct().ToArray();
                    if (oldProducts.Count() == newProducts.Count() && newProducts.Intersect(oldProducts).Count() == oldProducts.Count()) continue;
                    newValue = newProducts;
                }
                else
                {
                    var getValue = Gleaner.Web.Controllers.DashboardAgentController.GetGetValue(propForProfile.PropertyType);
                    var oldValue = getValue.Invoke(um, new[] { propForProfile.Name });
                    if (oldValue != null && oldValue is string) oldValue = (oldValue as string).Trim();
                    newValue = propForProfile.GetValue(agentProfile);
                    if (newValue != null && newValue is string) newValue = (newValue as string).Trim();
                    var equals = GetEquals(propForProfile.PropertyType);
                    if (equals != null && (bool)equals.Invoke(oldValue, new[] { newValue })) continue;
                }
                valuesForProfile[propForProfile.Name] = newValue;
                propsForProfileModified = true;
            }
            var siteUrl = Request.Url.Scheme + "://" + Request.Url.Authority + VirtualPathUtility.ToAbsolute("~/");
            if (propsForDbModified)
            {
                emailBody = "<table align='center' border='1' bordercolor='#ccc' cellpadding='5'>" + emailHeader + emailBody + "</table>";
                if (propsForProfileModified)
                {
                    emailBody += "<p><a href='" + siteUrl + "umbraco/#/admin/admin/agentapprove/" + um.Id + "'>Link to the rest agent properties approval</a></p>";
                }
            }
            if (propsForProfileModified)
            {
                um.SetValue("requestedProperties", Newtonsoft.Json.JsonConvert.SerializeObject(valuesForProfile));
                Services.MemberService.Save(um);
                if (!propsForDbModified)
                {
                    emailBody = "<p><a href='" + siteUrl + "umbraco/#/admin/admin/agentapprove/" + um.Id + "'>Link to the agent properties approval</a></p>";
                }
            }
            if (propsForDbModified || propsForProfileModified)
            {
                Scandia.SiteUtils.SendMail(null, null, emailTo, emailTo, "New request for agent profile update", emailBody, true);
            }
        }