/// <summary>
        /// Check for Scratch Card Validation Weather Card Exist or Used by Other or Used by same or User Exist
        /// </summary>
        /// <param name="requestModel"></param>
        /// <returns></returns>
        public CardValidationResponseModel ScratchCardValidation(ScratchCardRequestModel requestModel)
        {
            CardValidationResponseModel responseModel = new CardValidationResponseModel();

            #region GetUserID
            InstallationRepo installationRepo = new InstallationRepo();
            int? userId = installationRepo.ValidateInstallation(requestModel.InstallationGuid, requestModel.DeviceIdentifier,requestModel.ApplicationId);

            if (userId == null)
            {
                responseModel.IsValid = false;
                responseModel.Error = "User not exist";     
                return responseModel;
            }
            #endregion

            #region CardExist
            ScratchCardRepo scratchCardRepo = new ScratchCardRepo();
            bool IsExist = scratchCardRepo.ScratchCardExist(requestModel.CardNumber, requestModel.ApplicationId);
            if (!IsExist)
            {
                responseModel.IsValid = false;
                responseModel.Error = "Invalid Scratch card No";
                return responseModel;
            }
            #endregion

            #region CardUsed
            EnumTypes.ScratchCardUsed CardUsageEnum = scratchCardRepo.ScratchCardUsed(userId.Value, requestModel.CardNumber);  
            if (CardUsageEnum == EnumTypes.ScratchCardUsed.UsedBySame)
            {
                responseModel.IsValid = false;
                responseModel.Error = "Used by Same";
                return responseModel;
            }
            else if(CardUsageEnum== EnumTypes.ScratchCardUsed.UsedByOther)
            {
                responseModel.IsValid = false;
                responseModel.Error = "Used By Other";
                return responseModel;
            }
            #endregion

            #region RemoveFreeData
            DestroyFreeDataRepo freeDataRepo = new DestroyFreeDataRepo();
            freeDataRepo.DeleteFreeData(userId.Value); 
            #endregion

            #region CardType
            bool IsComplementary = scratchCardRepo.ScratchCardComplementary(requestModel.CardNumber);
            #endregion

            responseModel.IsValid = true;
            responseModel.Error = null;
            responseModel.IsComplimentary = IsComplementary;
            responseModel.UserId = userId.Value;
            return responseModel;
        }
        /// <summary>
        /// Delete free data , create file name , Get all dataset of users and generate file if not exist on file system
        /// </summary>
        /// <param name="requestModel"></param>
        /// <returns></returns>
        public DataResponseModel DownloadFile(DataRequestModel requestModel)
        {
            DataResponseModel dataResponseModel = new DataResponseModel();
            var datasetIds = new List<int>();

            DownloadRepo downloadrepo = new DownloadRepo();
            int? UserId = downloadrepo.GetUserId(requestModel.GUID, requestModel.DeviceId, requestModel.AppId);

            if(UserId == null)
            {
                dataResponseModel.IsValid = false;
                dataResponseModel.Error = "Invalid User";
                return dataResponseModel;
            }


            if ((requestModel.DatasetID == 0))   //Full Data                      //If full data
            {
                if (UserId == null)
                {
                    dataResponseModel.IsValid = false;
                    dataResponseModel.Error = "Invalid User";
                    return dataResponseModel;
                }


                List<int> getUserDataSets = downloadrepo.GetUserDataset(UserId.Value);

                if (getUserDataSets == null)
                {
                    dataResponseModel.IsValid = false;
                    dataResponseModel.Error = "Invalid User";
                    return dataResponseModel;
                }

                DestroyFreeDataRepo destroyFree = new DestroyFreeDataRepo();
                destroyFree.DeleteFreeData(UserId.Value);

                datasetIds = getUserDataSets;
            }
            else       
            {

                DestroyFreeDataRepo destroyFree = new DestroyFreeDataRepo();
                destroyFree.DeleteFreeData(UserId.Value);

                AddFreeData addFreeData = new AddFreeData();
                addFreeData.AddData(UserId.Value,requestModel.DatasetID);

                datasetIds.Add(requestModel.DatasetID);
            }

            string fileName = "";
            var folderPath = "";
            var filePath = "";

            GenerateJSON createFile = new GenerateJSON();            //Create File Name
            createFile.createFileName(datasetIds,ref filePath ,ref folderPath ,ref fileName);

            
            if (!File.Exists(filePath))
            {
                GenerateJSON generateJSON = new GenerateJSON();       //File Not Exists then generate data
                generateJSON.generateJSON(datasetIds,filePath,folderPath,fileName);
            }

            filePath = System.Web.Hosting.HostingEnvironment.MapPath(string.Format("{0}{1}", folderPath, fileName + ".gz"));   //Creating File Path

            GenerateTokenRepo token = new GenerateTokenRepo();
            string TokenId = token.AddToDatabase(requestModel.GUID , filePath);


            //InstallationRepo installationRepo = new InstallationRepo();
            //installationRepo.LastDateUpdated(requestModel.GUID);

            dataResponseModel.IsValid = true;
            dataResponseModel.Error = null;
            dataResponseModel.TokenId = TokenId;
            return dataResponseModel;
        }