#!/usr/bin/env python3
"""
Fitting experimental data: spherical nanoparticles with size distribution
in 3 layers system (experiment at GALAXI).
"""
import os
import bornagain as ba
from bornagain import deg, nm, ba_fitmonitor, R3
from matplotlib import pyplot as plt

radius = 5.75*ba.nm
sigma = 0.4
distance = 53.6*ba.nm
disorder = 10.5*ba.nm
kappa = 17.5
ptfe_thickness = 22.1*ba.nm
hmdso_thickness = 18.5*ba.nm


def get_sample():

    # defining materials
    vacuum = ba.Vacuum()
    si_color = (0.30, 0.62, 0.86)
    si_mat = ba.RefractiveMaterial("Si", si_color, 5.7816e-6, 1.0229e-7)
    ag_color = (0.86, 0.24, 0.18)
    ag_mat = ba.RefractiveMaterial("Ag", ag_color, 2.2475e-5, 1.6152e-6)
    ptfe_color = (0.93, 0.72, 0.25)
    ptfe_mat = ba.RefractiveMaterial("PTFE", ptfe_color, 5.20509e-6, 1.9694e-8)
    hmdso_color = (0.25, 0.65, 0.35)
    hmdso_mat = ba.RefractiveMaterial("HMDSO", hmdso_color, 2.0888e-6, 1.3261e-8)

    # collection of particles with size distribution
    nparticles = 20
    nfwhm = 2.0
    ff = ba.Sphere(radius)
    sphere = ba.Particle(ag_mat, ff)
    position = R3(0, 0, -1*hmdso_thickness)
    sphere.translate(position)
    #     ln_distr = ba.DistributionLogNormal(radius, sigma)
    #     par_distr = ba.ParameterDistribution(
    #         "/Particle/Sphere/Radius", ln_distr, nparticles, nfwhm,
    #     part_coll = ba.ParticleDistribution(sphere, par_distr)

    # interference function
    layout = ba.RadialParacrystal(sphere, distance, 1e6*ba.nm)
    layout.setKappa(kappa)
    layout.setDomainSize(2e4*nm)
    profile = ba.Profile1DGauss(disorder)
    layout.setProbabilityDistribution(profile)

    # roughness
    r_ptfe = ba.Roughness(2.3*ba.nm, 0.3, 5*ba.nm)
    r_hmdso = ba.Roughness(1.1*ba.nm, 0.3, 5*ba.nm)

    # layers
    vacuum_layer = ba.Layer(vacuum)
    hmdso_layer = ba.Layer(hmdso_mat, hmdso_thickness, r_hmdso)
    hmdso_layer.deposit2D(layout)
    ptfe_layer = ba.Layer(ptfe_mat, ptfe_thickness, r_ptfe)
    substrate_layer = ba.Layer(si_mat)

    # assembling sample
    sample = ba.Sample()
    sample.addLayer(vacuum_layer)
    sample.addLayer(hmdso_layer)
    sample.addLayer(ptfe_layer)
    sample.addLayer(substrate_layer)

    return sample
