#!/usr/bin/env python3
"""
Reflection from film on substrate with background/noise
"""
import bornagain as ba
from bornagain import nm, ba_plot as bp, deg


def get_sample():
    # create materials
    vacuum = ba.Vacuum()
    ni_color = (0.93, 0.48, 0.14)
    ni_mat = ba.SLDMaterial("Ni", ni_color, 9.4245e-06, 0)
    substrate_color = (0.28, 0.57, 0.82)
    substrate_mat = ba.SLDMaterial("SiSubstrate", substrate_color, 2.0704e-06, 0)

    # create layers
    ambient_layer = ba.Layer(vacuum)
    ni_layer = ba.Layer(ni_mat, 7*nm)
    substrate_layer = ba.Layer(substrate_mat)

    # create sample
    sample = ba.Sample()
    sample.addLayer(ambient_layer)
    sample.addLayer(ni_layer)
    sample.addLayer(substrate_layer)

    return sample


def get_simulation(sample, background):
    n = 500
    scan = ba.AlphaScan(n, 2*deg/n, 2*deg)
    scan.setWavelength(0.154*nm)
    scan.setIntensity(1e5)
    simulation = ba.SpecularSimulation(scan, sample)
    simulation.setBackground(background)
    return simulation


def simulate(background, title):
    sample = get_sample()
    simulation = get_simulation(sample, background)
    result = simulation.simulate()
    result.setTitle(title)
    return result


if __name__ == '__main__':
    ba.showSample3D(get_sample(), sample_size=120*nm, seed=0)
    results = [
        simulate(ba.ConstantBackground(2), "Constant background"),
        simulate(ba.PoissonBackground(), "Poisson noise"),
    ]
    bp.plot_multicurve(results)
    bp.plt.show()
