<?php

class Termination extends mfBaseModel {
  protected $forcestr = ['phone','email','note'];
  
  private $building;
  private $status;
  private $lineworker;
  private $workflowitems;
  private $cpeprovisioning;
  private $files;
  private $patching;
  private $creator;
  private $editor;
  
  
  public function getAddress($singelLine = false) {
    if(!$this->id) {
      return false;
    }
    
    $b = $this->getProperty("building");
    
    $address = $b->street;
    if($this->name) {
      $address .= " ".$this->name;
    }
    $address .= "\n".$b->zip." ".$b->city;
    if($singelLine) {
      $address = str_replace("\n", " ", $address);
    }
    
    return $address;
  }
  
  public function getWorkflowvalue($itemname, $type = "string") {
    if(array_key_exists($itemname, $this->getProperty("workflowitems"))) {
      return $this->getProperty("workflowitems")[$itemname]->value->{"value_$type"};
    }
    
    return null;
  }
  
  public function getPop() {
    $pop_id = $this->getWorkflowvalue('pop_id');
    if(!$pop_id) {
      return null;
    }
    return new Pop($pop_id);
  }
  
  public function getNewObjectCode() {
    if(!$this->building_id) {
      return false;
    }
    
    $bcode = $this->getProperty("building")->code;
    var_dump($bcode);
    if(!$bcode) {
      return false;
    }
    
    $codes = [];
    
    // get existing codes
    $res = $this->db->select("Termination", "code", "code like '$bcode.%'");
    if($this->db->num_rows($res)) {
      while($data = $this->db->fetch_object($res)) {
        $codes[] = $data->code;
      }
    } else {
      return $bcode.".001";
    }
    
    if(count($codes)) {
      sort($codes);
    }
    
    $last_code = end($codes);
    
    $m = [];
    if(preg_match('/\.(\d+)$/', $last_code, $m)) {
      if($m[1]) {
        $last_num = $m[1];
      }
    } else {
      return false;
    }
    
    $new_num = ++$last_num;
    $code = $bcode.".".sprintf("%03d", $new_num);
    
    return $code;
  }
  
  public function getLineworkportPairs() {
    $ports = $this->getProperty("workflowitems")["ports"]->value->value_string;
    if(!$ports) {
      return [];
    }
    
    $return = [];
    $return["range"] = [];
    $return["pairs"] = [];
    
    $ports = preg_replace('/[^0-9-]+/', "", $ports);
    if(strpos($ports, "-") !== false) {
      // port range
      $this->log->debug("is range");
      $port_parts = explode("-", $ports);
      if(is_array($port_parts) && count($port_parts) == 2) {
        $from = $port_parts[0];
        $to = $port_parts[1];
        
        if($port_parts[0] > $port_parts[1]) {
          $from = $port_parts[1];
          $to = $port_parts[0];
        }
        
        $range = [];
        $pairs = [];
        
        
        
        for($i = $from; $i <= $to; $i++) {
          $range[] = intval($i);
          if($i + 1 <= $to) {
            $pairs[] = $i."-".($i+1);
          }
        }
        
        $return["range"] = $range;
        $return["pairs"] = $pairs;
      }
    } else {
      // single port
      $this->log->debug("not a range");
      $return["range"][] = $ports;
    }
    
    //var_dump($return);exit;
    return $return;
    
  }
  
  public function resetProperties() {
    $this->building = null;
    $this->status = null;
    $this->lineworker = null;
    $this->workflowitems = null;
    $this->files = null;
    $this->creator = null;
    $this->editor = null;
  }
  
  public function getProperty($name) {
    if($this->$name == null) {
      
      if($name == "status") {
        $this->status = TerminationstatusModel::getOne($this->status_id);
        return $this->status;
      }
      
      if($name == "workflowitems") {
        foreach(WorkflowitemModel::search(["object_type" => "Termination", "active" => 1]) as $item) {
          $item->setObjectId($this->id);
          $this->workflowitems[$item->name] = $item;
        }
        //var_dump($this->workflowitems);exit;
        return $this->workflowitems;
      }
      
      if($name == "files") {
        $this->files = TerminationFileModel::search(["termination_id" => $this->id]);
        return $this->files;
      }
      
      if($name == "patching") {
        $this->patching = PatchingModel::getFirst(["termination_id" => $this->id]);
        return $this->patching;
      }
      
      if($name == "Cpeprovisioning") {
        $this->cpeprovisioning = CpeprovisioningModel::getFirst(["termination_id" => $this->id]);
        return $this->cpeprovisioning;
      }
      
      $classname = ucfirst($name);
      $idfield = $name."_id";
      $this->$name = new $classname($this->$idfield);
      
      if($this->$name->id) {
        return $this->$name;
      } else {
        return null;
      }
    }
    
    return $this->$name;
  }

}