#pragma once

template<typename T>
int parseNumber(FILE* input, const char* keyword, const size_t bufLen, T* n){

	char buf[bufLen];
	char buf1[bufLen];
	char comment[1];
	char *ret;

	while (fgets(buf,bufLen, input)!=NULL){
		comment[0]=buf[0];
		if(strncmp(comment,"#",1)==0){
			//printf("Comment!:%s\n",buf);
		}
		else{
			ret=strtok(buf,"=");
			if(strcmp(ret,keyword)==0){
				/*offset buf by keyword size + 1 for the "="*/
				strncpy (buf1, buf+strlen(keyword)+1, bufLen);	
       				printf("%30s: ",keyword);
				getFromString(buf1,n);
				rewind(input);
				return(0);
			}
		}
	}
	rewind(input);
	return(-1);
}

template<typename T>
int parseArray(FILE* input, const char* keyword, const size_t bufLen,
		const size_t arrLen, T y[]){

	char buf[bufLen];
	char buf1[bufLen];
	char comment[1];
	char *ret;

	while (fgets(buf,bufLen, input)!=NULL){
		comment[0]=buf[0];
		if(strncmp(comment,"#",1)==0){
			//printf("Comment!:%s\n",buf);
		}
		else{
			ret=strtok(buf,"=");

			if(strcmp(ret,keyword)==0){
				/*offset buf by keyword size + 1 for the "="*/
				strncpy (buf1, buf+strlen(keyword)+1, bufLen);	
       				printf("%30s:\n",keyword);
				ret=strtok(buf1,",");
				size_t j=0;
				while(ret!=NULL){
					if(j<arrLen){
						//y[j]=atof(ret);
						getFromString(ret,&y[j]);
					}
   		  			ret=strtok(NULL,",");
					j++;
				}
				rewind(input);
				if(j!=arrLen){
					printf("Check no: of values entered for %s\n",keyword);
					printf("%lu values required!\n",arrLen);
					return(-1);
				}
				else{
					return(0);
				}
			}
		}
	}
	rewind(input);
	return(-1);
}


/* Extract the droplet species and molefractions from input file using template */
/* Generic template function */
template<typename T>
int parseDropSpec(FILE* input, const char* keyword, const size_t bufLen,const int* dropType, std::vector<T>& dropPara){
    char buf[bufLen];
    char buf1[bufLen];
    char comment[1];
    char *ret;
    T n;

    while (fgets(buf,bufLen, input)!=NULL){
        comment[0]=buf[0];
        if(strncmp(comment,"#",1)==0){
            //printf("Comment!:%s\n",buf);
        }
        else{
            ret=strtok(buf,"=");
            if(strcmp(ret,keyword)==0){
                /*offset buf by keyword size + 1 for the "="*/
                /* Second argument in the strncpy function is the address !!! */
                /* Note: current version of code can only take dropType =0 or 1 */
                strncpy(buf1, buf+strlen(keyword)+1, bufLen);
                printf("%30s: ",keyword);
                if(*dropType ==0){
                    getFromString(buf1,&n);
                    dropPara.push_back(n);
                }else{
                    std::istringstream iss(buf1) ;
                    std::string token;
                    while(std::getline(iss,token,',')){
                        T arg = std::stod(token);
                        dropPara.push_back(arg);
                    }
                    printf("%.3f,%.3f\n",dropPara[0],dropPara[1]);
                }
                rewind(input);
                return(0);
            }
        }
    }
    rewind(input);
    return(-1);
}

/* Specialization for string */
template<>
inline int parseDropSpec<std::string>(FILE* input, const char* keyword, const size_t bufLen,const int* dropType, std::vector<std::string>& dropPara){
    char buf[bufLen];
    char buf1[bufLen];
    char comment[1];
    char *ret;

    while (fgets(buf,bufLen, input)!=NULL){
        buf[strlen(buf)-1] = '\0' ;
        comment[0]=buf[0];
        if(strncmp(comment,"#",1)==0){
            //printf("Comment!:%s\n",buf);
        }
        else{
            ret=strtok(buf,"=");
            if(strcmp(ret,keyword)==0){
                /*offset buf by keyword size + 1 for the "="*/
                /* Second argument in the strncpy function is the address !!! */
                /* Note: current version of code can only take dropType =0 or 1 */
                strncpy(buf1, buf+strlen(keyword)+1, bufLen);
                printf("%30s: ",keyword);
                if(*dropType ==0){
                    /* Convert from char* to string */
                    //printf("buf1 is %s!\n",buf1) ;
                    std::string str;
                    for(int i=0;i< strlen(buf1);i++){
                        if(buf1[i] != ' '){
                            str += buf1[i] ;
                        }
                    }
//                    printf("str is :%s!\n",str.c_str()) ;
                    dropPara.push_back(str);
                    printf("%s.\n",dropPara[0].c_str());
                }else{
                    std::istringstream iss(buf1) ;
                    std::string token;
                    while(std::getline(iss,token,',')){
                        //std::stod(token);
                        dropPara.push_back(token);
                    }
                    printf("%s!,%s!\n",dropPara[0].c_str(),dropPara[1].c_str());
                }
                rewind(input);
                return(0);
            }
        }
    }
    rewind(input);
    return(-1);
}
