const schema = require('./data/eds-c-textarea.schema.json');
const exampleJson = require('./data/eds-c-textarea.example.json');
const hintSchema = require('../eds-c-hint/data/eds-c-hint.schema.json');
const labelSchema = require('../eds-c-label/data/eds-c-label.schema.json');
const errorMessageSchema = require('../eds-c-error-message/data/eds-c-error-message.schema.json');
const handlebars = require('handlebars');

const { findTemplates } = require('../../development/tests/templateLoader.js');
const { validateSchema } = require('../../development/tests/validate-schema');
const dataAttributesSchema = require('../eds-c-data-attributes/data/eds-c-data-attributes.schema.json');
findTemplates([
	'eds-c-textarea',
	'eds-c-label',
	'eds-c-hint',
	'eds-c-error-message',
	'eds-c-data-attributes'
]).load(handlebars);

const componentTemplate = '{{> eds-c-textarea}}';
const componentHbs = handlebars.compile(componentTemplate);
const renderedComponent = componentHbs(exampleJson);

describe('Label Schema Validation and Accessibility Test', () => {
	const { configureAxe, toHaveNoViolations } = require('jest-axe');
	expect.extend(toHaveNoViolations);

	const axe = configureAxe({
		rules: {
			region: { enabled: false }
		}
	});

	test('Example data matches schema', () => {
		const valid = validateSchema(schema, exampleJson, [
			{ name: 'eds-c-data-attributes', schema: dataAttributesSchema },
			{ name: 'eds-c-label', schema: labelSchema },
			{ name: 'eds-c-hint', schema: hintSchema },
			{ name: 'eds-c-error-message', schema: errorMessageSchema }
		]);
		expect(valid).toBeTruthy();
	});

	test('Should display a label', () => {
		document.body.innerHTML = renderedComponent;
		const container = document.querySelector('.eds-c-form__field');
		const labelElement = container.querySelector('.eds-c-label');
		expect(labelElement).not.toBeNull();
		expect(labelElement.textContent.replace(/\s+/g, ' ').trim()).toContain(
			'Your content (optional)'
		);
	});

	test('Should render hint', () => {
		document.body.innerHTML = renderedComponent;

		const hintElement = document.querySelector('.eds-c-hint');
		expect(hintElement).not.toBeNull();
		expect(hintElement.textContent.trim()).toBe('What do you think?');
	});

	test('Should render error', () => {
		const testData = {
			...exampleJson,
			error: {
				text: 'Error message',
				errorInputId: 'eds-textarea-one',
				icon: '/img/icons/eds-i-error-filled-medium.svg#eds-i-error-filled-medium'
			}
		};
		document.body.innerHTML = componentHbs(testData);

		const errorElement = document.querySelector('.eds-c-error-message');
		const textAreaElement = document.querySelector('textarea');
		expect(errorElement).not.toBeNull();
		expect(errorElement.textContent.trim()).toBe('Error message');
		expect(textAreaElement.hasAttribute('invalid')).toBeTruthy();
		expect(textAreaElement.getAttribute('aria-invalid')).toBeTruthy();
		expect(textAreaElement.getAttribute('aria-describedby')).toBe(
			'error-eds-textarea-one'
		);
	});

	test('Should have the right data attributes', () => {
		const testData = {
			...exampleJson,
			dataAttributes: [
				{ name: 'track-context', value: 'springer-link' },
				{ name: 'track-content-type', value: 'collection' },
				{ name: 'track-label', value: 'collection' },
				{ name: 'test', value: 'textarea-test' }
			]
		};
		document.body.innerHTML = componentHbs(testData);

		const textAreaElement = document.querySelector('textarea');
		expect(textAreaElement.getAttribute('data-test')).toEqual('textarea-test');
		expect(textAreaElement.getAttribute('data-track-context')).toEqual(
			'springer-link'
		);
		expect(textAreaElement.getAttribute('data-track-content-type')).toEqual(
			'collection'
		);
		expect(textAreaElement.getAttribute('data-track-label')).toEqual(
			'collection'
		);
	});

	test('Should have the right attributes', () => {
		document.body.innerHTML = renderedComponent;

		const textAreaElement = document.querySelector('textarea');
		expect(textAreaElement.getAttribute('id')).toEqual('eds-amazing-content');
		expect(textAreaElement.getAttribute('name')).toEqual('content');
		expect(textAreaElement.textContent.replace(/\s+/g, ' ').trim()).toEqual(
			'content value, should I accept HTML?'
		);
		expect(textAreaElement.hasAttribute('disabled')).toBeFalsy();
	});

	test('Should have the rows/cols attributes', () => {
		const testData = {
			...exampleJson,
			rows: 5,
			cols: 5
		};
		document.body.innerHTML = componentHbs(testData);

		const textAreaElement = document.querySelector('textarea');
		expect(textAreaElement.getAttribute('rows')).toEqual('5');
		expect(textAreaElement.getAttribute('cols')).toEqual('5');
	});

	test('Should have the autocapitalize attribute', () => {
		const testData = {
			...exampleJson,
			autocapitalize: 'words'
		};
		document.body.innerHTML = componentHbs(testData);

		const textAreaElement = document.querySelector('textarea');
		expect(textAreaElement.getAttribute('autocapitalize')).toEqual('words');
	});

	test('Should have the autocomplete attribute', () => {
		const testData = {
			...exampleJson,
			autocomplete: 'on'
		};
		document.body.innerHTML = componentHbs(testData);

		const textAreaElement = document.querySelector('textarea');
		expect(textAreaElement.getAttribute('autocomplete')).toEqual('on');
	});

	test('Should have the autofocus attribute', () => {
		const testData = {
			...exampleJson,
			autofocus: true
		};
		document.body.innerHTML = componentHbs(testData);

		const textAreaElement = document.querySelector('textarea');
		expect(textAreaElement.hasAttribute('autofocus')).toBeTruthy();
	});

	test('Should have the disabled attribute', () => {
		const testData = {
			...exampleJson,
			disabled: true
		};
		document.body.innerHTML = componentHbs(testData);

		const textAreaElement = document.querySelector('textarea');
		expect(textAreaElement.hasAttribute('disabled')).toBeTruthy();
	});

	test('Should have the readonly attribute', () => {
		const testData = {
			...exampleJson,
			readonly: true
		};
		document.body.innerHTML = componentHbs(testData);

		const textAreaElement = document.querySelector('textarea');
		expect(textAreaElement.hasAttribute('readonly')).toBeTruthy();
	});

	test('Should have the placeholder attribute', () => {
		const testData = {
			...exampleJson,
			placeholder: 'my textarea placeholder'
		};
		document.body.innerHTML = componentHbs(testData);

		const textAreaElement = document.querySelector('textarea');
		expect(textAreaElement.getAttribute('placeholder')).toBe(
			'my textarea placeholder'
		);
	});

	test('Should have the tabindex attribute', () => {
		const testData = {
			...exampleJson,
			tabindex: 2
		};
		document.body.innerHTML = componentHbs(testData);

		const textAreaElement = document.querySelector('textarea');
		expect(textAreaElement.getAttribute('tabindex')).toBe('2');
	});

	test('Should have the required attribute', () => {
		const testData = {
			...exampleJson,
			required: true
		};
		document.body.innerHTML = componentHbs(testData);

		const textAreaElement = document.querySelector('textarea');
		expect(textAreaElement.hasAttribute('required')).toBeTruthy();
	});

	test('Should render inputmode attribute', () => {
		const testData = { ...exampleJson, inputmode: 'email' };
		document.body.innerHTML = componentHbs(testData);

		const textAreaElement = document.querySelector('textarea');
		expect(textAreaElement.getAttribute('inputmode')).toBe('email');
	});

	test('Should render title attribute', () => {
		const testData = { ...exampleJson, title: 'textarea title' };
		document.body.innerHTML = componentHbs(testData);

		const textAreaElement = document.querySelector('textarea');
		expect(textAreaElement.getAttribute('title')).toBe('textarea title');
	});

	test('Template passes basic HTML accessibility', async () => {
		expect(await axe(renderedComponent)).toHaveNoViolations();
	});
});
