import { inject, injectable, } from 'inversify';
import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';

import { TCustomNodeFactory } from '../../../types/container/custom-nodes/TCustomNodeFactory';
import { TIdentifierNamesGeneratorFactory } from '../../../types/container/generators/TIdentifierNamesGeneratorFactory';
import { TInitialData } from '../../../types/TInitialData';
import { TNodeWithStatements } from '../../../types/node/TNodeWithStatements';

import { ICallsGraphData } from '../../../interfaces/analyzers/calls-graph-analyzer/ICallsGraphData';
import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
import { IOptions } from '../../../interfaces/options/IOptions';
import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
import { IStringArrayStorage } from '../../../interfaces/storages/string-array-storage/IStringArrayStorage';

import { initializable } from '../../../decorators/Initializable';

import { CustomNode } from '../../../enums/custom-nodes/CustomNode';
import { ObfuscationEvent } from '../../../enums/event-emitters/ObfuscationEvent';

import { AbstractCustomNodeGroup } from '../../AbstractCustomNodeGroup';
import { NodeAppender } from '../../../node/NodeAppender';
import { StringArrayNode } from '../StringArrayNode';
import { StringArrayCallsWrapper } from '../StringArrayCallsWrapper';
import { StringArrayRotateFunctionNode } from '../StringArrayRotateFunctionNode';

@injectable()
export class StringArrayCustomNodeGroup extends AbstractCustomNodeGroup {
    /**
     * @type {ObfuscationEvent}
     */
    protected appendEvent: ObfuscationEvent = ObfuscationEvent.AfterObfuscation;

    /**
     * @type {Map<CustomNode, ICustomNode>}
     */
    @initializable()
    protected customNodes!: Map <CustomNode, ICustomNode>;

    /**
     * @type {TCustomNodeFactory}
     */
    private readonly customNodeFactory: TCustomNodeFactory;

    /**
     * @type {IStringArrayStorage}
     */
    private readonly stringArrayStorage: IStringArrayStorage;

    /**
     * @param {TCustomNodeFactory} customNodeFactory
     * @param {IStringArrayStorage} stringArrayStorage
     * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
     * @param {IRandomGenerator} randomGenerator
     * @param {IOptions} options
     */
    constructor (
        @inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
        @inject(ServiceIdentifiers.TStringArrayStorage) stringArrayStorage: IStringArrayStorage,
        @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
            identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
        @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
        @inject(ServiceIdentifiers.IOptions) options: IOptions
    ) {
        super(identifierNamesGeneratorFactory, randomGenerator, options);

        this.customNodeFactory = customNodeFactory;
        this.stringArrayStorage = stringArrayStorage;
    }

    /**
     * @param {TNodeWithStatements} nodeWithStatements
     * @param {ICallsGraphData[]} callsGraphData
     */
    public appendCustomNodes (nodeWithStatements: TNodeWithStatements, callsGraphData: ICallsGraphData[]): void {
        if (!this.stringArrayStorage.getLength()) {
            return;
        }

        // stringArrayNode append
        this.appendCustomNodeIfExist(CustomNode.StringArrayNode, (customNode: ICustomNode) => {
            NodeAppender.prepend(nodeWithStatements, customNode.getNode());
        });

        // stringArrayCallsWrapper append
        this.appendCustomNodeIfExist(CustomNode.StringArrayCallsWrapper, (customNode: ICustomNode) => {
            NodeAppender.insertAtIndex(nodeWithStatements, customNode.getNode(), 1);
        });

        // stringArrayRotateFunctionNode append
        this.appendCustomNodeIfExist(CustomNode.StringArrayRotateFunctionNode, (customNode: ICustomNode) => {
            NodeAppender.insertAtIndex(nodeWithStatements, customNode.getNode(), 1);
        });
    }

    public initialize (): void {
        this.customNodes = new Map <CustomNode, ICustomNode>();

        if (!this.options.stringArray) {
            return;
        }

        const stringArrayNode: ICustomNode<TInitialData<StringArrayNode>> =
            this.customNodeFactory(CustomNode.StringArrayNode);
        const stringArrayCallsWrapper: ICustomNode<TInitialData<StringArrayCallsWrapper>> =
            this.customNodeFactory(CustomNode.StringArrayCallsWrapper);
        const stringArrayRotateFunctionNode: ICustomNode<TInitialData<StringArrayRotateFunctionNode>> =
            this.customNodeFactory(CustomNode.StringArrayRotateFunctionNode);

        const stringArrayName: string = this.stringArrayStorage.getStorageName();
        const stringArrayCallsWrapperName: string = this.stringArrayStorage.getStorageCallsWrapperName();
        const stringArrayRotationAmount: number = this.stringArrayStorage.getRotationAmount();

        stringArrayNode.initialize(this.stringArrayStorage, stringArrayName);
        stringArrayCallsWrapper.initialize(stringArrayName, stringArrayCallsWrapperName);
        stringArrayRotateFunctionNode.initialize(stringArrayName, stringArrayRotationAmount);

        this.customNodes.set(CustomNode.StringArrayNode, stringArrayNode);
        this.customNodes.set(CustomNode.StringArrayCallsWrapper, stringArrayCallsWrapper);

        if (this.options.rotateStringArray) {
            this.customNodes.set(CustomNode.StringArrayRotateFunctionNode, stringArrayRotateFunctionNode);
        }
    }
}
