using System;
using System.Collections.ObjectModel;
using System.Windows.Forms;

namespace masseCritique2
{
public partial class Form1 : Form
{
Random R = new Random(DateTime.Now.Millisecond);

public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Collection>> sphere = Create_Sphere(float.Parse(textBox1.Text), float.Parse(textBox1.Text), int.Parse(textBox6.Text));

double Nbr_Avogadro = 6.022E23;
double masse_volumique = double.Parse(textBox3.Text) * 1E6;
double rayon = double.Parse(textBox1.Text);

double volume = 4 / 3 * 3.14159 * Math.Pow(rayon, 3);
double masse = masse_volumique * volume;
double Nbr_Atomes = masse/double.Parse(textBox4.Text) * Nbr_Avogadro;
double densite_lineaire = Math.Pow(Nbr_Atomes / volume, 0.3333);

double surfaceBarn_atome = double.Parse(textBox2.Text) * 1E-28;

int part = int.Parse(textBox6.Text);

double totalSize = 0;
long cntr = 0;

double tmop = 2 * rayon / part;

float prob = float.Parse(textBox8.Text)*10;

for (int i = 0; i < sphere.Count; i++)
{
double tt = 0;
long cntr1 = 0;
for (int j = 0; j < sphere[i].Count; j++)
{
for (int k = 0; k < sphere[i][j].Count; k++)
{
if (sphere[i][j][k] == true)
{
for (int z = 0; z < part; z++)
{
/*if(R.Next(0,1000) > prob)*/ tt += Get_moyenne_distance(tmop, sphere, i, j, k, prob);
cntr1++;
}
}
}
}
totalSize += tt;
cntr += cntr1;
}

double distance_moyenne = (totalSize/cntr);
double surface_theorique = Math.Pow(1 / densite_lineaire, 2);

double probabilite = (surfaceBarn_atome) / (surface_theorique) * 100 * densite_lineaire * distance_moyenne;

textBox5.Text = probabilite.ToString();

Algorithm_Non_1A();
}

private void Algorithm_Non_1A()
{
Collection>> sphere = Create_Sphere(float.Parse(textBox1.Text), float.Parse(textBox1.Text), int.Parse(textBox6.Text));

double Nbr_Avogadro = 6.022E23;
double masse_volumique = double.Parse(textBox3.Text);
double rayon = double.Parse(textBox1.Text);

double volume = 4 / 3 * 3.14159 * Math.Pow(rayon, 3);
double masse = masse_volumique * volume;
double Nbr_Atomes = masse / double.Parse(textBox4.Text) * Nbr_Avogadro;
double densite_lineaire = Math.Pow(Nbr_Atomes / volume, 0.3333);

double surfaceBarn_atome = double.Parse(textBox2.Text);


int prob = int.Parse(textBox8.Text);


double distance_moyenne = rayon;
double surface_theorique = Math.Pow(1 / densite_lineaire, 2);

double probabilite = (surfaceBarn_atome) / (surface_theorique) * 100 * densite_lineaire * distance_moyenne;

// by asumption of the same probability of Pu-240 and Pu-239

probabilite -= prob / 100.0 * probabilite;

textBox9.Text = probabilite.ToString();
}

private double Get_moyenne_distance(double step, Collection>> sphere, int x, int y, int z, float prob)
{

double cntr = 0;
long cntr2 = 0;

for (int i = 0; i < sphere.Count; i++)
{
for (int j = 0; j < sphere[i].Count; j++)
{
for (int k = 0; k < sphere[i][j].Count; k++)
{
int RRR = R.Next(0, 1000);

if (sphere[i][j][k] == true)
{
float distance = (float)Math.Sqrt(Math.Pow(x * step - i * step, 2) + Math.Pow(y * step - j * step, 2) + Math.Pow(z * step - k * step, 2));

if (RRR > prob) cntr+=distance;
if(distance > step) cntr2++;
}
}
}
}

return cntr / cntr2;
}

private Collection>> Create_Sphere(float rayon, float rayonI, int parts)
{
Collection>> sphere = new Collection>>();

double step = 2 * rayon / parts;

for(int i = 0; i < parts; i++)
{
Collection> tmp2 = new Collection>();
for(int j =0; j < parts; j++)
{
Collection tmp = new Collection();
for(int k = 0; k < parts; k++)
{
bool test = false;

float distance = (float)Math.Sqrt(Math.Pow(rayon - i * step, 2) + Math.Pow(rayon - j * step, 2) + Math.Pow(rayon - k * step, 2));

if (distance < rayonI) test = true;

tmp.Add(test);
}
tmp2.Add(tmp);
}
sphere.Add(tmp2);
}

return sphere;
}