// UI
objref vBoxGluMain, graphSEClamp, graphGluout, plotShapeVoltage, plotShapeGluOut, plotShapeitrans
// SEClamp on the soma
objref seClamp

// Sets SEClamp params on the soma.
// $1 - Amplitude
// $2 - Duration
proc stimulGlu() {
    soma {
        seClamp = new SEClamp(0.5)
        seClamp.amp1 = $1   // nA
        seClamp.dur1 = $2
    }
}

// Updates Gluout/Gluin values on the cell.
// $1 - X coordinate circle center
// $2 - Y coordinate circle center
// $3 - Radius of a circle
// $4 - TimeBegin
// $5 - Time of rise
// $6 - Maximum Glu
// $7 - PX2
// $8 - Gluin
// $9 - Time decay
// $10 - - Z coordinate circle center
proc GluIteration () { local tau1, tau2
    tau1 = $5
    tau2 = $9
    InitialGlutamateConcentration = 20e-6
    forall {
        if (( (($1-x3d(1))^2 + ($2-y3d(1))^2  < $3^2) || (($1+$7-x3d(1))^2+($2-y3d(1))^2  < $3^2))  && (($10-z3d(1))^2 < 100) && (t > $4)) {
            Gluout_GluTrans = InitialGlutamateConcentration + $6*(tau2/(tau2-tau1)*(-exp(-(t-$4)/tau1) + exp(-(t-$4)/tau2)))
        } else  {
            Gluout_GluTrans = InitialGlutamateConcentration
        }
        Gluin_GluTrans = $8
    }
}

// Runs Glutamate simulation.
proc runGlutamate() {
    //stimulGlu(Amplitude, Duration)
    run()
    // Remove point processes after simulation
    objref seClamp
}

// Sets initial UI and simulation parameters.
proc initParamsGlutamate() {
    // Voltage Clamp of Soma, set of parameters
    Amplitude = -85       // Voltage clamp in mV
    Duration = 20000      // Duration of Voltage Clamp in mV
    DelayStim = 0

    // Parameter of Glutamate release in two places
    PX = 0                // X- Coordinate of the place
    PY = -20              // Y - coordinate of the place
	PZ =0                 // Z - coordinate of the place
    PX2 = 0               // Distance in x-coordinate of another place of release. If you need to define a single place of release just PX2 = 0
    R_FRAP = 3.0          // Radius of release
    TimeBegin = 5         // Time of release
    Tau1 = 10             // Decay time of release
    Tau2 = 12             // Rise time of release
    MaxGlu = 0.01         // Maxium concentration of Glutamate in mM
    Gluin = 0.3           // intracellular concentration of Glutamate in mM
}

// Shows Glutamate simulation UI.
proc showGlutamateUi() {
    vBoxGluMain = new VBox()
    vBoxGluMain.intercept(1)	
	
    {
        xpanel("")
        xlabel("================== Uncaging glutamate ==================")
        xvalue("X coordinate (um)","PX", 1,"", 0, 1 )
        xvalue("Y coordinate (um)","PY", 1,"", 0, 1 )
		xvalue("Z coordinate (um)","PZ ", 1,"", 0, 1 )
        xvalue("Uncaging radius (um) ","R_FRAP", 1,"", 0, 1 )
        xvalue("Uncaging onset (ms)","TimeBegin", 1,"", 0, 1 )
        xvalue("Glutamate concentration Max (mM)","MaxGlu", 1,"", 0, 1 )
        xvalue("Distance between two release (um)","PX2", 1,"", 0, 1 )
        //xvalue("Intracellular Glutamate in (mM)","Gluin", 1,"", 0, 1 )
        xvalue("Tau rise (ms)","Tau1", 1,"", 0, 1 )
        xvalue("Tau decay (ms)","Tau2", 1,"", 0, 1 )
        xlabel("======================================================")
        xpanel(625,164)

        xpanel("")
        xlabel("================== Voltage clamp of soma==================")
        xvalue("Somatic voltage clamp (mV)","Amplitude", 1,"", 0, 1)
        xvalue("Clamp duration (ms) ","Duration", 1,"", 0, 1)
        xvalue("Simulation time (ms)","tstop", 1,"", 0, 1)
        xbutton("Run simulation", "runGlutamate()")
        xpanel(328,129)
    }
    vBoxGluMain.intercept(0)
    vBoxGluMain.map("Glu settings", 195, 102, 300, 380)
    
    removeIfExists(graphList[1], graphGluout)
    removeIfExists(graphList[1], graphSEClamp)

    graphGluout = new Graph(0)
    graphList[1].append(graphGluout)
    graphGluout.size(0,150,-3.49246e-010,0.0101)
    graphGluout.view(0, -3.49246e-010, 150, 0.0101, 602, 705, 400, 200)
    graphGluout.addvar("soma[0].Gluout_GluTrans( 0.05 )", 1, 1, 0.8, 0.9, 2)
	graphGluout.yaxis(0)
    graphGluout.label(0.5, 0.09, "Time (ms)", 2, 1, 0, 1, 1)
	graphGluout.label(0.1, 0.9, "[Glu]0 (mM)", 2, 1, 0, 1, 1)

    graphSEClamp = new Graph(0)
    graphList[1].append(graphSEClamp)
    graphSEClamp.size(0,150,-0.015,-5.82077e-010)
    graphSEClamp.view(0, -0.015, 150, 0.015, 1145, 705, 400, 200)
    graphSEClamp.addexpr("seClamp.i", 1, 1, 0.8, 0.9, 2)
	graphSEClamp.yaxis(0)
    graphSEClamp.label(0.5, 0.9, "Time (ms)", 2, 1, 0, 1, 1)
    graphSEClamp.label(0.1, 0.09, "Clamp current (nA)", 2, 1, 0, 1, 1)

    removeIfExists(fast_flush_list, plotShapeGluOut)
    removeIfExists(fast_flush_list, plotShapeVoltage)

    plotShapeGluOut = new PlotShape(0)
    fast_flush_list.append(plotShapeGluOut)
    plotShapeGluOut.size(-50,50,-50,50)
    plotShapeGluOut.view(-50, -49.9003, 100, 100, 601, 102, 400.64, 400.32)
    plotShapeGluOut.exec_menu("Shape Plot")
    plotShapeGluOut.variable("Gluout_GluTrans")
	plotShapeGluOut.label(0.5, 0.09, "[Glutamate]0 (mM)", 2, 1, 0, 1, 1)
    plotShapeGluOut.show(0)
    plotShapeGluOut.scale(0,0.005)

    plotShapeVoltage = new PlotShape(0)
    fast_flush_list.append(plotShapeVoltage)
    plotShapeVoltage.size(-50,50,-50,50)
    plotShapeVoltage.view(-50, -49.9003, 100, 100, 1145, 102, 400.64, 400.32)
    plotShapeVoltage.exec_menu("Shape Plot")
    plotShapeVoltage.variable("v")
	plotShapeVoltage.label(0.1, 0.99, "Transmembrane Voltage (mV)", 2, 1, 0, 1, 1)
    plotShapeVoltage.show(0)
    plotShapeVoltage.scale(-85,-83)
	
	plotShapeitrans = new PlotShape(0)
    fast_flush_list.append(plotShapeitrans)
    plotShapeitrans.size(-50,50,-50,50)
    plotShapeitrans.view(-50, -49.9003, 100, 100, 1145, 102, 400.64, 400.32)
    plotShapeitrans.exec_menu("Shape Plot")
    plotShapeitrans.variable("itrans_GluTrans")
	plotShapeitrans.label(0.15, 0.99, "I (mA/cm2)", 2, 1, 0, 1, 1)
    plotShapeitrans.show(0)
    plotShapeitrans.scale(-0.07,0.07)
}

// Opens Glutamate simulation window.

proc RunAstroGlu() {
 if ($1 > 0) {
    load_file(1, "Simulations/PointProcessManager.hoc")
	OnlySingleGluSimulations = 0
	} else {
	print "New trials"
	}
    initParamsGlutamate()
    showGlutamateUi()
}