[ActionName("verifymobile")]                        //[Route("api/verification/verifymobile/{PhoneNumber}")]
        public HttpResponseMessage VerifyMobile(ClientRequestModel requestModel)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid Request");
            }

            requestModel = new ClientRequestModel
            {
                //PhoneNumber = "0341087",
                //CountryCode = "PK"

                PhoneNumber = requestModel.PhoneNumber,
                CountryCode = requestModel.CountryCode
            };


            VerificationAction verifyActions = new VerificationAction();
            ClientResponseModel responseModel = verifyActions.VerifyMobile(requestModel);

            if (responseModel.Error != null)
            {
                return Request.CreateResponse(HttpStatusCode.OK, responseModel , "application/json");
            }

            return Request.CreateResponse(HttpStatusCode.OK, responseModel, "application/json");
        }
 public ClientResponseModel VerifyMobile(ClientRequestModel requestModel)
 {
     VerificationCase verifycase = new VerificationCase();
     return verifycase.VerifyMobile(requestModel);
 }
        /// <summary>
        /// Add request In Meomry
        /// </summary>
        /// <param name="requestModel"></param>
        /// <returns></returns>
        public ClientResponseModel VerifyMobile(ClientRequestModel requestModel)
        {
            ClientResponseModel responseModel = new ClientResponseModel();
            DurationOfNextRequestToServerFromSender = SmsSendingInterval + SmsReceivingInterval + threeAttemptsByServer;

            #region //Check Senders Exist Or Not
            var GetAllSenders = (from pool in PoolList
                                 from GetAllsenders in pool.ListOfSenders
                                 select GetAllsenders).ToList();

            if (GetAllSenders.Count == 0)    //Phone Number Verification
            {
                responseModel.IsValid = false;
                responseModel.Error = "Service Unavailable Try Again Later";
                return responseModel;
            }
            #endregion

            #region //Generate Code
            Random generateCode = new Random();
            int code = generateCode.Next(111111, 999999);

            #endregion

            #region //Assign Values
            SMSRequestModel smsModel = new SMSRequestModel
            {
                PhoneNumber = requestModel.PhoneNumber,
                CountryCode = requestModel.CountryCode,
                PinCode = code,
                RequestTime = DateTime.Now
            };

            #endregion

            #region //Assign Pool To request
            foreach (var item in PoolList)
            {
                if (item.CountryCodes.Contains(requestModel.CountryCode))
                {
                    smsModel.PoolName = item.PoolName;
                    break;
                }
            }


            #endregion

            #region //Assign Unique ID
            if (listOfSmsModel.Count == 0)
            {
                smsModel.UniqueId = 1;
            }

            else
            {
                smsModel.UniqueId = listOfSmsModel.Count + 1;
            }
            listOfSmsModel.Add(smsModel);

            #endregion

            #region //Get all Request
            int CountActiveSenders = 0;
            var getRequest = listOfSmsModel.OrderBy(x => x.RequestTime).Where(x => x.SmsState == "Unread" && x.CountryCode == requestModel.CountryCode).ToList();

            #endregion

            #region //Active Sender
            //Get Senders List
            var sendersList = (from pool in PoolList
                               from senderslist in pool.ListOfSenders
                               where pool.PoolName == smsModel.PoolName && senderslist.IsActive == true
                               select senderslist).Distinct().ToList();

            //Get Active Senders
            foreach (var singleObject in sendersList)
            {
                if (DateTime.Now > singleObject.NextAppearTime)
                {
                    //If difference is less than active time than mark sender as active
                    if ((DateTime.Now - singleObject.NextAppearTime).TotalSeconds > SenderInactiveTime)
                    {
                        singleObject.IsActive = false;
                    }

                    else
                        CountActiveSenders++;
                }

                else
                {
                    CountActiveSenders++;
                }
            }

            #endregion

            #region //Get dedicated Numbers
            var dedicatedNumbersList = (from pool in PoolList
                                        from senderslist in pool.ListOfSenders
                                        from senders in senderslist.MobileNumber
                                        where pool.PoolName == smsModel.PoolName && senderslist.IsActive == true
                                        select senderslist.MobileNumber).Distinct().ToList();
            #endregion

            #region //NextSenderComingTime And Interveiw
            //Get List of Next Appear Time of All sender
            var NextAppearTime = (from pool in PoolList
                                  from ListOfSender in pool.ListOfSenders
                                  where pool.PoolName == smsModel.PoolName
                                  select ListOfSender.NextAppearTime).ToList();

            //getting Time of Next Sender
            DateTime EstimatedDateInNextSender = NextAppearTime.OrderBy(x => x).FirstOrDefault(x => x > smsModel.RequestTime);
            double EstimatedSecondsRemainingInNextSender = (EstimatedDateInNextSender - smsModel.RequestTime).TotalSeconds;


            if (EstimatedSecondsRemainingInNextSender < 0)    //If No sender is able to come after date time.Now
            {
                responseModel.IsValid = false;
                responseModel.Error = "Service Unavailable or try Again Later";
                return responseModel;
            }
            #endregion

            #region //Set Estimated Time If only 1 Request
            if (getRequest.Count <= 1)
            {
                EstimatedTimeToReachSms = DurationOfNextRequestToServerFromSender + EstimatedSecondsRemainingInNextSender;
            }
            #endregion

            #region //Set Estimated Time If More Than 1 Request
            else
            {
                //Get Request Time
                double TotalRequestTime = getRequest.Count * (DurationOfNextRequestToServerFromSender) + EstimatedSecondsRemainingInNextSender;


                //Getting Time of Second Sender
                List<DateTime> EstimatedDateInSecondSender = NextAppearTime.OrderBy(x => x).Where(x => x > smsModel.RequestTime).ToList();

                //To Check If second Sender Exist Or Not
                if (EstimatedDateInSecondSender.Count > 1)
                {
                    double EstimatedSecondsRemainingInSecondSender = (EstimatedDateInSecondSender.ElementAt(1) - smsModel.RequestTime).TotalSeconds;

                    if (TotalRequestTime > EstimatedSecondsRemainingInSecondSender)
                    {
                        double Mean = (TotalRequestTime + EstimatedSecondsRemainingInSecondSender) / 2;

                        if (Mean > MaximumWaitTime)
                        {
                            EstimatedTimeToReachSms = MaximumWaitTime;
                        }

                        else
                            EstimatedTimeToReachSms = Mean;
                    }
                }

                else
                    EstimatedTimeToReachSms = MaximumWaitTime;
            }
            #endregion

            #region //ReturnResponse
            responseModel.DedicatedNumbers = dedicatedNumbersList;
            responseModel.Code = smsModel.PinCode;
            responseModel.RequestId = smsModel.UniqueId;
            responseModel.EstimatedTime = Convert.ToInt32(EstimatedTimeToReachSms);
            responseModel.IsValid = true;
            responseModel.Error = null;
            return responseModel;
            #endregion
        }