<?php

class Building extends mfBaseModel {
  protected $forcestr = ['street','zip','phone','email','note'];
  
  private $network;
  private $networksection;
  private $pop;
  private $type;
  private $status;
  private $pipeworker;
  private $terminations;
  private $workflowitems;
  private $files;
  
  public function getAddress($singelLine = false) {
    if(!$this->id) {
      return false;
    }
    
    $address = $this->street."\n".$this->zip." ".$this->city;
    if($singelLine) {
      $address = str_replace("\n", " ", $address);
    }
    
    return $address;
  }
  
  protected function afterSave() {
    $this->resetProperties();
  }
  
  public function resetProperties() {
    $this->network = null;
    $this->pop = null;
    $this->type = null;
    $this->status = null;
    $this->pipeworker = null;
    $this->terminations = null;
    $this->workflowitems = null;
    $this->files = null;
  }
  
  public function getNewObjectCode() {
    if(!$this->zip) {
      return false;
    }
    
    $cc = "AT";
    $zip = $this->zip;
    
    for($try = 16; $try > 0; $try--) {
      $rnd[0] = random_int(0, 255);
      $rnd[1] = random_int(0, 255);
      $rnd[2] = random_int(0, 255);
      $rnd[3] = random_int(0, 255);

      $code = "$cc-$zip-";
      foreach($rnd as $r) {
        $code .= str_pad(dechex($r), 2, "0", STR_PAD_LEFT);
      }

      if(BuildingModel::search(['code' => $code])) {
        $this->log->warn(__FILE__."::getNewObjectCode: New Code already in use. Trying again for a maximum of $try times.");
      } else {
        // code is unique
        break;
      }
    }
    
    //var_dump($code);
    //var_dump($try);exit;
    
    if($try == 0) {
      return null;
    }
    
    return $code;
  }
  
  public function getLaeaCoordinates() {
    if(!$this->gps_lat || !$this->gps_lat) {
      return false;
    }
    
    // Elipsenparameter der Erdoberfläche
    $a = 6378137;
    $e = 0.081819191;
    
    // Lage des Ausgangspunktes in natürlichen Koordinaten und Korrekturwerte in Meter
    $latO = 0.907571211;
    $lonO = 0.174532925;
    $FE = 4321000;
    $FN = 3210000;
    
    // Mathematische Konstanten
    $qP = 1.99553108748562;
    $qO = 1.56982570415136;
    $PI = M_PI;
    
    // Umrechnung der Eingabekoordinaten in rad
    $Lat = $this->gps_lat * $PI / 180;
    $Lon = $this->gps_long * $PI / 180;
    
    // Berechnungen
    $q = (1 - $e ** 2) * ((sin($Lat) / (1 - $e ** 2 * sin($Lat) ** 2)) - ((1 / (2 * $e)) * log((1 - $e * sin($Lat)) / (1 + $e * sin($Lat)))));
    $beta = asin($q / $qP);
    $betaO = asin($qO / $qP);
    $Rq = $a * ($qP / 2) ** 0.5;
    $b = $Rq * (2 / (1 + Sin($betaO) * Sin($beta) + Cos($betaO) * Cos($beta) * Cos($Lon - $lonO))) ** 0.5;
    $D = $a * (Cos($latO) / (1 - $e ** 2 * Sin($latO) ** 2) ** 0.5) / ($Rq * Cos($betaO));
    $East = floor(($FE + (($b * $D) * (Cos($beta) * Sin($Lon - $lonO))))/100);
    $North = floor(($FN + ($b / $D) * ((Cos($betaO) * Sin($beta)) - (Sin($betaO) * Cos($beta) * Cos($Lon - $lonO))))/100);
    $Gridcell = "100mN{$North}E{$East}";
    
    return $Gridcell;
  }
  
  public function getProperty($name) {
    if($this->$name == null) {
      
      if($name == "type") {
        $this->type = new Buildingtype($this->type_id);
        return $this->type;
      }
      
      if($name == "status") {
        $this->status = new Buildingstatus($this->status_id);
        return $this->status;
      }
      
      if($name == "pipeworker") {
        $this->pipeworker = new Address($this->pipeworker_id);
        return $this->pipeworker;
      }
      
      if($name == "terminations") {
        $this->terminations = TerminationModel::search(['building_id' => $this->id]);
        return $this->terminations;
      }
      
      if($name == "workflowitems") {
        foreach(WorkflowitemModel::search(["object_type" => "building", "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 = BuildingFileModel::search(["building_id" => $this->id]);
        return $this->files;
      }
      
      $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;
  }
  
}