#-------------------------------------------------------------------------------
# Name:        StandardCell2.py
# Purpose:     Build a simple ball and stick neuron in python
# From:        https://nrn.readthedocs.io/en/latest/tutorials/ball-and-stick-1.html
# Author:      JamesBrown
#
# Created:     2023-07-13
# Copyright:   (c) JamesBrown 2023
# Licence:     <your licence>
#-------------------------------------------------------------------------------

# Useses
from neuron import h
#from neuron import gui
import matplotlib.pyplot as plt
from neuron.units import ms,mV,um,s # As good practice, we’ll load some unit definitions:
h.load_file("stdrun.hoc")
h.load_file('C:/nrn/Neuron/font.hoc') # Load the font changer HOC

# Current
A = 1
ma = A * 1E-3
ua = A * 1E-6
na = A * 1E-9
pa = A * 1E-12


def main():
    pass
if __name__ == '__main__':
    main()

#Defining the cell morphology
#   Create the sections

class TBallAndStick:
      # Initialize
    def __init__(self, gid):
        self._gid = gid
        self._setup_morphology()
        self._setup_biophysics()

    def _setup_morphology(self):
        self.soma = h.Section(name="soma", cell=self)
        self.dend = h.Section(name="dend", cell=self)
        self.all = [self.soma, self.dend]
        self.soma.L = 12.6157 * um
        self.soma.diam = 12.6157 * um
        self.dend.L = 200 * um
        self.dend.diam = 1 * um
        self.dend.connect(self.soma)

    def _setup_biophysics(self):
        for sec in self.all:
            sec.Ra = 100  # Axial resistance in Ohm * cm
            sec.cm = 1  # Membrane capacitance in micro Farads / cm^2
        self.soma.insert ("hh")
        for seg in self.soma:
            seg.hh.gnabar = (
              0.12 # Sodium conductiance in S/sm^2
              )
            seg.hh.gkbar = (
              0.036 # Potassium conductance in S/cm^2
              )
            seg.hh.gl = 0.0003 # Leak conductance in S/cm^2
            seg.hh.el = -54.3 * mV

        # Insert passive current in the dendrite
        self.dend.insert("pas")
        for seg in self.dend:
            seg.pas.g = 0.001
            seg.pas.e = -65 * mV


    def __repr__(self):
        return "BallAndStick[{}]".format(self._gid)

print (" ============ S T A R T ================")

# Check Topology
cell = TBallAndStick(0)

# Define stylized geometry
#print('cell.soma area: ',cell.soma(0.5).area(),'um')

# INSTRUMENTATION

# Simulation"

# define the current clamp as on the distal end of the dendrite
##stim = h.IClamp(cell.dend(1))
##stim.get_segment()
##stim.delay = 5 * ms
##stim.dur =   10 * ms
##stim.amp = 0.320

stim2 = h.SEClamp(cell.dend(1))
stim2.dur1 = 10 * ms
stim2.amp1 =  0 * mV

h.psection( 'dend' )

#Recording

dend_Vin = h.Vector().record(cell.dend(1)._ref_v)
dend_Vout = h.Vector().record(cell.dend(0)._ref_v)
soma_V = h.Vector().record(cell.soma(0.0)._ref_v)
t = h.Vector().record(h._ref_t)

# Run
h.finitialize(-60 * mV)
h.continuerun(25 *  ms)

# Plot the results
color_set = ['red', 'blue', 'black', 'orange', 'cyan']
plt.title('Plot of cell membrain voltage')


plt.plot(t, dend_Vin ,color='blue', label = 'dend_Vin')
plt.plot(t, dend_Vout ,color='black', label = 'dend_Vout')
plt.plot(t, soma_V,color='red',label = "soma_V" )
plt.xlabel("t (ms)")
plt.ylabel("v (mV)")
plt.legend()

plt.show()

print (" ============ E N D ================")
