import {BaseFrameworkAdaptor} from '../../../adaptors/framework/BaseFrameworkAdaptor';
import {BaseModule} from '../../base/Module';
import {ModuleDefinitions} from '../Module';
import {Provider} from '../Provider';
import {SimpleXdmDefinitionBuilder} from '../../../definitions/SimpleXdmDefinitionBuilder';
import {TestProvider} from './TestProvider';
import {TestSimpleXdmDefinition} from './TestSimpleXdmDefinition';
import {TestSimpleXdmDefinitionBuilder} from './TestSimpleXdmDefinitionBuilder';
import {TestSimpleXdmDefinitionImpl} from './TestSimpleXdmDefinitionImpl';


export module TestModuleDefinitions {

    export const TEST_MODULE_NAME = 'testmodule';

    export class Props implements ModuleDefinitions.Props {

        adaptor: BaseFrameworkAdaptor;
        testProvider: TestProvider;

        constructor(
                adaptor: BaseFrameworkAdaptor,
                testProvider: TestProvider) {
            this.adaptor = adaptor;
            this.testProvider = testProvider;
        }

    }

    export class State implements ModuleDefinitions.State {

        enabled: boolean;
        registered: boolean;
        testProvider: TestProvider;
        simpleXdmDefinitionBuilder: SimpleXdmDefinitionBuilder;

        constructor(testProvider: TestProvider, simpleXdmDefinition: TestSimpleXdmDefinition) {
            this.enabled = true;
            this.registered = false;
            this.testProvider = testProvider;
            this.simpleXdmDefinitionBuilder = new TestSimpleXdmDefinitionBuilder(testProvider);
        }

    }

}

/**
 * This module is used for testing.
 */
export class TestModule extends BaseModule<TestModuleDefinitions.Props, TestModuleDefinitions.State> {

    constructor(props: TestModuleDefinitions.Props) {
        super(props);
    }

    componentWillMount() {
        if (!this.props.testProvider) {
            throw new Error('this.props.testProvider is not defined');
        }
        const simpleXdmDefinition = new TestSimpleXdmDefinitionImpl(this.props.testProvider);
        this.setState(new TestModuleDefinitions.State(this.props.testProvider, simpleXdmDefinition));
    }

    public getModuleRegistrationName(): string {
        return TestModuleDefinitions.TEST_MODULE_NAME;
    }

    public getSimpleXdmDefinitionBuilder() : SimpleXdmDefinitionBuilder {
      return this.state.simpleXdmDefinitionBuilder;
    }

    getProvider(): Provider {
        return this.props.testProvider;
    }

    render(): JSX.Element | null {
        return null;
    }
}
