<?php

abstract class enumbasic {
    private function __construct(){}

    public static function getEnumList($other=false, $exclude=false){
      $ancestral = get_called_class();
        if($other){
          $retorno = array_merge($ancestral::$nickname, $ancestral::$others);
          if ($exclude){
            foreach($exclude as $itemExclude){
              unset($retorno[$itemExclude]);
            }
          }
          return $retorno;
        }else{
          $retorno = $ancestral::$nickname;
          if ($exclude){
            foreach($exclude as $itemExclude){
              unset($retorno[$itemExclude]);
            }
          }
          return $retorno;
        }
    }

    public static function getDescriptionList(){
      $ancestral = get_called_class();
      return $ancestral::$description;
    }

    public static function getNickFromValue($value, $others=false){
      $enumlist = self::getEnumList($others);
      if((is_array($enumlist)) && (isset($enumlist[$value]))){
        return $enumlist[$value];
      }else{
       return null;
      }
    }

    public static function getNickFromConstValue($constValue){
      $enumlist = self::getEnumList();
      if(is_array($enumlist)){
        foreach(self::getEnumList() as $key => $value){
          if ($key == $constValue){
            return $value;
            break;
          }
        }
      }else{
        return null;
      }
    }

    public static function getConstFromNickValue($nickValue){
      $enumlist = self::getEnumList();
      if(is_array($enumlist)){
        foreach(self::getEnumList() as $key => $value){
          if ($value == $nickValue){
            return $key;
            break;
          }
        }
      }else{
        return null;
      }
    }

    public static function getDescriptionFromConstValue($constValue){
      $enumlist = self::getDescriptionList();
      if(is_array($enumlist)){
        foreach(self::getDescriptionList() as $key => $value){
          if ($key == $constValue){
            return $value;
            break;
          }
        }
      }else{
        return null;
      }
    }


    public static function getNickFromInt($intValue, $other=false){
      $enumlist = self::getEnumList($other);
      if(is_array($enumlist)){
        $cont = 0 ;
        foreach ($enumlist as $key => $value) {
          if($cont==$intValue){
            return $key;
          }
          $cont++;
        }
      }
    }

    public static function getRemoveArray($indices = array(), $other=false) {
      $enumlist = self::getEnumList($other);
      foreach ($indices as $key => $indice) {
        unset($enumlist[$indice]);
      }
      return $enumlist;
    }

    public static function getEnumListConst(){
      $oClass = new \ReflectionClass(get_called_class());
      $constants = $oClass->getConstants();
      return (array_combine(array_keys($constants), array_values($constants)));
    }

    public static function getTypeForJs() {
      $ancestral = get_called_class();
      $name = substr($ancestral, strripos($ancestral, '\\'));

      $return = array('name' => $name, 'constants' => self::getEnumListConst(), 'nickname' => $ancestral::$nickname);
      if (isset($ancestral::$others)) {
        $return['others'] = $ancestral::$others;
      }

      return $return;
    }

    public static function getTabs($other=false){
        $enumlist = self::getEnumList($other);

        return array_map(function($key, $value){
            return ['text' => $value, 'active' => false, 'key' => $key];
        }, array_keys($enumlist), $enumlist);

    }

    public static function getOthers(){
        $ancestral = get_called_class();
        return $ancestral::$others;
    }

}
