import imp
import random

class IF:
    def __init__(self,MT):
        print "Microtubule object:",MT
        self.MT=MT
        self.cooldownframes=100
        self.maxcrosslinks=0
    def IFStep(self,frame=-1):
        if frame==self.cooldownframes and self.MT.Params.GetParameter('PlasmaMembrane')==False:
            self.MT.Params.SetParameter(['CatastropheProbability',0.00])
            self.MT.LayerActions.append(['CatastropheProbability',0.0])
            self.MT.Params.SetParameter(['TargetMTCount',0])
        if frame<self.cooldownframes and self.MT.Params.GetParameter('PlasmaMembrane')==False:
            self.MT.Params.SetParameter(['DONTADD',False])
            self.MT.Params.SetParameter(['CatastropheProbability',1000000])
            self.MT.Params.SetParameter(['TargetMTCount',0])
        if frame>self.cooldownframes:
            if self.MT.Params.GetParameter('PlasmaMembrane')==False:
                totlength=self.MT.ReturnTotalLength()
                if totlength>20: self.MT.Params.SetParameter(['CatastropheProbability',10])
                elif totlength<0.5: self.MT.Params.SetParameter(['CatastropheProbability',0])
                print 'Total length:',totlength,'   Cat:',self.MT.Params.GetParameter('CatastropheProbability')
        if random.random()<0.05 and self.MT.Params.GetParameter('PlasmaMembrane')==True and self.MT.TotalMicrotubuleCount()<300:
            self.MT.AddRandomMT()
    def MicrotubuleIF(self,mt,mtnumber,totallength,maximumlength,frame=-1):
        mt.SaveMTCoordinates()
        mt.StepForward(totallength,maximumlength)
        collobject=self.MT.CheckSpecificCollision(mtnumber)
        if collobject.HitSomething()==True:
            mt.RestoreMT()
            decision=self.MT.ZipperDecision(collobject)
            if decision=='Zipper': mt.Zipper(collobject.angle2)
            elif decision=='ReverseZipper': mt.ReverseZipper(collobject.angle2)
            elif decision=='Catastrophe': mt.HaveCatastrophe()
            elif decision=='Sever': self.MT.SeverMicrotubule(collobject)
            else: mt.Pause()
        if self.MT.Params.GetParameter('PlasmaMembrane')==True:
            try: currentcat=mt.CatastropheProbability
            except: currentcat=0
            crossovers=self.MT.OtherFaceMTs.CountMTCrossovers(mt.Coordinates)
            newcatrate=5.0**crossovers/1000.0
            #newcatrate=0.2*crossovers
            if crossovers>self.maxcrosslinks: self.maxcrosslinks=crossovers
            if currentcat<newcatrate: mt.CatastropheProbability = newcatrate
        if random.random()<0.01 and self.MT.Params.GetParameter('PlasmaMembrane')==False:
            collobject=self.MT.OtherFaceMTs.CheckLinesCollisionWithPopulation(mt.Coordinates)
            if collobject.HitSomething()==False:
                self.MT.OtherFaceMTs.MTcollection.append(mt)
                del self.MT.MTcollection[mtnumber]
        if self.MT.Params.GetParameter('PlasmaMembrane')==False and random.random()<0.1*mt.Length():
            self.MT.SpawnMT()
        if self.MT.Params.GetParameter('PlasmaMembrane')==True and random.random()<0.05*mt.Length():
            self.MT.SpawnMT()
        if mt.SmallMT()==True or self.MT.Cell.Polygon.CheckInsideCell2(mt.tempcoords)==False:
            try:
                mt.PrepareToDie()
                del self.MT.MTcollection[mtnumber]
            except:
                pass
