#!/usr/bin/env python3
"""
Magnetic cylinders in DWBA
"""
import bornagain as ba
from bornagain import ba_plot as bp, deg, R3, nm, nm2


def get_sample():
    # Materials
    vacuum = ba.Vacuum()
    substrate_color = (0.28, 0.57, 0.82)
    substrate_mat = ba.RefractiveMaterial("Substrate", substrate_color, 15e-6, 0)
    B = R3(0, 1e6, 0)
    particle_color = (0.86, 0.24, 0.18)
    particle_mat = ba.RefractiveMaterial("magParticle", particle_color, 5e-6, 0, B)

    # Particles
    ff = ba.Cylinder(5*nm, 5*nm)
    particle = ba.Particle(particle_mat, ff)

    # Multilayer
    vacuum_layer = ba.Layer(vacuum)
    vacuum_layer.deposit2D(ba.Dilute2D(0.01/nm2, particle))
    substrate_layer = ba.Layer(substrate_mat)
    sample = ba.Sample()
    sample.addLayer(vacuum_layer)
    sample.addLayer(substrate_layer)

    return sample


def get_simulation(sample, pol_dir, an_dir):
    n = 100
    beam = ba.Beam(1e9, 0.1*nm, 0.2*deg)
    detector = ba.SphericalDetector(n, -1*deg, 1*deg, n, 0, 2*deg)

    beam.setPolarization(pol_dir)
    detector.setAnalyzer(an_dir)

    return ba.ScatteringSimulation(beam, sample, detector)


def simulate(pol_dir, an_dir):
    sample = get_sample()
    simulation = get_simulation(sample, pol_dir, an_dir)
    return simulation.simulate()


if __name__ == '__main__':
    z_up = R3(0, 0, 1)
    z_dn = R3(0, 0, -1)
    results = [
        simulate(z_up, z_up),
        simulate(z_up, z_dn),
        simulate(z_dn, z_up),
        simulate(z_dn, z_dn)
    ]
    # Labels with Unicode minus sign (U+2212) instead of hyphen-minus
    labels = ['++', '+\u2212', '\u2212+', '\u2212\u2212']
    plotargs = bp.parse_commandline()
    fontsize = plotargs.get('label_fontsize', 12)
    bp.plot2d_to_grid(results, 2, fontsize=fontsize, unit_aspect=1, **plotargs)
    # Add labels inside each subplot
    fig = bp.plt.gcf()
    for i, ax in enumerate(fig.axes[:4]):
        ax.text(0.5, 0.92, labels[i], transform=ax.transAxes,
                fontsize=fontsize * 2.4, color='white', ha='center', va='top')
    bp.export(**plotargs)
