/*!
 * 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 deepFreeze from 'deep-freeze';
import mockRest from 'test-utils/MockRest.js';
import { storeCreator } from 'sdc-app/AppStore.js';
import { cloneAndSet } from 'test-utils/Util.js';
import LicenseModelActionHelper from 'sdc-app/onboarding/licenseModel/LicenseModelActionHelper.js';
import LicenseModelCreationActionHelper from 'sdc-app/onboarding/licenseModel/creation/LicenseModelCreationActionHelper.js';
import {
    LicenseModelPostFactory,
    LicenseModelDispatchFactory,
    LicenseModelStoreFactory
} from 'test-utils/factories/licenseModel/LicenseModelFactories.js';
import VersionFactory from 'test-utils/factories/common/VersionFactory.js';
import { default as CurrentScreenFactory } from 'test-utils/factories/common/CurrentScreenFactory.js';
import { actionsEnum as VersionControllerActionsEnum } from 'nfvo-components/panel/versionController/VersionControllerConstants.js';
import { SyncStates } from 'sdc-app/common/merge/MergeEditorConstants.js';
import { itemTypes } from 'sdc-app/onboarding/versionsPage/VersionsPageConstants.js';

describe('License Model Module Tests', function() {
    it('Add License Model', () => {
        const store = storeCreator();
        deepFreeze(store.getState());

        const licenseModelPostRequest = LicenseModelPostFactory.build();

        const licenseModelToAdd = LicenseModelDispatchFactory.build();

        const licenseModelIdFromResponse = 'ADDED_ID';

        mockRest.addHandler('post', ({ options, data, baseUrl }) => {
            expect(baseUrl).toEqual(
                '/onboarding-api/v1.0/vendor-license-models/'
            );
            expect(data).toEqual(licenseModelPostRequest);
            expect(options).toEqual(undefined);
            return {
                value: licenseModelIdFromResponse
            };
        });

        return LicenseModelCreationActionHelper.createLicenseModel(
            store.dispatch,
            {
                licenseModel: licenseModelToAdd
            }
        ).then(response => {
            expect(response.value).toEqual(licenseModelIdFromResponse);
        });
    });

    it('Validating readonly screen after submit', () => {
        const version = VersionFactory.build({}, { isCertified: false });
        const itemPermissionAndProps = CurrentScreenFactory.build(
            {},
            { version }
        );
        const licenseModel = LicenseModelStoreFactory.build();
        deepFreeze(licenseModel);

        const store = storeCreator({
            currentScreen: { ...itemPermissionAndProps },
            licenseModel: {
                licenseModelEditor: { data: licenseModel }
            }
        });
        deepFreeze(store.getState());

        const certifiedVersion = {
            ...itemPermissionAndProps.props.version,
            status: 'Certified'
        };

        const expectedCurrentScreenProps = {
            itemPermission: {
                ...itemPermissionAndProps.itemPermission,
                isCertified: true
            },
            props: {
                isReadOnlyMode: true,
                version: certifiedVersion
            }
        };
        const expectedSuccessModal = {
            cancelButtonText: 'OK',
            msg: 'This license model successfully submitted',
            timeout: 2000,
            title: 'Submit Succeeded',
            type: 'info'
        };

        const versionsList = {
            itemType: itemTypes.LICENSE_MODEL,
            itemId: licenseModel.id,
            versions: [{ ...certifiedVersion }]
        };

        let expectedStore = store.getState();
        expectedStore = cloneAndSet(
            expectedStore,
            'currentScreen.itemPermission',
            expectedCurrentScreenProps.itemPermission
        );
        expectedStore = cloneAndSet(
            expectedStore,
            'currentScreen.props',
            expectedCurrentScreenProps.props
        );
        expectedStore = cloneAndSet(
            expectedStore,
            'modal',
            expectedSuccessModal
        );
        expectedStore = cloneAndSet(
            expectedStore,
            'versionsPage.versionsList',
            versionsList
        );

        mockRest.addHandler('put', ({ data, options, baseUrl }) => {
            expect(baseUrl).toEqual(
                `/onboarding-api/v1.0/vendor-license-models/${
                    licenseModel.id
                }/versions/${version.id}/actions`
            );
            expect(data).toEqual({
                action: VersionControllerActionsEnum.SUBMIT
            });
            expect(options).toEqual(undefined);
            return { returnCode: 'OK' };
        });

        mockRest.addHandler('put', ({ data, options, baseUrl }) => {
            expect(baseUrl).toEqual(
                `/onboarding-api/v1.0/vendor-license-models/${
                    licenseModel.id
                }/versions/${version.id}/actions`
            );
            expect(data).toEqual({
                action: VersionControllerActionsEnum.CREATE_PACKAGE
            });
            expect(options).toEqual(undefined);
            return { returnCode: 'OK' };
        });

        mockRest.addHandler('fetch', ({ data, options, baseUrl }) => {
            expect(baseUrl).toEqual(
                `/onboarding-api/v1.0/items/${licenseModel.id}/versions/${
                    version.id
                }`
            );
            expect(data).toEqual(undefined);
            expect(options).toEqual(undefined);
            return {
                ...certifiedVersion,
                state: {
                    synchronizationState: SyncStates.UP_TO_DATE,
                    dirty: false
                }
            };
        });

        mockRest.addHandler('fetch', ({ data, options, baseUrl }) => {
            expect(baseUrl).toEqual(
                `/onboarding-api/v1.0/items/${licenseModel.id}`
            );
            expect(data).toEqual(undefined);
            expect(options).toEqual(undefined);
            return { ...certifiedVersion };
        });

        mockRest.addHandler('fetch', ({ data, options, baseUrl }) => {
            expect(baseUrl).toEqual(
                `/onboarding-api/v1.0/items/${licenseModel.id}/versions`
            );
            expect(data).toEqual(undefined);
            expect(options).toEqual(undefined);
            return { results: [{ ...certifiedVersion }] };
        });

        return LicenseModelActionHelper.performSubmitAction(store.dispatch, {
            licenseModelId: licenseModel.id,
            version
        }).then(() => {
            expect(store.getState()).toEqual(expectedStore);
        });
    });
});
