import i18n from '~/locale/i18n';

const EMAIL_REGEXP = /^[_a-z0-9-]+([.+][_a-z0-9-]+)*@(?!mynixplay\.com$)([a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$)/i;
const URL_REGEXP = /([a-zA-Z\d]+:\/\/)?((\w+:\w+@)?([a-zA-Z\d.-]+\.[A-Za-z]{2,4})(:\d+)?(\/.*)?)/i;
const SYMBOL_REGEXP = /[^&/:$%<>]*/;
const CUSTOM_EMAIL_REGEXP = /[:+:-@-_.A-Za-z0-9]*/;
const USERNAME_REGEXP = /[-_.A-Za-z0-9]*/;
const PASSWORD_REGEXP = /^(?:(?=.*[a-z])(?:(?=.*[A-Z])(?=.*[\d\W])|(?=.*\W)(?=.*\d))|(?=.*\W)(?=.*[A-Z])(?=.*\d)).{8,32}$/;

export const defaultConstrains = {
	required: {
		presence: { allowEmpty: false, message: i18n.t('common.fieldRequired') },
	},
};

export const createConstrains = (fields = []) => {
	if (fields.length === 0) return {};
	const userNameConstrain = (fieldName, maxLength) => ({
		presence: { allowEmpty: false, message: i18n.t(`users.${fieldName}-required`) },
		length: { maximum: maxLength, tooLong: i18n.t(`users.${fieldName}-tooLong`) },
		not: {
			validator: 'format',
			pattern: URL_REGEXP,
			message: i18n.t(`users.${fieldName}-url`),
		},
		format: {
			validator: 'format',
			pattern: SYMBOL_REGEXP,
			message: i18n.t(`users.${fieldName}-symbol`),
		},
	});

	const definitions = {
		fullName: {
			presence: { allowEmpty: false, message: 'Full name is required' },
		},
		mobileNumber: {
			length: { minimum: 13, maximum: 13, tooShort: 'Mobile number must be 10 characters' },
			presence: { allowEmpty: false, message: 'Mobile number is required' },
		},
		verificationCode: {
			length: { minimum: 6, maximum: 6, tooShort: 'Verification code must be 6 characters' },
			presence: { allowEmpty: false, message: 'Verification code is required' },
		},
		firstName: userNameConstrain('firstName', 30),
		lastName: userNameConstrain('lastName', 30),
		friendName: {
			presence: { allowEmpty: false, message: 'friendName' },
			length: { maximum: 64, tooLong: 'friendName' },
			format: {
				validator: 'format',
				pattern: SYMBOL_REGEXP,
				message: 'friendName',
			},
		},
		playlistName: {
			presence: { allowEmpty: false, message: i18n.t('common.fieldRequired') },
			length: { maximum: 100, tooLong: i18n.t('common.maxCharacters', { length: 100 }) },
		},
		albumName: {
			presence: { allowEmpty: false, message: i18n.t('common.fieldRequired') },
			length: { maximum: 100, tooLong: i18n.t('common.maxCharacters', { length: 100 }) },
		},
		email: {
			presence: { allowEmpty: false, message: i18n.t('users.email-required') },
			format: {
				pattern: EMAIL_REGEXP,
				message: i18n.t('users.email-invalid'),
			},
		},
		newEmail: {
			presence: { allowEmpty: false, message: i18n.t('users.email-required') },
			format: {
				pattern: EMAIL_REGEXP,
				message: i18n.t('users.email-invalid'),
			},
		},
		confirmNewEmail: {
			presence: { allowEmpty: false, message: i18n.t('users.email-required') },
			format: {
				pattern: EMAIL_REGEXP,
				message: i18n.t('users.email-invalid'),
			},
			equality: {
				attribute: 'newEmail',
				message: i18n.t('users.theEmailDoNotMatch'),
			},
		},
		username: {
			presence: { allowEmpty: false, message: i18n.t('users.username-required') },
			length: { minimum: 4, tooShort: i18n.t('users.username-tooShort') },
			format: { pattern: USERNAME_REGEXP, message: i18n.t('users.username-invalid') },
		},
		myPassword: {
			presence: { allowEmpty: false, message: i18n.t('users.password-invalid') },
			length: { minimum: 8, maximum: 32, message: i18n.t('users.password-invalid') },
			format: {
				pattern: PASSWORD_REGEXP,
				message: 'Must contain set characters',
			},
		},
		country: {
			presence: { allowEmpty: false, message: i18n.t('common.locationRequired') },
		},
		customEmail: {
			presence: { allowEmpty: false },
			length: { minimum: 4, maximum: 254, tooShort: i18n.t('users.username-tooShort') },
			format: {
				validator: 'format',
				pattern: CUSTOM_EMAIL_REGEXP,
			},
		},
	};

	const constrains = {};
	fields.forEach((f) => {
		constrains[f] = definitions[f];
	});

	return constrains;
};
