package com.nisum.mytime.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.stereotype.Service;

import com.nisum.mytime.model.MasterData;
import com.nisum.mytime.repository.LocationRepo;
import com.nisum.mytime.repository.MasterDataRepo;

@Service
public class DataValidations {
	public static boolean validateNumber(String number) {
		boolean flag = false;
		if(null != number && !MyTimeUtils.EMPTY_STRING.equals(number.trim())) {
			flag = number.matches("^\\d+$") && isNumberInRange(number.trim());
		}
		return flag;
	}
	
	public static boolean isNumberInRange(String number) {
		long empId = Long.parseLong(number);
 		return (empId >= MyTimeUtils.EMPID_START && empId<= MyTimeUtils.EMPID_END);
	}
		
	 public static boolean validateName(String name) {
    	boolean flag = false;
    	if(null != name && !MyTimeUtils.EMPTY_STRING.equals(name.trim())) {
			String regx =  "^[\\p{L} .'-]+$";
		    Pattern pattern = Pattern.compile(regx,Pattern.CASE_INSENSITIVE);
		    Matcher matcher = pattern.matcher(name.trim());
		    flag = matcher.find();
    	}
		return flag;
	}
	 
				
	public static boolean isValidGender(String gender) {
		boolean flag =  false;
		if( null != gender && !MyTimeUtils.EMPTY_STRING.equals(gender.trim())) {
			flag = gender.trim().equalsIgnoreCase(MyTimeUtils.MALE) || gender.equalsIgnoreCase(MyTimeUtils.FEMALE);
		}
		return flag;
	}
			
	public static boolean validateEmail(String email) {
		boolean flag = false;
		if(null != email && !MyTimeUtils.EMPTY_STRING.equals(email)) {
			if(email.trim().endsWith("@nisum.com")) {
				String regx =  "^[\\p{L} .'-]+$";
				email = email.substring(0, email.indexOf("@")-1);
				Pattern pattern = Pattern.compile(regx,Pattern.CASE_INSENSITIVE);
			    Matcher matcher = pattern.matcher(email.trim());
			    flag = matcher.find();
			}
		}
		return flag;
	}
			
	public  static boolean isYesOrNo(String value) {
		boolean flag = false;
		if(null != value && !MyTimeUtils.EMPTY_STRING.equals(value.trim())) {
			value = value.trim();
			flag = value.equalsIgnoreCase(MyTimeUtils.YES) || value.equalsIgnoreCase(MyTimeUtils.STRING_Y) || value.equalsIgnoreCase(MyTimeUtils.NO) || value.equalsIgnoreCase(MyTimeUtils.STRING_N);
		}
		return flag;
	}
		
	public  static boolean isValidFunctionalGroup(String functionalGroup,MasterDataRepo masterDataRepo) {
		boolean flag = false;
		if(null != functionalGroup && !MyTimeUtils.EMPTY_STRING.equals(functionalGroup.trim())) {
			List<MasterData> fsData = masterDataRepo.findByMasterDataTypeAndMasterDataNameAndActiveStatus(MyTimeUtils.MASTERDATA_FG, functionalGroup.trim(), true);
			flag = fsData.size() > MyTimeUtils.INT_ZERO;
		}
		return flag;
	}
		
	public  static boolean isValidDesignation(String designation, MasterDataRepo masterDataRepo) {
		boolean flag = false;
		if(null != designation && !MyTimeUtils.EMPTY_STRING.equals(designation.trim())) {
			List<MasterData> designationData = masterDataRepo.findByMasterDataTypeAndMasterDataNameAndActiveStatus(MyTimeUtils.MASTERDATA_DESIGNATION, designation.trim(), true);
			flag = designationData.size() > MyTimeUtils.INT_ZERO;
		}
		return flag;
	}
		
		
	public  static boolean isValidEmploymentType(String employmentType, MasterDataRepo masterDataRepo) {
		boolean flag = false;
		if(null != employmentType && MyTimeUtils.EMPTY_STRING.equals(employmentType)){
			flag = true;
		}else if(null != employmentType && !MyTimeUtils.EMPTY_STRING.equals(employmentType.trim())) {
			List<MasterData> empTypeData = masterDataRepo.findByMasterDataTypeAndMasterDataNameAndActiveStatus(MyTimeUtils.MASTERDATAD_EMLOYMENT_TYPE, employmentType.trim(), true);
			flag = empTypeData.size() > MyTimeUtils.INT_ZERO ;
		}
		return flag;
	}
		
	public  static boolean isValidRole(String role,MasterDataRepo masterDataRepo) {
		boolean flag = false;
		if(null != role && MyTimeUtils.EMPTY_STRING.equals(role)){
			flag = true;
		}else if(null != role && !MyTimeUtils.EMPTY_STRING.equals(role.trim().trim())) {
			List<MasterData> roleData = masterDataRepo.findByMasterDataTypeAndMasterDataNameAndActiveStatus(MyTimeUtils.MASTERDATA_ROLES, role.trim(), true);
			flag = roleData.size() > MyTimeUtils.INT_ZERO;
		}
		return flag;
	}
		
	public  static boolean isFutureDate(Date date) {
		boolean flag = false;
		if(null != date) {
			flag = new Date().after(date) ;
		}
		return flag;
	}
	
	public  static boolean isValidDate(Date date) {
		return null != date;
	}
	
	public  static boolean isAgeGreaterThanTwenty(Date dob, Date doj) {
		boolean flag = false;
		if( null != dob && null != doj) {
			Calendar dojCal = Calendar.getInstance();
			dojCal.setTime(doj);
			Calendar dobCal = Calendar.getInstance();
			dobCal.setTime(dob);
			flag = dojCal.get(Calendar.YEAR)  - dobCal.get(Calendar.YEAR) > MyTimeUtils.INT_TWENTY;
		}
		return flag;
	}
	
	
	public  static boolean isValidWorkLocation(String workLocation, LocationRepo locationRepo) {
		boolean flag = false;
		if(null != workLocation && !MyTimeUtils.EMPTY_STRING.equals(workLocation.trim())) {
			flag= locationRepo.findByLocationAndActiveStatus(workLocation.trim(), true). size() > MyTimeUtils.INT_ZERO ;
		}
		return flag;
	}
	
	public static boolean isActive(String active) {
		boolean flag = false;
		if(null != active && MyTimeUtils.EMPTY_STRING.equals(active.trim())) {
			flag =  MyTimeUtils.ACTIVE.equalsIgnoreCase(active.trim());
		}
		return flag;
	}
}
