package main

import (
	"anesthesia/aduAnes"
	"fmt"
	"math"
	"os"
	"strconv"
)

func main() {
	if len(os.Args) < 3 {
		fmt.Printf(" Anesthesia \n \n")
		fmt.Printf("The anesthesia program takes 2 arguments -age and -weight(kg), and will calculate the common anesthesia doses. \n \n")
		fmt.Printf("References: \n W.W. Mapleson: Effects of age on MAC in humans, a Meta-Analysis https://doi.org/10.1093/bja/76.2.179 \n \n")
		fmt.Printf("Program written by Moses G. Olson, MD				 copyright 2020.\n \n")
		return
	}

	arg := os.Args

	kg, err := strconv.ParseFloat(arg[1], 64)
	if err != nil {
		fmt.Println("You must enter a number weight in kg")
		return
	}

	age, err := strconv.ParseFloat(arg[2], 64)
	if err != nil {
		fmt.Println("You must enter an age in years")
		return
	}

	prop := kg * aduAnes.PropInduct
	etom := kg * aduAnes.EtomInduct
	ket := kg * aduAnes.KetInduct
	roc := kg * aduAnes.Roc
	lido := kg * aduAnes.Lido
	aprp := kg * aduAnes.Aprepit
	bena := kg * aduAnes.Bena
	zofr := kg * aduAnes.Ondan
	meto := kg * aduAnes.Metoc
	dex := kg * aduAnes.Dex
	midPO := kg * aduAnes.MidPO
	midK := kg * aduAnes.MidwKet
	ketPO := kg * aduAnes.KetPO
	midD := kg * aduAnes.MidDart
	ketD := kg * aduAnes.KetDart
	glyD := kg * aduAnes.GlycoDart
	succ := kg * aduAnes.SuccRapSeq
	glyRev := kg * aduAnes.Glyco
	neoRev := kg * aduAnes.Neo
	sugg0 := kg * 4
	sugg4 := kg * 2
	rapSugg := kg * 16

	//The number -0.002679, and formula for calculating MAC(age) is taken from British Journal of Anesthesia Vol 76, Issue 2, Feb 1996, Page 181
	ageDiff := -0.002679 * (age - 40)
	pow := math.Pow(10, ageDiff)

	sevo := aduAnes.SevoMAC * pow
	des := aduAnes.DesMAC * pow
	iso := aduAnes.IsoMAC * pow

	fmt.Printf("%v year old, %v kg", age, kg)

	// To avoid overdose "if" statements will be used to insert max dose for obese patients

	//Pre Medications
	fmt.Printf("\n \n")
	fmt.Println("Pre Medications")
	switch {
	case midPO > 25:
		fmt.Printf(" Midazolam  PO:		\t20 mg		Ketamine PO: 		%.0f mg + 20 mg of midazolam \n", ketPO)
	default:
		fmt.Printf(" Midazolam  PO:		\t%.0f mg	Ketamine PO: 		%.0f mg + %.0f mg of midazolam \n", midPO, ketPO, midK)
	}

	switch {
	case glyD > 0.4:
		fmt.Printf(" Ket/Mid/Glyco Dart:		%.0f/%.1f/0.4 mg \n \n", ketD, midD)
	default:
		fmt.Printf(" Ket/Mid/Glyco Dart:		%.0f/%.1f/%.2f mg \n \n", ketD, midD, glyD)
	}

	//Induction Medications
	fmt.Println("Induction Doses")
	switch {
	case prop > 200:
		fmt.Printf(" Propofol:			200 mg		Etomidate:		20 mg \n")
	default:
		fmt.Printf(" Propofol:			%.0f mg		Etomidate:		%.0f mg \n", prop, etom)
	}

	switch {
	case lido > 100:
		fmt.Printf(" Rocuronium:		\t%.0f mg		Lidocaine:		100 mg (max 40 mg for prevention of Propofol pain)\n", roc)
	default:
		fmt.Printf(" Rocuronium:		\t%.0f mg		Lidocaine:		%.0f mg (only 40 mg for prevention of Propofol pain)\n", roc, lido)
	}
	fmt.Printf(" Ketamine:			%.0f mg 		Succinylcholine:     	%.0f mg (rapid sequence) \n \n", ket, succ)

	//Antiemetics
	fmt.Println("Antiemetic Doses")
	switch {
	case zofr > 4:
		fmt.Printf(" Zofran:			4 mg		Dexamethasone:		10 mg \n")
		fmt.Printf(" 8 mg with history PONV\n")
	default:
		fmt.Printf(" Zofran:			%.0f mg		Dexamethasone:		%.0f mg \n", zofr, dex)
	}

	switch {
	case aprp > 80:
		fmt.Printf(" Aprepitant:		\t80 mg PO	Diphenhydramine:	%.0f  mg \n", bena)
	default:
		fmt.Printf(" Aprepitant:		\t%.0f mg PO		Diphenhydramine:	%.0f  mg \n", aprp, bena)
	}

	fmt.Printf(" Metochlopramide:	        %.0f mg		Scopolamine:		1 patch \n", meto)
	fmt.Printf("(Metochlopramide's anti-nausea effect at 10mg is no different than placebo, BJA-83 1999) \n \n")

	//Inhalational Agents
	fmt.Println("Inhalational Anesthetics")
	fmt.Printf(" MAC of Sevflurane: 	\t%.2f %% \n", sevo)
	fmt.Printf(" MAC of Desflurane: 	\t%.2f %% \n", des)
	fmt.Printf(" MAC of Isoflurane: 	\t%.2f %% \n \n", iso)

	//Reversal Angents
	fmt.Println("Reversal Agents")
	switch {
	case neoRev > 5:
		fmt.Printf(" Neostigmine:		5 mg		Glycopyrrolate: 	1  mg \n")
	default:
		fmt.Printf(" Neostigmine:		\t%.0f mg		Glycopyrrolate: 	%.0f  mg \n", neoRev, glyRev)
	}

	fmt.Printf(" Bridion TOF < 2:	        %.0f mg		Bridion TOF 4:		%.0f mg \n", sugg0, sugg4)
	fmt.Printf(" Rapid Reversal Bridion:	%.0f mg ", rapSugg)
	fmt.Printf("\n \n")

}
