#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#%% Import libraries

import math
import argparse
import numpy as np
import random

#%% Defined command line options for the user (2 values of distance (d0, d1), 2 values of thickness (T0, T1), and 1 random value of eccentricity (epsilon) from equatorial or mid-latitude distributions)

CLI = argparse.ArgumentParser()
CLI.add_argument(
    "--distances",
    nargs="*",
    type=float,
)
CLI.add_argument(
    "--thicknesses",
    nargs="*",
    type=float,
)
CLI.add_argument(
    "--eccentricity",
    nargs="*",
    type=str,
    choices=["equatorial", "mid-latitude"],
    default=["equatorial"],
)

# parse the command line
args = CLI.parse_args()

d = args.distances
T = args.thicknesses

d0_km = d[0]
d1_km = d[1]

#%% Beta parameters to calculate (and sample) the equatorial or mid-latitude eccentricity: 
    
alpha_equatorial = 2.3911
beta_equatorial = 1.0184
alpha_mid_latitude = 4.3004
beta_mid_latitude = 1.0285


beta_params = {
    "equatorial": (alpha_equatorial, beta_equatorial),
    "mid-latitude": (alpha_mid_latitude, beta_mid_latitude)
}


eps = {}
if args.eccentricity:
    for choice in args.eccentricity:
        alpha, beta = beta_params[choice]
        sampled_value = random.betavariate(alpha, beta)
        eps[choice] = sampled_value
        print(f"Sampled eccentricity {choice}: {eps[choice]}")
else:
    alpha, beta = beta_params[args.eccentricity[0]]
    sampled_value = random.betavariate(alpha, beta)
    eps = {args.eccentricity[0]: sampled_value}
    print(f"Sampled eccentricity {args.eccentricity[0]}: {eps[args.eccentricity[0]]}")

#%% Define the range of sqrt(A)=x (for x0 and x1)

r = math.sqrt(3.14)

# Calculation of k and y if the user has chosen one type of eccentricity:
if args.eccentricity:
    valore_per_key = eps[args.eccentricity[0]]
    valore_float = float(valore_per_key)
    eps2 = valore_float ** 2
    k = 1 - eps2
    y = pow(k, 0.25)

##WARNING (condition of existence for the following calculation):
    war = d0_km * 2 / k
    if d1_km > war:
        # Define the range for x0 and X1:
        left0 = 0.5 * r * d0_km * y
        right0 = d0_km * r * y / k
        left1 = 0.5 * r * d1_km * y
        right1 = r * d1_km * y / k
        print('***** Calculations for x0, x1 *****')
        print(round(left0,2), ' < x0 < ', round(right0,2))
        print(round(left1,2), ' < x1 < ', round(right1,2))


#%%Calculate Lamba_thick range (in km) 

        #Definition of the parameters used for l_left (minimum value):
        a1= (pow(d1_km,2)/4)
        a0 = (pow(d0_km,2)/pow(k,2))
        a = a1-a0
        b = math.log(T[0]/T[1])
        arg1=a/b
        #Definition of the parameters used for l_right (maximum value):
        c1 = pow(d1_km,2)/pow(k,2)  
        c0 = pow(d0_km,2)/4
        c= c1-c0
        arg2=c/b
        #lambda_left < lambda < lambda_right
        l_left_I= r*y*np.sqrt(arg1)
        l_right_I= r*y*np.sqrt(arg2)
        print ('***** Calculations for LAMBDA *****')
        print (round(l_left_I,2), ' < Lambda  < ', round(l_right_I,2))
        
#%%Calculate Volume (in m^3)
#volume_min(left) < volume < volume_max(right)

        vol_left= (3*1e06)*pow(l_left_I,1.53)
        vol_right= (3*1e06)*pow(l_right_I,1.53)
        print ('***** Calculations for VOLUME *****')
        print (round(vol_left/1e9,2), ' < Volume (km^3) < ', round(vol_right/1e9,2))
        
#%%Calculate Mass (in kg)

        rho= 1000  #density in (kg/m^3)
        mass_left = rho*vol_left
        mass_right = rho*vol_right
        #print (mass_left, ' < Mass (kg) < ', mass_right)
        
#%%Calculate Magnitude (from Mason et al., 2004)
#Magnitude_min(left) < Magnitude < Magnitude(right) 
 
        mag_left= math.log10(mass_left)-7.0
        mag_right= math.log10(mass_right)-7.0
        print ('***** Calculations for MAGNITUDE *****')
        print (round(mag_left,2), ' < Magnitude < ', round(mag_right,2))

             
    else:
        print ('Existance condition not provided: please, choose other distances d0,d1')

