package controllers.services
import models._
import beans._
import scala.collection.immutable.Seq

case class PartRecursion(){
   
  def down(parentPart: Part, parentQuantity: Long, opt: Int, key: String):Seq[PartsListShowBean] = {
    println("*********** Check2 **********")
    var partBuffer = Seq[PartsListShowBean]()
    parentPart.parts.map(ch => {
     val pr = parentPart.partRelation(ch.id)
      if(pr.delDcId==0) {
        var quantity:Long = 0
        if(opt==2) {
          quantity = parentQuantity * pr.quantity
        } else {
          quantity = pr.quantity
        }
        if(opt!=3 || opt==3 && ch.name.startsWith("Unit")) {
          if(key.isEmpty()) {
            partBuffer = partBuffer.:+(PartsListShowBean(parentPart.name, ch, quantity, pr.relationKey, quantity * ch.price))
          } else {
            if(pr.relationKey.equals(key)) {
              partBuffer = partBuffer.:+(PartsListShowBean(parentPart.name, ch, quantity, pr.relationKey, quantity * ch.price))
            }
          }
        }
        if(opt==1 || opt==2 && ch.price.==(0) || opt==3) {
          down(ch, quantity, opt, key).par.map(rpb => partBuffer = partBuffer.:+(rpb))
        }
      }
    })
    return partBuffer
  }

  def up(childPart: Part, opt: Int): Seq[PartsListBean] = {
    var partBuffer = Seq[PartsListBean]()
    if(childPart.parentParts.size == 0 && opt == 2) {
      partBuffer = partBuffer.:+(PartsListBean("", childPart.name, 0, ""))
    }
    childPart.parentParts.map( prt => {
      if(prt.partRelation(childPart.id).delDcId == 0 ) {
        if(opt != 2) {
          val pr = prt.partRelation(childPart.id)
          partBuffer = partBuffer.:+(PartsListBean(childPart.name, prt.name, pr.quantity, pr.relationKey))
        }
        if(opt != 1) {
          up(prt, opt).par.map(rpb => partBuffer = partBuffer.:+(rpb))
        }
      }
    })
    return partBuffer
  }
}