/*
 * Copyright © 2016-2018 European Support Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import React from 'react';
import TestUtils from 'react-dom/test-utils';
import {scryRenderedDOMComponentsWithTestId} from 'test-utils/Util.js';
import deepFreeze from 'deep-freeze';
import mockRest from 'test-utils/MockRest.js';
import {cloneAndSet} from 'test-utils/Util.js';
import {storeCreator} from 'sdc-app/AppStore.js';

import {actionsEnum as vcActionsEnum} from 'nfvo-components/panel/versionController/VersionControllerConstants.js';
import Configuration from 'sdc-app/config/Configuration.js';
import {mapStateToProps, mapActionsToProps} from 'sdc-app/onboarding/revisions/Revisions.js';
import RevisionsView from 'sdc-app/onboarding/revisions/RevisionsView.jsx';
import RevisionsActionHelper from 'sdc-app/onboarding/revisions/RevisionsActionHelper.js';
import {RevisionsPagePropsFactory} from 'test-utils/factories/revisions/RevisionsFactories.js';
import {UserFactory} from 'test-utils/factories/users/UsersFactories.js';
import VersionFactory from 'test-utils/factories/common/VersionFactory.js';
import {screenTypes} from 'sdc-app/onboarding/OnboardingConstants.js';
import ReactTestUtils from 'react-dom/test-utils';
import {enums} from 'sdc-app/onboarding/OnboardingConstants.js';

const state = {};
state.revisions = RevisionsPagePropsFactory.buildList(2);
state.users = {usersList : UserFactory.buildList(2)};
state.revisions[0].user = state.users.usersList[0].userId;
state.revisions[1].user = state.users.usersList[1].userId;


describe('Revision List Tests', () => {
	/*
	it ('mapStateToProps mapper exists', () => {

		expect(mapStateToProps).toBeTruthy();

	});

	it ('should have state in props', () => {

		const props = mapStateToProps(state);
		expect(props.revisions.length).toEqual(2);

	});

	it('simple jsx test', () => {

		const store = storeCreator();
		const dispatch = store.dispatch;

		const props = Object.assign({}, mapStateToProps(state), mapActionsToProps(dispatch, {}));
		const renderer = TestUtils.createRenderer();
		renderer.render(<RevisionsView {...props} />);

		const renderedOutput = renderer.getRenderOutput();
		expect(renderedOutput).toBeTruthy();

	});

	it('get list data', () => {

		const store = storeCreator();
		const dispatch = store.dispatch;

		const props = Object.assign({}, mapStateToProps(state), mapActionsToProps(dispatch, {}));

		let revisionsView = TestUtils.renderIntoDocument(
			<RevisionsView {...props} />
		);
		let list = scryRenderedDOMComponentsWithTestId(revisionsView,'revision-list-item');
		expect(list.length).toEqual(props.revisions.length);
		let revert = scryRenderedDOMComponentsWithTestId(revisionsView,'form-submit-button');
		expect(revert[0].innerHTML).toEqual('Revert');
		let date = scryRenderedDOMComponentsWithTestId(revisionsView,'revision-date');
		expect(date.length).toEqual(props.revisions.length);
		expect(date[0].children[0].className).toEqual('revision-date');
		expect(date[0].children[1].className).toEqual('revision-time');
		let user = ReactTestUtils.scryRenderedDOMComponentsWithClass(revisionsView, 'svg-icon-label');
		expect(user[0].innerHTML).toEqual(state.users.usersList[0].fullName);
		expect(user[1].innerHTML).toEqual(state.users.usersList[1].fullName);
		let message = scryRenderedDOMComponentsWithTestId(revisionsView,'revision-message');
		expect(message[0].children[0].innerHTML).toEqual('Show Message With More Mock');
		expect(message[1].children[0].innerHTML).toEqual('Show Message Mock');
	});
*/

});


describe('Revisions Action Helper', () => {
	let store, dispatch, restPrefix = '', revisions, version;
	let itemId = 'testRevisionId';

	beforeAll(() => {
		restPrefix = Configuration.get('restPrefix');
		store = storeCreator();
		dispatch = store.dispatch;
		deepFreeze(store.getState());
		revisions = RevisionsPagePropsFactory.buildList(2);
		version = VersionFactory.build();
	});

	beforeEach(() => {
		mockRest.resetQueue();
	});



	it('Get revisions list', () => {

		const expectedStore = cloneAndSet(store.getState(), 'revisions', revisions);
		mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
			expect(baseUrl).toEqual(`${restPrefix}/v1.0/items/${itemId}/versions/${version.id}/revisions`);
			expect(data).toEqual(undefined);
			expect(options).toEqual(undefined);
			return {results: revisions};
		});

		return RevisionsActionHelper.fetchRevisions(dispatch, {itemId, version}).then(() => {
			expect(store.getState()).toEqual(expectedStore);
			expect(store.getState().revisions.length).toEqual(2);
		});
	});
/*
	it('Revert to revision software product model', () => {
		mockRest.resetQueue();
		let revisionId = revisions[1].id;
		mockRest.addHandler('put', ({options, data, baseUrl}) => {
			expect(baseUrl).toEqual(`${restPrefix}/v1.0/items/${itemId}/versions/${version.id}/actions`);
			expect(data).toEqual({
				action: vcActionsEnum.REVERT,
				revisionRequest: {
					revisionId: revisionId
				}

			});
			expect(options).toEqual(undefined);
			return {results: {}};
		});

		mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
			expect(baseUrl).toEqual(`${restPrefix}/v1.0/items/${itemId}/versions/${version.id}`);
			expect(data).toEqual(undefined);
			expect(options).toEqual(undefined);
			return {results: {}};
		});

		mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
			expect(baseUrl).toEqual(`${restPrefix}/v1.0/vendor-software-products/${itemId}/versions/${version.id}`);
			expect(data).toEqual(undefined);
			expect(options).toEqual(undefined);
			return {results: {}};
		});



		mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
			expect(baseUrl).toEqual(`${restPrefix}/v1.0/vendor-software-products/${itemId}/versions/${version.id}/questionnaire`);
			expect(data).toEqual(undefined);
			expect(options).toEqual(undefined);
			return {data: JSON.stringify({}), schema: JSON.stringify({})};
		});

		mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
			expect(baseUrl).toEqual(`${restPrefix}/v1.0/vendor-software-products/${itemId}/versions/${version.id}/components`);
			expect(data).toEqual(undefined);
			expect(options).toEqual(undefined);
			return {results: {}};
		});


		return RevisionsActionHelper.revertToRevision(dispatch, {itemId, version,  revisionId, itemType: screenTypes.SOFTWARE_PRODUCT}).then(() => {
		});

	});
*/
	/*it('Revert to revision license model', () => {

		let revisionId = revisions[0].id;
		mockRest.addHandler('put', ({options, data, baseUrl}) => {
			expect(baseUrl).toEqual(`${restPrefix}/v1.0/items/${itemId}/versions/${version.id}/actions`);
			expect(data).toEqual({
				action: vcActionsEnum.REVERT,
				revisionRequest: {
					revisionId: revisionId
				}

			});
			expect(options).toEqual(undefined);
			return {results: {}};
		});

		mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
			expect(baseUrl).toEqual(`${restPrefix}/v1.0/items/${itemId}/versions/${version.id}`);
			expect(data).toEqual(undefined);
			expect(options).toEqual(undefined);
			return {results: {}};
		});

		mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
			expect(baseUrl).toEqual(`${restPrefix}/v1.0/vendor-license-models/${itemId}/versions/${version.id}`);
			expect(data).toEqual(undefined);
			expect(options).toEqual(undefined);
			return {results: {}};
		});


		let vlmFetched = ['license-agreements', 'feature-groups', 'entitlement-pools', 'license-key-groups'];
		vlmFetched.forEach(fetchCall => {
			mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
				expect(baseUrl).toEqual(`${restPrefix}/v1.0/vendor-license-models/${itemId}/versions/${version.id}/` + fetchCall);
				expect(data).toEqual(undefined);
				expect(options).toEqual(undefined);
				return {results: {}};
			});
		});


		return RevisionsActionHelper.revertToRevision(dispatch, {itemId, version,  revisionId, itemType: screenTypes.LICENSE_MODEL}).then(() => {
		});

	});*/




});