<?php

namespace admin\factory;

class ShopAttribute
{
  static public function get_attributes_list()
  {
    global $mdb;

    $rows = $mdb -> select( 'pp_shop_attributes', '*', [ 'ORDER' => [ 'o' => 'ASC' ] ] );
    if ( \S::is_array_fix( $rows ) ) foreach ( $rows as $row )
    {
      $attribute = \admin\factory\ShopAttribute::attribute_details( $row['id'] );
      $attribute['values'] = \admin\factory\ShopAttribute::get_attribute_values( $row['id'] );
      $attributes[] = $attribute;
    }

    return $attributes;
  }

  // pobierz nazwę wartości atrybutu
  static public function get_attribute_value_by_id( int $value_id )
  {
    global $mdb;
    return $mdb -> get( 'pp_shop_attributes_values_langs', 'name', [ 'AND' => [ 'value_id' => $value_id, 'lang_id' => \front\factory\Languages::default_language() ] ] );
  }

  //pobierz nazwę atrybutu
  static public function get_attribute_name_by_id( int $attribute_id )
  {
    global $mdb;
    return $mdb -> get( 'pp_shop_attributes_langs', 'name', [ 'AND' => [ 'attribute_id' => $attribute_id, 'lang_id' => \front\factory\Languages::default_language() ] ] );
  }

  static public function values_save( int $attribute_id, $names, $values, $ids, $default_value = '', $impact_on_the_price )
  {
    global $mdb;

    $main_language = \front\factory\Languages::default_language();

    if ( \S::is_array_fix( $ids[$main_language] ) )
      $ids_delete = implode( ',', $ids[$main_language] );
    else
    {
      if ( $ids[$main_language] )
        $ids_delete = $ids[$main_language];
    }

    if ( $ids_delete )
      $mdb -> query( 'DELETE FROM pp_shop_attributes_values WHERE id NOT IN (' . $ids_delete . ') AND attribute_id = ' . $attribute_id );

    for ( $i = 0; $i < count( $names[$main_language] ); ++$i )
    {
      if ( $ids[$main_language][$i] )
      {
        $mdb -> update( 'pp_shop_attributes_values', [
          'impact_on_the_price' => $impact_on_the_price[$i] ? \S::normalize_decimal( $impact_on_the_price[$i] ) : null,
        ], [
          'id' => $ids[$main_language][$i],
        ] );

        \admin\factory\ShopProduct::update_product_price_by_attribute_value_impact( $ids[$main_language][$i], $impact_on_the_price[$i] );

        $langs = \admin\factory\Languages::languages_list();

        foreach ( $langs as $lang )
        {
          if ( $names[$lang['id']][$i] and $mdb -> count( 'pp_shop_attributes_values_langs', [ 'AND' => [ 'value_id' => $ids[$main_language][$i], 'lang_id' => $lang['id'] ] ] ) )
          {
            $mdb -> update( 'pp_shop_attributes_values_langs', [
              'name' => $names[$lang['id']][$i],
              'value' => $values[$lang['id']][$i] ? $values[$lang['id']][$i] : null,
            ], [
              'AND' => [
                'value_id' => $ids[$main_language][$i],
                'lang_id' => $lang['id'],
              ],
            ] );
          }
          elseif ( $names[$lang['id']][$i] and !$mdb -> count( 'pp_shop_attributes_values_langs', [ 'AND' => [ 'value_id' => $ids[$main_language][$i], 'lang_id' => $lang['id'] ] ] ) )
          {
            $mdb -> insert('pp_shop_attributes_values_langs', [
              'value_id' => $ids[$main_language][$i],
              'lang_id' => $lang['id'],
              'name' => $names[$lang['id']][$i],
              'value' => $values[$lang['id']][$i] ? $values[$lang['id']][$i] : null,
            ] );
          }
        }

        if ( $default_value == $i )
          $default_value_id = $ids[$main_language][$i];
      }
      else
      {
        $mdb -> insert( 'pp_shop_attributes_values', [ 'attribute_id' => $attribute_id ] );
        $value_id = $mdb -> id();

        if ( $value_id )
        {
          $mdb -> update( 'pp_shop_attributes_values', [
            'impact_on_the_price' => $impact_on_the_price[$i] ? \S::normalize_decimal( $impact_on_the_price[$i] ) : null,
          ], [
            'id' => $value_id,
          ] );

          if ( $impact_on_the_price[$i] )
            \admin\factory\ShopProduct::update_product_price_by_attribute_value_impact( $value_id, \S::normalize_decimal( $impact_on_the_price[$i] ) );

          $langs = \admin\factory\Languages::languages_list();
          if ( \S::is_array_fix( $langs ) ) foreach ( $langs as $lang )
          {
            if ( $names[$lang['id']][$i] )
            {
              $mdb -> insert('pp_shop_attributes_values_langs', [
                'value_id' => $value_id,
                'lang_id' => $lang['id'],
                'name' => $names[$lang['id']][$i],
                'value' => $values[$lang['id']][$i] ? $values[$lang['id']][$i] : null,
              ] );
            }
          }

          if ( $default_value == $i )
            $default_value_id = $value_id;
        }
      }
    }

    if ( $default_value_id )
    {
      $mdb -> update( 'pp_shop_attributes_values', [ 'is_default' => 0 ], [ 'attribute_id' => $attribute_id ] );
      $mdb -> update( 'pp_shop_attributes_values', [ 'is_default' => 1 ], [ 'id' => $default_value_id ] );
    }

    \S::delete_cache();

    return $attribute_id;
  }

  static public function get_attribute_values( int $attribute_id )
  {
    global $mdb;

    $results = $mdb -> select( 'pp_shop_attributes_values', [ 'id', 'is_default', 'impact_on_the_price' ], [ 'attribute_id' => $attribute_id ] );
    if ( \S::is_array_fix( $results ) ) foreach ( $results as $row )
    {
      $results2 = $mdb -> select( 'pp_shop_attributes_values_langs', [ 'lang_id', 'name', 'value' ], [ 'value_id' => $row['id'] ] );
      if ( \S::is_array_fix( $results2 ) ) foreach ( $results2 as $row2 )
        $row['languages'][$row2['lang_id']] = $row2;

      $values[] = $row;
    }

    return $values;
  }

  static public function delete_attribute( int $attribute_id )
  {
    global $mdb, $user;

    if ( $mdb -> delete( 'pp_shop_attributes', [ 'id' => $attribute_id ] ) )
    {
      \Log::save_log( 'Atrybut został usunięty | ID: ' . $attribute_id, $user['id'] );
      return true;
    }
    return false;
  }

  static public function attribute_save( int $attribute_id, $name, int $status, int $type, int $o )
  {
    global $mdb, $user;

    if ( !$attribute_id )
    {
      $mdb -> insert( 'pp_shop_attributes', [
        'status' => $status,
        'type' => $type,
        'o' => $o
      ] );

      $id = $mdb -> id();

      if ( !$id )
        return false;

      \Log::save_log( 'Dodano nowy atrybut | ID: ' . $id, $user['id'] );

      foreach ( $name as $key => $val )
      {
        $mdb -> insert( 'pp_shop_attributes_langs', [
          'attribute_id' => (int)$id,
          'lang_id' => $key,
          'name' => $name[$key],
        ] );
      }

      \S::delete_dir( '../temp/' );

      return $id;
    }
    else
    {
      $mdb -> update( 'pp_shop_attributes', [
        'status' => $status,
        'type' => $type,
        'o' => $o
      ], [
        'id' => $attribute_id,
      ] );

      \Log::save_log( 'Zaktualizowano atrybut | ID: ' . $attribute_id, $user['id'] );

      foreach ( $name as $key => $val )
      {
        if ( $translation_id = $mdb -> get( 'pp_shop_attributes_langs', 'id', [ 'AND' => [ 'attribute_id' => $attribute_id, 'lang_id' => $key ] ] ) )
          $mdb -> update( 'pp_shop_attributes_langs', [
            'lang_id' => $key,
            'name' => $name[$key],
          ], [
            'id' => $translation_id
          ] );
        else
        $mdb -> insert( 'pp_shop_attributes_langs', [
          'attribute_id' => (int)$attribute_id,
          'lang_id' => $key,
          'name' => $name[$key],
        ] );
      }

      \S::delete_dir( '../temp/' );

      return $attribute_id;
    }
  }

  static public function value_details( int $value_id )
  {
    global $mdb;

    $value = $mdb -> get( 'pp_shop_attributes_values', '*', [ 'id' => (int) $value_id ] );

    $results = $mdb -> select( 'pp_shop_attributes_values_langs', [ 'lang_id', 'name', 'value' ], [ 'value_id' => (int) $value_id ] );
    if ( \S::is_array_fix( $results ) ) foreach ( $results as $row)
      $value['languages'][$row['lang_id']] = $row;

    return $value;
  }

  static public function attribute_details( int $attribute_id )
  {
    global $mdb;

    $attribute = $mdb -> get( 'pp_shop_attributes', '*', [ 'id' => (int) $attribute_id ] );

    $results = $mdb -> select( 'pp_shop_attributes_langs', [ 'lang_id', 'name' ], [ 'attribute_id' => (int) $attribute_id ] );
    if ( \S::is_array_fix( $results ) ) foreach ( $results as $row)
      $attribute['languages'][$row['lang_id']] = $row;

    return $attribute;
  }
}
