// UI
objref vBoxPotassiumMain, graphPotassiumSEClamp, graphPotasasiumIn, plotShapeVoltagePotasasium, plotShapePotasasiumIn
// SEClamp on the soma
objref seClamp

// Sets SEClamp params on the soma.
// $1 - Amplitude
// $2 - Duration
proc stimulPotasasium() {
    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 - Diffusion coefficient
// $6 - duration of potassium input
// $7 - PX2
// $8 - Amplitude oof external Potassium current
// $9 - Density of potassium current
// $10 - Z coordinate circle center
// $11 - Resting concentration 
proc PotasasiumIteration () { local tau1, tau2
   Dk_kdifl = $5
   tau2 = $9
   tau1=10    
    forall {
		Dk_kdifl = $5
        Kp_kpump = $9
        Krest_kpump =	$11	
        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)  && (t < $6)) {
		// if (( (($1-x3d(1))^2 + ($2-y3d(1))^2  < $3^2) || (($1+$7-x3d(1))^2+($2-y3d(1))^2  < $3^2))) {   
		// ka_kdifl=140 // concentration clamp on the area
		   iextra_kdifl = $8
        } else  {
           iextra_kdifl = 0
        }      
    }
}


proc PotasasiumExtra () { 
    forall {
        if (( (($1-x3d(1))^2 + ($2-y3d(1))^2  < $4^2))  && (($3-z3d(1))^2 < 100)) {
		   ko = $5
        } else  {
           ko = $6
        }      
    }
}






// Runs Glutamate simulation.
proc runGlutamate() {
    stimulPotasasium(Amplitude, Duration)
    run()

    // Remove point processes after simulation
    objref seClamp
}

// Sets initial UI and simulation params.
proc initParamsPotassium() {
    // 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
    RFRAP = 3.0           // Radius of release
    TimeBegin = 5         // Time of release
    DCoef = 0.60          // diffusion
    KpLeak = 0.1          // Decay time of release
    Kduration = 200       // Duration of potassium input
    KCurrent = 1          // Amplitude of Extra Potassium currentin mM
	KResting = 110 // Resting concentration of potasesium in mM
	
	PX_o = 0              // X-Coordinate of extracellular potassium
	PY_o = 25             // Y-Coordinate of extracellular potassium
	PZ_o = 0              // Z-Coordinate of extracellular potassium
	R_o  = 10                 // radius of extracellular potassium changes
	PotassiumInside = 5       // Extracellular potassium cocnetration inside the circle
    PotassiumBasic = 3        // Basic extracellular potassium cocnentration 
	
}

// Shows Glutamate simulation UI.
proc showPotasasiumUi() {
    vBoxPotassiumMain = new VBox()
    vBoxPotassiumMain.intercept(1)	
	
    {
        xpanel("")
        xlabel("================== Potassium input ==================")
        xvalue("X coordinate [K]in(um)","PX", 1,"", 0, 1 )
        xvalue("Y coordinate [K]in (um)","PY", 1,"", 0, 1 )
		xvalue("Z coordinate [K]in (um)","PZ ", 1,"", 0, 1 )
        xvalue("Potassium [K]in input radius (um) ","RFRAP", 1,"", 0, 1 )
        xvalue("Potassium [K]in input onset (ms)","TimeBegin", 1,"", 0, 1 )
        xvalue("Duration of potassium input (ms)","Kduration", 1,"", 0, 1 )
        //xvalue("Distance between two release (um)","PX2", 1,"", 0, 1 )
        xvalue("Peak K+ current (mA/cm2)","KCurrent", 1,"", 0, 1 )
        xvalue("Diffusion coefficient (um2/ms)","DCoef", 1,"", 0, 1 )
        xvalue("K+ leak rate (mA/cm2)","KpLeak", 1,"", 0, 1 )
		xvalue("Basal [K+]in concentration (mM)","KResting", 1,"", 0, 1 )
		 xlabel("================== Extracellular potassium distribution ==================")
		xvalue("X coordinate [K]o (um)", "PX_o", 1,"", 0, 1 )
		xvalue("Y coordinate [K]o (um)", "PY_o", 1,"", 0, 1 )
		xvalue("Z coordinate [K]o (um)", "PZ_o", 1,"", 0, 1 )
	    xvalue("Potassium [K]o distribution radius (um) ", "R_o", 1,"", 0, 1 )
	    xvalue("Potassium [K]o inside circle (mM)", "PotassiumInside", 1,"", 0, 1 )
        xvalue("Basic [K]o outside circle (mM)", "PotassiumBasic", 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)
    }
    vBoxPotassiumMain.intercept(0)
    vBoxPotassiumMain.map("Potassium settings", 195, 102, 300, 530)
    
	
	
  //  removeIfExists(graphList[1], graphPotasasiumIn)
  //  removeIfExists(graphList[1], graphPotassiumSEClamp)

    graphPotasasiumIn = new Graph(0)
    graphList[1].append(graphPotasasiumIn)
    graphPotasasiumIn.size(0,150,-3.49246e-010,0.0101)
    graphPotasasiumIn.view(0, 110, 150, 120, 602, 705, 400, 200)
    graphPotasasiumIn.addvar("dendrite[1].ki( 0.05 )", 1, 1, 0.8, 0.9, 2)
	graphPotasasiumIn.addvar("dendrite[51].ki( 0.05 )", 2, 1, 0.8, 0.9, 2)
	graphPotasasiumIn.yaxis(0)
    graphPotasasiumIn.label(0.5, 0.09, "Time (ms)", 2, 1, 0, 1, 1)
	graphPotasasiumIn.label(0.1, 0.9, "[K+]in (mM)", 2, 1, 0, 1, 1)

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

   // removeIfExists(fast_flush_list, plotShapePotasasiumIn)
  //  removeIfExists(fast_flush_list, plotShapeVoltagePotasasium)

    plotShapePotasasiumIn = new PlotShape(0)
    fast_flush_list.append(plotShapePotasasiumIn)
    plotShapePotasasiumIn.size(-50,50,-50,50)
    plotShapePotasasiumIn.view(-50, -49.9003, 100, 100, 601, 102, 400.64, 400.32)
    plotShapePotasasiumIn.exec_menu("Shape Plot")
    plotShapePotasasiumIn.variable("ki")
	plotShapePotasasiumIn.label(0.1, 0.5, "[K+]in (mM)", 2, 1, 0, 1, 1)
    plotShapePotasasiumIn.show(0)
    plotShapePotasasiumIn.scale(110,120)

    plotShapeVoltagePotasasium = new PlotShape(0)
    fast_flush_list.append(plotShapeVoltagePotasasium)
    plotShapeVoltagePotasasium.size(-50,50,-50,50)
    plotShapeVoltagePotasasium.view(-50, -49.9003, 100, 100, 1145, 102, 400.64, 400.32)
    plotShapeVoltagePotasasium.exec_menu("Shape Plot")
    plotShapeVoltagePotasasium.variable("v")
	plotShapeVoltagePotasasium.label(0.5, 0.09, "Transmembrane Voltage (mV)", 2, 1, 0, 1, 1)
    plotShapeVoltagePotasasium.show(0)
    plotShapeVoltagePotasasium.scale(-87,-83)
	
	    plotShapeVoltagePotasasium = new PlotShape(0)
    fast_flush_list.append(plotShapeVoltagePotasasium)
    plotShapeVoltagePotasasium.size(-50,50,-50,50)
    plotShapeVoltagePotasasium.view(-50, -49.9003, 100, 100, 1745, 102, 400.64, 400.32)
    plotShapeVoltagePotasasium.exec_menu("Shape Plot")
    plotShapeVoltagePotasasium.variable("ko")
	plotShapeVoltagePotasasium.label(0.5, 0.09, "[K+]o (mM)", 2, 1, 0, 1, 1)
    plotShapeVoltagePotasasium.show(0)
    plotShapeVoltagePotasasium.scale(0,12)
}

// Opens Glutamate simulation window.
proc RunAstroPotasasium() {
  if ($1 > 0) { 
    load_file(1, "Simulations/PointProcessManager.hoc")
	} else {
	print "New trials"
	}
    initParamsPotassium()
    showPotasasiumUi()
}