<?php

class Address extends mfBaseModel {
  protected $forcestr = ['street','company','zip','phone','fax','mobile','note'];
  private $parent;
  private $types;
  private $attributes;
  private $permissions;
  
  private $phoneparts;
  
  public function getFullName() {
    // Assumes "Firma1 Firma2" or "firstname lastname" as readable form
    $name = "";
    
    if($this->firstname && $this->lastname) {
      $name = $this->firstname . " " . $this->lastname;
    } elseif($this->lastname) {
        $name = $this->lastname;
    } elseif($this->firstname) {
      $name = $this->firstname;
    }
    
    return $name;
    
  }
  
  public function splitPhoneNumber() {
    if(!$this->phone) {
      return false;
    }
    
    if($this->phoneparts) {
      return $this->phoneparts;
    }
    
    $phone = preg_replace('/[^0-9]\+/', '', $this->phone);
    
    $cc = "";
    $num = "";
    if(substr($phone, 0, 1) === '+') {
      $cc = substr($phone, 1, 2);
      $num = substr($phone, 3);
    } elseif(substr($phone, 0, 2) === '00') {
      $cc = substr($phone, 2, 2);
      $num = substr($phone, 4);
    } elseif(substr($phone, 0, 1) === '0') {
      $cc = 43;
      $num = substr($phone, 1);
    } else {
      $cc = 43;
      $num = $phone;
    }
    
    $this->phoneparts = [$cc,$num];
    return $this->phoneparts;
    
    
    
  }
  
  public function getCompanyOrName($returnParent = false) {
    if($returnParent && $this->parent_id) {
      return $this->getProperty("parent")->getCompanyOrName(true);
    }
    
    if($this->company) {
      return $this->company;
    }
    return $this->getFullName();
  }

  
  
  public function loadAddresstypes() {
    if(!$this->id) {
      return false;
    }
    
    if(!$this->parent_id) {
      $this->types = AddresstypeModel::search(['address_id' => $this->id], true);
    } else {
      // get types from parent
      
      $parent = $this->getProperty("parent");
      $types = $parent->getProperty("types");
      $this->types = $types;
    }
    
    return true;
    
  }
  
  public function generateServicePin() {
    if(!$this->customer_number) {
      return false;
    }
    $num1 = rand(65, 90);
    $num2 = rand(65, 90);
    
    $c1 = chr($num1);
    $c2 = chr($num2);
    
    $spin = $c1.$c2.$this->customer_number;
    return $spin;
  }
  
  public function getProperty($name) {
    if($this->$name == null) {
      
      if(!$this->id) {
        return null;
      }
      
      if($name == "types") {
        $this->loadAddresstypes();
        return $this->types;
      }
      
      if($name == "attributes") {
        $attribs = AddressattributeModel::search(['address_id' => $this->id]);
        if(count($attribs)) {
          $this->attributes = [];
          foreach($attribs as $a) {
            $this->attributes[$a->name] = $a;
          }
        }
        return $this->attributes;
      }
      
      if($name == "permissions") {
        $this->permissions = NetworkAddressModel::search(['address_id' => $this->id]);
        return $permissions;
      }
      
      if($name == "parent") {
        $this->parent = new Address($this->parent_id);
        return $this->parent;
      }
      
      $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;
  }
}