package com.agilex.healthcare.schedulingmanager.validator;

import com.agilex.healthcare.mobilehealthplatform.domain.ValidationError;
import com.agilex.healthcare.mobilehealthplatform.domain.ValidationResult;
import com.agilex.healthcare.mobilehealthplatform.validator.ValidatorHelper;
import com.agilex.healthcare.schedulingmanager.domain.SMPatientPreference;
import com.agilex.healthcare.utility.NullChecker;

import java.util.Arrays;
import java.util.List;

public class PreferenceValidator {
	
	public ValidationResult<SMPatientPreference> validate(SMPatientPreference pref) {
		
		ValidationResult<SMPatientPreference> validationResult = new ValidationResult<SMPatientPreference>();
		
		validateEmail(pref, validationResult);
		
		validatePhNumber(pref, validationResult);

        validatePreferenceFrequency(pref, validationResult);
		
		return validationResult;
	}

    private void validatePreferenceFrequency(SMPatientPreference pref, ValidationResult<SMPatientPreference> validationResult) {
        String[] validValues = {"Each new message", "Daily", "Never"};
        List<String> validValuesList =  Arrays.asList(validValues);
        if (!validValuesList.contains(pref.getNotificationFrequency())){
             validationResult.addIfNotNull(new ValidationError("Invalid Notification Frequency", "notificationFrequency"));
        }
    }

    private void validatePhNumber(SMPatientPreference pref,
			ValidationResult<SMPatientPreference> validationResult) {
		String phoneNumber = pref.getTextMsgPhNumber();
        if(NullChecker.isNotNullish(phoneNumber)){
            if (!ValidatorHelper.isPhoneNumberLengthValid(phoneNumber) ||
                    !ValidatorHelper.isPhoneNumberHasMinDigits(phoneNumber) ||
                    !ValidatorHelper.isPhoneNumberValidCharacters(phoneNumber)){

                validationResult.addIfNotNull(new ValidationError("Phone must have a minimum of 10 digits and only contain digits and these characters: + ( ) - . x space", "textMsgPhNumber"));
            }
        }
	}

	private void validateEmail(SMPatientPreference pref,
			ValidationResult<SMPatientPreference> validationResult) {
		validationResult.setRequestObject(pref);
		String email = pref.getEmailAddress();
        if(NullChecker.isNotNullish(email)){

            if (!ValidatorHelper.isEmailLengthValid(email)
                    || !ValidatorHelper.isEmailAddressFormatValid(email)) {
                validationResult
                        .addIfNotNull(new ValidationError(
                                "Email must be between 3 and 50 characters in a valid email format",
                                "emailAddress"));
            }
        }
    }
}
