/// <summary>
        /// Returns a MVCList with all contacts from a Client.
        /// </summary>
        /// <param name="page"></param>
        /// <param name="idCliente"></param>
        /// <returns></returns>
        public MvcList<ContactModel> ListAll(int? page, int idCliente)
        {
            var promoter = new WS.Promoter.Promoter().GetPromoter(Settings.AutenticationKeyEarningSystem, idCliente);

            if (promoter != null)
            {
                var contactList = new Contact().SelectAll(Settings.AutenticationKeyEarningSystem, promoter.IdPromoter);

                page = page ?? 1;

                var contactListPage = new Tuple<IEnumerable<ContactModel>, Int32>(
                    contactList.OrderBy(x => x.Name)
                        .Skip((page.Value * Constants.ManageContactRegistersPerPage) - Constants.ManageContactRegistersPerPage)
                        .Take(Constants.ManageContactRegistersPerPage).ToList(), contactList.Count());

                var list = new MvcList<ContactModel>(contactListPage.Item1,
                                                     page.Value,
                                                     contactListPage.Item2,
                                                     Constants.ManageContactRegistersPerPage);

                return list;
            }

            return new MvcList<ContactModel>(new ContactModel[0], 0, 0);
        }
        public JsonResult Update(string idContact, string name, string email)
        {
            if (new WS.Promoter.Promoter().ValidateEmail(Settings.AutenticationKeyEarningSystem, email)) return Json(new JsonRequestResult { ResultType = JsonRequestResultType.Error, Message = Resources.Resource.GS_Contact_EmailAlreadyExist }, JsonRequestBehavior.AllowGet);

            try
            {
                var idContato = Convert.ToInt32(idContact);

                var contactUpdate = new Contact().SelectById(Settings.AutenticationKeyEarningSystem,
                                                                        idContato);

                if (contactUpdate != null)
                {
                    contactUpdate.Name = name;
                    contactUpdate.Email = email;

                    if(new Contact().ValidateEmailExists(Settings.AutenticationKeyEarningSystem, contactUpdate))
                    {
                        return Json(new JsonRequestResult { ResultType = JsonRequestResultType.Error, Message = Resources.Resource.GS_Invite_ContactAlreadyExists }, JsonRequestBehavior.AllowGet);    
                    }

                    var status = new Contact().Update(Settings.AutenticationKeyEarningSystem, contactUpdate);

                    if (status)
                    {
                        return
                            Json(
                                new JsonRequestResult
                                    {
                                        ResultType = JsonRequestResultType.Success,
                                        Message = Resources.Resource.GS_Contact_Update_Success
                                    },
                                JsonRequestBehavior.AllowGet);

                    }

                    return Json(new JsonRequestResult { ResultType = JsonRequestResultType.Error, Message = Resources.Resource.GS_Contact_Update_Error }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                LogService.Log("GS.ContactController.Update() :: Exception : ", ex);

                return Json(new JsonRequestResult { ResultType = JsonRequestResultType.Error, Message = Resources.Resource.GS_Contact_Update_Error }, JsonRequestBehavior.AllowGet);
            }

            return new JsonResult();
        }
        public JsonResult Delete(string idContact)
        {
            try
            {
                var idContato = Int32.Parse(idContact);

                var contactDelete = new Contact().SelectById(Settings.AutenticationKeyEarningSystem, idContato);

                if (contactDelete != null)
                {
                    var dontHaveSendMail = contactDelete.IdContact != null && new Contact().ValidateSendMail(
                        Settings.AutenticationKeyEarningSystem, contactDelete.IdContact.Value);

                    if (dontHaveSendMail)
                    {
                        var statusDelete = new Contact().Delelete(Settings.AutenticationKeyEarningSystem,
                                                     contactDelete);

                        if (statusDelete)
                        {
                            return Json(new JsonRequestResult { ResultType = JsonRequestResultType.Success, Message = Resources.Resource.GS_Contact_Delete_Success }, JsonRequestBehavior.AllowGet);
                        }
                    }
                    else
                    {
                        return Json(new JsonRequestResult { ResultType = JsonRequestResultType.Error, Message = Resources.Resource.GS_Contact_Delete_HaveEmail }, JsonRequestBehavior.AllowGet);
                    }
                    return Json(new JsonRequestResult { ResultType = JsonRequestResultType.Error, Message = Resources.Resource.GS_Contact_Delete_Error }, JsonRequestBehavior.AllowGet);
                }

            }
            catch (Exception ex)
            {
                LogService.Log("GS.ContactController.Delete() :: Exception : ", ex);

                return Json(new JsonRequestResult { ResultType = JsonRequestResultType.Error, Message = ex.Message }, JsonRequestBehavior.AllowGet);
            }

            return new JsonResult();
        }
        public JsonResult Create(string name, string email)
        {
            try
            {
                if (new WS.Promoter.Promoter().ValidateEmail(Settings.AutenticationKeyEarningSystem, email)) return Json(new JsonRequestResult { ResultType = JsonRequestResultType.Error, Message = Resources.Resource.GS_Contact_EmailAlreadyExist }, JsonRequestBehavior.AllowGet);

                if (SecurityHelper.GainSystemUserIsAuthenticated())
                {
                    var userAuth = FormsAuthenticationUtil.UserAuthenticated;

                    if (userAuth != null)
                    {
                        if (userAuth.IdCliente != null)
                        {
                            var promoter = new WS.Promoter.Promoter().GetPromoter(Settings.AutenticationKeyEarningSystem, userAuth.IdCliente.Value);

                            if (promoter != null)
                            {
                                var contact = new ContactModel
                                                  {
                                                      Name = name, 
                                                      Email = email, 
                                                      IdPromoter = promoter.IdPromoter
                                                  };

                                string validationMessage;
                                if (Service.ValidarNovoContato(Settings.AutenticationKeyEarningSystem, contact, out validationMessage))
                                {

                                    var idNewContact = new Contact().Insert(Settings.AutenticationKeyEarningSystem,
                                                                            contact);
                                    if (idNewContact != 0)
                                    {
                                        return
                                            Json(
                                                new JsonRequestResult
                                                    {
                                                        ResultType = JsonRequestResultType.Success,
                                                        Message = Resources.Resource.GS_Contact_Create_Success
                                                    },
                                                JsonRequestBehavior.AllowGet);
                                    }

                                    return
                                        Json(
                                            new JsonRequestResult
                                                {
                                                    ResultType = JsonRequestResultType.Error,
                                                    Message = Resources.Resource.GS_Contact_Create_Error
                                                },
                                            JsonRequestBehavior.AllowGet);
                                }
                                else
                                    return
                                        Json(
                                            new JsonRequestResult
                                            {
                                                ResultType = JsonRequestResultType.Error,
                                                Message = validationMessage
                                            },
                                            JsonRequestBehavior.AllowGet);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogService.Log("GS.ContactController.Create() :: Exception : ", ex);

                return Json(new JsonRequestResult { ResultType = JsonRequestResultType.Error, Message = Resources.Resource.GS_Contact_Create_Error }, JsonRequestBehavior.AllowGet);
            }

            return new JsonResult();
        }