<?php
namespace shop;

class ProductAttribute implements \ArrayAccess
{
  // sprawdź czy wartość atrybutu jest domyślna
  static public function is_value_default( int $value_id )
  {
    global $mdb;

    $cacheHandler = new \Shared\Cache\CacheHandler();
    $cacheKey = "\shop\ProductAttribute::is_value_default:$value_id";

    $objectData = $cacheHandler -> get( $cacheKey );

    if ( !$objectData )
    {
      $is_default = $mdb -> get( 'pp_shop_attributes_values', 'is_default', [ 'id' => $value_id ] );
      $cacheHandler -> set( $cacheKey, $is_default );
    }
    else
    {
      return unserialize( $objectData );
    }

    return $is_default;
  }

  public function offsetExists($offset) {
    return isset($this->$offset);
  }

  public function offsetGet($offset) {
    return $this->$offset;
  }

  public function offsetSet($offset , $value) {
    $this->$offset = $value;
  }

  public function offsetUnset($offset) {
    unset($this->$offset);
  }

  public function __construct( int $attribute_id, $lang_id = null )
  {
    global $mdb;

    if ( !$lang_id )
      $lang_id = ( new \Domain\Languages\LanguagesRepository( $mdb ) )->defaultLanguage();

    $result = $mdb -> get( 'pp_shop_attributes', '*', [ 'id' => $attribute_id ] );
    if ( \Shared\Helpers\Helpers::is_array_fix( $result ) ) foreach ( $result as $key => $val )
      $this -> $key = $val;

    $results = $mdb -> select( 'pp_shop_attributes_langs', '*', [ 'AND' => [ 'attribute_id' => $attribute_id, 'lang_id' => $lang_id ] ] );
    if ( \Shared\Helpers\Helpers::is_array_fix( $results ) ) foreach ( $results as $row )
      $this -> language = $row;
  }

  // pobierz kolejność atrybutu
  static public function get_attribute_order( int $attribute_id )
  {
    global $mdb;

    $cacheHandler = new \Shared\Cache\CacheHandler();
    $cacheKey = "\shop\ProductAttribute::get_attribute_order:$attribute_id";

    $objectData = $cacheHandler -> get( $cacheKey );

    if ( !$objectData )
    {
      $order = $mdb -> get( 'pp_shop_attributes', 'o', [ 'id' => $attribute_id ] );
      $cacheHandler -> set( $cacheKey, $order );
    }
    else
    {
      return unserialize($objectData);
    }

    return $order;
  }

  // pobierz nazwę atrybutu za pomocą wartości
  static public function getAttributeNameByValue( int $value_id, string $lang_id )
  {
    global $mdb;
    return array_pop( $mdb -> query( 'SELECT name FROM pp_shop_attributes_langs AS psal INNER JOIN pp_shop_attributes_values AS psav ON psal.attribute_id = psav.attribute_id WHERE psav.id = ' . $value_id . ' AND lang_id = \'' . $lang_id . '\'' ) -> fetch( \PDO::FETCH_ASSOC ) );
  }

  // pobierz nazwę wartości
  static public function get_value_name( int $value_id, string $lang_id )
  {
    global $mdb;

    $cacheHandler = new \Shared\Cache\CacheHandler();
    $cacheKey = "\shop\ProductAttribute::get_value_name:$value_id:$lang_id";

    $objectData = $cacheHandler -> get( $cacheKey );

    if ( !$objectData )
    {
      $value_name = $mdb -> get( 'pp_shop_attributes_values_langs', 'name', [ 'AND' => [ 'value_id' => (int)$value_id, 'lang_id' => $lang_id ] ] );
      $cacheHandler -> set( $cacheKey, $value_name );
    }
    else
    {
      return unserialize($objectData);
    }
    return $value_name;
  }

  // pobierz nazwę atrybutu
  static public function getAttributeName( int $attribute_id, string $lang_id )
  {
    global $mdb;
    return $mdb -> get( 'pp_shop_attributes_langs', 'name', [ 'AND' => [ 'attribute_id' => (int)$attribute_id, 'lang_id' => $lang_id ] ] );
  }
}