<?php

/**
 * Description of User
 *
 * @author fronk
 */
class User extends mfBaseModel {
  public $permissions;
  public $flags;
  public $address;
  
  private $my_networks;
 
  public function init() {
    $this->table = "Worker";
    
    if(defined("MFUSERTABLE")) {
      $this->table = MFUSERTABLE;
    }
  }
  
  /**
   * Loads currently logged in user 
   */
  public function loadMe() {
    if(defined("INTERNAL_USER_ID") && is_numeric(INTERNAL_USER_ID)) {
      $this->fetch(INTERNAL_USER_ID);
      return true;
    }
    
    $username = $_SESSION[MFAPPNAME.'_username'];
    $res = $this->db->select($this->table,"*","username='$username' LIMIT 1");
    if($this->db->num_rows($res)) {
      $data = $this->db->fetch_object($res);
      $this->load($data);
      return true;
    }
    return false;
  }
  
  public function getAbbrName() {
    if(strpos($this->name, " ") === false) {
      return $this->name;
    }
    
    $m = [];
    if(preg_match('/^([^ ]+) ([^ ]+)(?: ([^ ]+))?$/', $this->name, $m)) {
      $firstname = $m[1];
      if($m[3]) {
        $middlename = $m[2];
        $lastname = $m[3];
      } else {
        $middlename = "";
        $lastname = $m[2];
      }
      
      //var_dump($m);exit;
      
      $abbr = "$firstname ";
      if($middlename) {
        $abbr .= substr($middlename, 0, 1). ". ";
      }
      $abbr .= substr($lastname, 0, 1). ". ";
      return $abbr;
    }
    
    return $this->name;
  }
  
  protected function afterLoad() {
    $wp = new WorkerPermission();
    $wp->loadByUserId($this->id);
    $this->permissions = $wp;
    $this->loadFlags();
    
    $this->address = new Address($this->address_id);

    return true;
  }
  
  public function getFlag($name) {
    return new WorkerFlag($this->id, $name);
  }
  
  private function loadFlags() {
    $res = $this->db->select("WorkerFlag", "*", "worker_id=".$this->id);
    if(!$this->db->num_rows($res)) {
      return false;
    }
    
    while($data = $this->db->fetch_object($res)) {
      $this->flags[$data->name] = $data->value;
    }
  }
  
  protected function afterSave() {
    $this->afterLoad();
  }
  
  protected function afterDelete() {
    if(is_object($this->permissions)) {
      $this->permissions->delete();
    }
  }
  
  public function is($what) {
    if(!$this->id) {
      return false;
    }
    
    if(!is_array($what)) {
      $what = [$what];
    }
    
    foreach($what as $w) {
      if(is_object($this->permissions) && property_exists($this->permissions, "is$w")) {
        if($this->permissions->{"is$w"} === true) {
          return true;
        }
      }
      //var_dump($this->address->types);exit;
      if(is_object($this->address) && property_exists($this->address, "types") && is_array($this->address->types)) {
        if($this->address->types[$w]->id) {
          return true;
        }
      }
      
    }
    
    return false;
  }
  
  public function isAdmin() {
    if(is_object($this->permissions) && property_exists($this->permissions, "isAdmin")) {
      return $this->permissions->isAdmin;
    }
    $this->log->warning("No permissions object in user");
    return false;
  }
  
  public function __toString() {
    return $this->username;
  }
  
  
  public function getProperty($name) {
    if($this->$name == null) {
      
      if(!$this->id) {
        return null;
      }
      
      if($name == "my_networks") {
        $this->my_networks = $this->getMyNetworks();
        return $this->my_networks;
      }
      
      $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;
  }

  public function myNetworks($type) {
    $typenets = [];
    $my_networks = $this->getProperty("my_networks");
    
    //var_dump($my_networks);exit;
    
    foreach($my_networks as $net) {
      //var_dump($net);exit;
      $address_id = $this->address_id;
      if($this->getProperty("address")->parent_id) {
        $address_id = $this->getProperty("address")->parent_id;
      }
      $found = NetworkaddressModel::search(['network_id' => $net->id, "addresstype" => [$type], 'address_id' => $address_id]);
      //var_dump($found);exit;
      if($found) {
        $this->log->debug("allowed net: ".$net->id);
        $typenets[] = $net;
      }
      
    }
    
    //var_dump($typenets);exit;
    return $typenets;
  }
  
  private function getMyNetworks() {
    if(!$this->id) {
      return false;
    }
    
    $my_networks = [];
    
    $address_id = $this->address_id;
    
    if($this->getProperty("address")->parent_id) {
      $address_id = $this->getProperty("address")->parent_id;
    }
    //var_dump($address_id);exit;
    $network_list = NetworkAddressModel::search(['address_id' => $address_id]);
    foreach($network_list as $n) {
      if(!array_key_exists($n->network_id, $my_networks)) {
        $my_networks[$n->network_id] = new Network($n->network_id);
      }
    }
    
    return $my_networks;
  }
}
