#! /usr/bin/env python
import argparse
types=['single','sizing','sensor','thick']
#Parsing args
parser = argparse.ArgumentParser(description="LGAD sensor simulator.")
parser.add_argument('type', metavar='type', type=str, nargs='?', help='Type of simulation to run, acceptable values are %s. Default is "single"'%types)
parser.add_argument('-n','-e', metavar="events", type=int, nargs='?', help="Number of events to run. Default is 300", default=300)
parser.add_argument('-c','--config', metavar="config", dest="config", type=str, nargs='?', help="URL to the plates config file. Defaults to plates.json", default="plates.json")
parser.add_argument('--toggle','-t', dest="toggle", metavar="toggle", type=int, nargs='+', help="Give the index range of plates to include in the line of best fit. Note: The scorin plane/sensor is included in this index. Give it in the form of a tuple of a closed set, like the command \"lgad -t 0 2\" will include plates, 0, 1 and 2 into the line of best fit.", default=None)
parser.add_argument('--no-scatter', dest="scatter", action="store_false", help="Enable coulomb scattering. Default True", default=None)
parser.add_argument('--scatter', dest="scatter", action="store_true", help="Enable coulomb scattering. Default True", default=None)
parser.add_argument('--sensor-thickness', dest="thickness", type=float, help="Thickness of the sensor in radiation lengths, default is zero.", default=0.0)
parser.add_argument('--sensor-position', dest="pos", type=float, help="The x positions of the sensor or scoring plane. Default is 405.", default=405)
parser.add_argument('--min', dest="min", type=float, help="Minmum x-position to test.", default=305)
parser.add_argument('--max', dest="max", type=float, help="Maximum x-position to test.", default=635)



args=parser.parse_args()

#Import statements.
#Note the reason for the try catch is for when it is installed using the pip package.
try:
    from lgad.simulation import simulate
    from lgad.moving_plates import *
    from lgad.thick_detector import *
    from lgad.utility import *
    from lgad.variable_thickness import *
except Exception:
    from moving_plates import *
    from simulation import simulate
    from thick_detector import *
    from utility import *
    from variable_thickness import *


#Using the parameters from argparse we run the specific simulation.
if args.type is None:
    print("No type was given, defaulting to single.")
elif args.type == "single":
    scoringPlane=Plate(args.pos,args.thickness,True)
    plates=getPlates(args.config,scoringPlane)
    simulate(scoringPlane=scoringPlane,plates=plates,events=args.n, plt=True, use=args.scatter)
elif args.type == "sensor":
    if args.toggle is None:
        moving_plates(plate_min=args.min, plate_max=args.max,events=args.n, sensor_radlen=args.thickness, use=args.scatter,config=args.config)
    else:
        toggle=args.toggle
        toggle[1]+=1
        moving_one_plate(plate_min=args.min, plate_max=args.max,events=args.n, sensor_radlen=args.thickness, toggle=toggle, use=args.scatter,config=args.config)
elif args.type == "thick":
    thick(events=args.n, sensor_radlen=args.thickness, toggle=None, use=args.scatter,config=args.config)
elif args.type == "sizing":
    variable_thickness(events=args.n,config=args.config)
else: 
    print("Unknown type given. Please use one of the following: %s"%(types))
