<?php

class Network extends mfBaseModel {
  private $owner;
  private $pops;
  private $addresstypes;
  private $roles;
  private $sections;
  private $buildings;

  
  public function getTypeAddresses($search_type) {
    if(!$this->id) {
      return false;
    }
    $addresses = [];
    
    $addresstypes = $this->getProperty("addresstypes");
    //var_dump($addresstypes);exit;
    foreach($addresstypes as $address_id => $atypes) {
      //var_dump($atypes);
      foreach($atypes as $atype) {
        //var_dump($atype);
        if($atype->type == $search_type && !array_key_exists($address_id, $addresses)) {
          $addresses[$address_id] = new Address($address_id);
        }
      }
    }
    
    return $addresses;
  }
  
  public function getAddressesByType($type) {
    $addresses = [];
    
    $addresstypes = NetworkAddressModel::search(['network_id' => $this->id, "type" => $type]);
    
    
    foreach($addresstypes as $at) {
      if($at->type == $type) {
        $addresses[$at->address_id] = $at->address;
      }
    }
    
    // check for parent addresses
    foreach($addresses as $address) {
      if($address->parent_id && !array_key_exists($address->id, $addresses)) {
        $addresses[$address->parent_id] = new Address($address->parent_id);
      }
    }
    
    // check for child addresses
    foreach($addresses as $address) {
      $childs = AddressModel::search(["parent_id" => $address->id]);
      foreach($childs as $child) {
        if(!array_key_exists($child->id, $addresses)) {
          $addresses[$child->id] = $child;
        }
      }
    }
    
    
    //var_dump($addresses);exit;
    return $addresses;
  }
  
  public function getAddressUsersByAddresstype($type) {
    $addresses = $this->getAddressesByType($type);
    if(!$addresses) {
      return [];
    }
    
    $users = [];
    foreach($addresses as $address) {
      $tmp_users = UserModel::search(["address_id" => $address->id]);
      foreach($tmp_users as $u) {
        if(!array_key_exists($u->id, $users)) {
          $users[$u->id] = $u;
        }
      }
    }
    
    return $users;
    
    
  }
  
  public function loadAddresstypes() {
    if(!$this->id) {
      return false;
    }
    
    //$this->loadRoles();
    
    $types = NetworkAddressModel::search(['network_id' => $this->id]);
    
    foreach($types as $type) {
      $this->addresstypes[$type->address_id][] = $type;
    }
    
    //var_dump($this->addresstypes);exit;
  }
  
  public function getProperty($name) {
    if($this->$name == null) {

      if(!$this->id) {
        return null;
      }
      
      if($name == "owner") {
        $this->owner = new Address($this->owner_id);
        return $this->owner;
      }
      
      if($name == "pops") {
        $pops = PopModel::search(['network_id' => $this->id]);
        $this->pops = $pops;
        return $this->pops;
      }
      
      if($name == "addresstypes") {
        $this->loadAddresstypes();
        return $this->addresstypes;
      }
      
      if($name == "sections") {
        $this->sections = NetworksectionModel::search(['network_id' => $this->id]);
        return $this->sections;
      }
      
      if($name == "buildings") {
        $this->buildings = BuildingModel::search(["network_id" => $this->id]);
        return $this->buildings;
      }
      
      $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;
  }
}