package com.madeu.util;

import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class ValidationCheckUtil {

	public static Boolean emptyCheck(String value){
		if(null==value||("").equals(value)||("null").equals(value)||("NULL").equals(value)||("undefined").equals(value)) {
			return false;
		}
		else{
			return true;
		}
	}
	
	public static Boolean lengthCheck(String value, int maxLength){
		int strLength = value.length();
		log.debug("strLength : "+strLength);
		
		if(strLength > maxLength) {
			return false;
		}
		else{
			return true;
		}
	}
	
	public static Boolean dateCheck(String value){
	
		try{
			int strLength = value.length();
			log.debug("strLength : "+strLength);
			
			if(strLength == 10){
				SimpleDateFormat dateFormatParser = new SimpleDateFormat("yyyy-MM-dd");
				dateFormatParser.setLenient(false);
				dateFormatParser.parse(value);
			
				return true;
			}
			else{
				return false;
			}
		
		}catch (Exception e){
			return false;
		}
	}
	
	public static Boolean dateTimeCheck(String value){
	
		try{
			int strLength = value.length();
			log.debug("strLength : "+strLength);
			
			if(strLength == 16){
				SimpleDateFormat dateFormatParser = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				dateFormatParser.setLenient(false);
				dateFormatParser.parse(value);
				
				return true;
			}
			else{
				return false;
			}
		
		}catch (Exception e){
			return false;
		}
	}
	
	public static Boolean timeCheck(String value){
	
		try{
			int strLength = value.length();
			log.debug("strLength : "+strLength);
			
			if(strLength == 5){
				SimpleDateFormat dateFormatParser = new SimpleDateFormat("HH:mm");
				dateFormatParser.setLenient(false);
				dateFormatParser.parse(value);
				
				return true;
			}
			else{
				return false;
			}
		}
		catch (Exception e){
			return false;
		}
	}

	// 최소 1개의 영문자, 숫자, 특수문자 포함하는 8 ~ 20자
	public static Boolean passwordCheck(String password){
		Pattern passwordPattern = Pattern.compile("^(?=.*[a-zA-Z])(?=.*\\d)(?=.*\\W).{8,20}$");
		Matcher passwordMatcher = passwordPattern.matcher(password);
		
		if(!passwordMatcher.find()) {
			return false;
		}
		else{
			return true;
		}
	}

	// 최소 1개의 영문자, 숫자 포함
	public static Boolean passwordCheck2(String password){
		Pattern passwordPattern = Pattern.compile("^(?=.*[a-zA-Z])(?=.*\\d)");
		Matcher passwordMatcher = passwordPattern.matcher(password);

		if(!passwordMatcher.find()) {
			return false;
		}
		else{
			return true;
		}
	}

	public static Boolean emailCheck(String email){
		Pattern emailPattern = Pattern.compile("^([0-9a-zA-Z_\\.-]+)@([0-9a-zA-Z_-]+)(\\.[0-9a-zA-Z_-]+){1,2}$");
		Matcher emailMatcher = emailPattern.matcher(email);
		
		if(!emailMatcher.find()) {
			return false;
		}
		else{
			return true;
		}
	}
	
	public static Boolean phoneNumberCheck(String phoneNumber){
		Pattern phoneNumberPattern = Pattern.compile("^[0-9]{8,11}$");
		Matcher phoneNumberMatcher = phoneNumberPattern.matcher(phoneNumber);
		
		if(!phoneNumberMatcher.find()) {
			return false;
		}
			else{
			return true;
		}
	}
	
	public static Boolean numberCheck(String number){
		Pattern numberPattern = Pattern.compile("^[0-9]+$");
		Matcher numberMatcher = numberPattern.matcher(number);
		
		if(!numberMatcher.find()) {
			return false;
		}
			else{
			return true;
		}
	}
	
	public static Boolean nickNameCheck(String nickName){
		Pattern nickNamePattern = Pattern.compile("^[a-zA-Z가-힣0-9 _]+$");
		Matcher nickNameMatcher = nickNamePattern.matcher(nickName);
		
		if(!nickNameMatcher.find()) {
			return false;
		}
			else{
			return true;
		}
	}
	
	public static Boolean buzNoCheck(String buzNo){
		String compNumber = buzNo.replace("-", "");
		
		int hap = 0;
		int temp = 0;
		int check[] = {1,3,7,1,3,7,1,3,5};  //사업자번호 유효성 체크 필요한 수
	
		if(compNumber.length() != 10)    //사업자번호의 길이가 맞는지를 확인한다.
			return false;
	
		for(int i=0; i < 9; i++){
			if(compNumber.charAt(i) < '0' || compNumber.charAt(i) > '9')  //숫자가 아닌 값이 들어왔는지를 확인한다.
				return false; 
			
			hap = hap + (Character.getNumericValue(compNumber.charAt(i)) * check[temp]); //검증식 적용
			temp++;
		}
			
		hap += (Character.getNumericValue(compNumber.charAt(8))*5)/10;
	
		if ((10 - (hap%10))%10 == Character.getNumericValue(compNumber.charAt(9))) //마지막 유효숫자와 검증식을 통한 값의 비교
			return true;
		else
			return false;
	}
	
	public static void main(String[] args){
	
		try {
			boolean emptyCheck = ValidationCheckUtil.emptyCheck("484651984561");
			boolean lengthCheck = ValidationCheckUtil.lengthCheck("길이체크", 255);
			boolean dateCheck = ValidationCheckUtil.dateCheck("2022-03-01");
			boolean dateTimeCheck = ValidationCheckUtil.dateTimeCheck("2022-03-01 12:11");
		
			log.debug("emptyCheck     : "+emptyCheck);
			log.debug("lengthCheck    : "+lengthCheck);
			log.debug("dateCheck      : "+dateCheck);
			log.debug("dateTimeCheck  : "+dateTimeCheck);
		
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}
