//*************************************************************************
// create and set netstims using champawt(), chnmdawt(), chint()



objref ConnectAMPA_[1000]
objref ConnectNMDA_[1000]
objref ConnectGABA_[1000]		// GABA only in soma
objref ConnectFAKE_[1000]		// NetCon that allows me to deliver an event to netstims

objref vsrc_
objref vsrcg_
objref tsrc_
objref fakeit					// i need something to connect the fake NetCon to


// create ampa connections
proc champawt() {    nAMPA_WT = $1   
    for i = 0, cell_number-1 {
    
    	for j = 0, NGLU-1 {
           	k = j*cell_number + i
			ConnectAMPA_[k] = new NetCon( NS_NetStim[k].pp, MSP_Cell[i].synlist.object[j].pp, 0, 0, nAMPA_WT) 
        }
    }
 }


// create nmda connections
proc chnmdawt() {    nNMDA_WT = $1 
    for i = 0, cell_number-1 {

    	for j = 0, NGLU-1 {
            k = j*cell_number + i
	        ConnectNMDA_[k] = new NetCon( NS_NetStim[k].pp, MSP_Cell[i].synlist.object[j+NGLU].pp, 0, 0, nNMDA_WT) 
        }
    }
}


// create gaba connections
proc chgabawt() {    nGABA_WT = $1
    for i = 0, cell_number-1 {

        for j = 0, NGABA-1  {
            k = j*cell_number + i
            ConnectGABA_[k] = new NetCon( NS_NetStim[k+NGABA*cell_number].pp, MSP_Cell[i].synlist.object[j+2*NGLU].pp, 0, 0, nGABA_WT) 
        }
    }
}


// This procedure will use vector play to change the inputs for up and down states
proc chfreq() {		// downstate frequency, upstate frequency, time of cycle, gaba:ampa number inputs ratio
    nDS_FRQ = $1
    nUS_FRQ = $2
    nT_CYCLE = $3
    nGA_RATIO = $4

    ncyc = 7		// number of cycles, as in 1 ds, 1 us, 1 ds, 1 us, 1 ds = 5

    tsrc_ = new Vector( 20, 10e5)		// used for predicting the interval changes for jstims
    for i = 0, ncyc-2 {tsrc_.x[i] = (i+1)*nT_CYCLE}	// netstims do not change interval until new spike is registered - jstim fixes this
    tsrc_.play( "for i = 0, NSYN-1 {NS_NetStim[i].pp.change = $1}", nT_CYCLE-dt)

    vsrc_ = new Vector( 20, nDS_FRQ) 		// init to down state frequency
    vsrc_.x[1] = nUS_FRQ 				// higher frequency for up states
    vsrc_.x[3] = nUS_FRQ
    vsrc_.x[5] = nUS_FRQ
    vsrc_.x[7] = nUS_FRQ
    vsrc_.play(" for i = 0, NGLU-1 { NS_NetStim[i].pp.frequency = $1 }", nT_CYCLE-dt )

    vsrcg_ = new Vector( 20, nDS_FRQ*(NGLU/NGABA)*nGA_RATIO)	// inputs to gaba netstims
    vsrcg_.x[1] = nUS_FRQ * (NGLU/NGABA) * nGA_RATIO	
    vsrcg_.x[3] = nUS_FRQ * (NGLU/NGABA) * nGA_RATIO	
    vsrcg_.x[5] = nUS_FRQ * (NGLU/NGABA) * nGA_RATIO	
    vsrcg_.play(" for i = NGLU, NGLU+NGABA-1  { NS_NetStim[i].pp.frequency = $1 }", nT_CYCLE-dt )
}





proc make_netstims() {
	for i = 0, NGLU*cell_number-1 {		// cortical cells 
		cell_append(new NS_NetStim(), 0, 0, 0)
	}

	for i = NGLU*cell_number, (NGLU+NGABA)*cell_number-1 {	//makes NGABA interneurons
		cell_append(new NS_NetStim(), 0, 0, 0)
	}

	for i = 0, (NGLU+NGABA)*cell_number-1 {
		ConnectFAKE_[i] = new NetCon( fakeit, NS_NetStim[i].pp,1,0,1) 	// source, target, threshold, delay, weight
	}			// now, ConnectFAKE_ can be used to deliver an event to NetStim from hoc

	champawt(AMPA_WT)  
	chnmdawt(NMDA_WT)
	chgabawt(GABA_WT)

	gampa(G_AMPA)
	gnmda(G_NMDA)
	ggaba(G_GABA)
	
	chfreq(0,0,284,1)  // downstate frequency, upstate frequency, time of cycle, GABA:AMPA ratio
}

make_netstims()
setnetn(0)			// allows events to played into NetCons via ConnectFAKEs

print "cells loaded"