package controllers.services
import models._
import beans._
import scala.collection.mutable.ArrayBuffer
case class PartRecursion() {
  
  def down(parentPart: Part, partBuffer: ArrayBuffer[PartsListBean], opt: Int):Unit = {
    for(child <-parentPart.parts) {
      if(parentPart.partRelation(child.id).delDcId==0) {
        if(opt!=3 || opt==3 && child.name.startsWith("Unit")) {
          	partBuffer += PartsListBean(parentPart.name, child.name, parentPart.partRelation(child.id).quantity)
	     }
	    if(opt==1 || opt==2 && !child.name.startsWith("Part") || opt==3) {
	      PartRecursion().down(child, partBuffer, opt)
	    }
	   }
    }
  }
  
  def up(childPart: Part, partBuffer: ArrayBuffer[PartsListBean], opt: Int):Unit = {
    if(childPart.parentParts.size == 0 && opt == 2) {
	   partBuffer += PartsListBean("", childPart.name,0)
	 }
	 for(parent <- childPart.parentParts) {
	   if(parent.partRelation(childPart.id).delDcId == 0 ) {
	     if(opt != 2) {
	       partBuffer += PartsListBean(childPart.name, parent.name, parent.partRelation(childPart.id).quantity)
	      }
		  if(opt != 1) {
		    PartRecursion().up(parent, partBuffer, opt)
		  }
		}
	  }

  }

}