package org.implab.gradle.variants.sources.internal;

import org.implab.gradle.variants.sources.VariantSourcesExtension.LateConfigurationPolicySpec;

public class DefaultLateConfigurationPolicySpec implements LateConfigurationPolicySpec {

    private LateConfigurationMode policyMode = LateConfigurationMode.FAIL;
    private boolean policyApplied = false;

    public LateConfigurationMode mode() {
        finalizePolicy();
        return policyMode;
    }

    public void finalizePolicy() {
        policyApplied = true;
    }

    @Override
    public void failOnLateConfiguration() {
        assertApplyOnce();
        policyMode = LateConfigurationMode.FAIL;
    }

    @Override
    public void warnOnLateConfiguration() {
        assertApplyOnce();
        policyMode = LateConfigurationMode.WARN;
    }

    @Override
    public void allowLateConfiguration() {
        assertApplyOnce();
        policyMode = LateConfigurationMode.APPLY;
    }

    private void assertApplyOnce() {
        if (policyApplied)
            throw new IllegalStateException("Lazy configuration policy already applied");
        policyApplied = true;
    }

}
