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;
		number = number.trim();
		if(!MyTimeUtils.EMPTY_STRING.equals(number)) {
			flag = number.matches("^\\d+$") && isNumberInRange(number);
		}
		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;
    	name = name.trim();
    	if(!MyTimeUtils.EMPTY_STRING.equals(name)) {
			String regx =  "^[\\p{L}]+$";
		    Pattern pattern = Pattern.compile(regx,Pattern.CASE_INSENSITIVE);
		    Matcher matcher = pattern.matcher(name);
		    flag = matcher.find();
    	}
		return flag;
	}
	 
				
	public static boolean isValidGender(String gender) {
		boolean flag =  false;
		gender = gender.trim();
		if( !MyTimeUtils.EMPTY_STRING.equals(gender)) {
			flag = gender.equalsIgnoreCase(MyTimeUtils.MALE) || gender.equalsIgnoreCase(MyTimeUtils.FEMALE) || gender.equalsIgnoreCase(MyTimeUtils.M) || gender.equalsIgnoreCase(MyTimeUtils.F);
		}
		return flag;
	}
			
	public static boolean validateEmail(String email) {
		boolean flag = false;
		email =  email.trim();
		if( !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);
			    flag = matcher.find();
			}
		}
		return flag;
	}
			
	public  static boolean isYesOrNo(String value) {
		boolean flag = false;
		value = value.trim();
		if(!MyTimeUtils.EMPTY_STRING.equals(value)) {
			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;
		functionalGroup = functionalGroup.trim();
		if(!MyTimeUtils.EMPTY_STRING.equals(functionalGroup)) {
			List<MasterData> fsData = masterDataRepo.findByMasterDataTypeAndMasterDataNameAndActiveStatus(MyTimeUtils.MASTERDATA_FG, functionalGroup, true);
			flag = fsData.size() > MyTimeUtils.INT_ZERO;
		}
		return flag;
	}
		
	public  static boolean isValidDesignation(String designation, MasterDataRepo masterDataRepo) {
		boolean flag = false;
		designation = designation.trim();
		if( !MyTimeUtils.EMPTY_STRING.equals(designation)) {
			List<MasterData> designationData = masterDataRepo.findByMasterDataTypeAndMasterDataNameAndActiveStatus(MyTimeUtils.MASTERDATA_DESIGNATION, designation, true);
			flag = designationData.size() > MyTimeUtils.INT_ZERO;
		}
		return flag;
	}
		
		
	public  static boolean isValidEmploymentType(String employmentType, MasterDataRepo masterDataRepo) {
		boolean flag = false;
		employmentType = employmentType.trim();
		if(MyTimeUtils.EMPTY_STRING.equals(employmentType)){
			flag = true;
		}else if( !MyTimeUtils.EMPTY_STRING.equals(employmentType)) {
			List<MasterData> empTypeData = masterDataRepo.findByMasterDataTypeAndMasterDataNameAndActiveStatus(MyTimeUtils.MASTERDATAD_EMLOYMENT_TYPE, employmentType, true);
			flag = empTypeData.size() > MyTimeUtils.INT_ZERO ;
		}
		return flag;
	}
		
	public  static boolean isValidRole(String role,MasterDataRepo masterDataRepo) {
		boolean flag = false;
		role =  role.trim();
		if( MyTimeUtils.EMPTY_STRING.equals(role)){
			flag = true;
		}else if(!MyTimeUtils.EMPTY_STRING.equals(role)) {
			List<MasterData> roleData = masterDataRepo.findByMasterDataTypeAndMasterDataNameAndActiveStatus(MyTimeUtils.MASTERDATA_ROLES, role, 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 (dob == null) {
			flag = true;
		} else if(null != doj) {
			Calendar cal = Calendar.getInstance();
			cal.setTime(doj);
			int dojYear =  cal.get(Calendar.YEAR);
			cal.setTime(dob);
			flag = dojYear  - cal.get(Calendar.YEAR) > MyTimeUtils.INT_TWENTY;
		}
		return flag;
	}
	
	
	public  static boolean isValidWorkLocation(String workLocation, LocationRepo locationRepo) {
		boolean flag = false;
		workLocation = workLocation.trim();
		if(!MyTimeUtils.EMPTY_STRING.equals(workLocation)) {
			flag= locationRepo.findByLocationAndActiveStatus(workLocation, true). size() > MyTimeUtils.INT_ZERO ;
		}
		return flag;
	}
	
	public static boolean isActive(String active) {
		boolean flag = false;
		active = active.trim();
		if(null != active && MyTimeUtils.EMPTY_STRING.equals(active)) {
			flag =  MyTimeUtils.ACTIVE.equalsIgnoreCase(active);
		}
		return flag;
	}
}
