<?php
namespace front\factory;
class ShopCategory
{
  static public function get_category_sort( int $category_id )
  {
    global $mdb;

    $cacheHandler = new \Shared\Cache\CacheHandler();
    $cacheKey = "get_category_sort:$category_id";
    $objectData = $cacheHandler->get( $cacheKey );

    if ( !$objectData )
    {
      $category_sort = $mdb -> get( 'pp_shop_categories', 'sort_type', [ 'id' => $category_id ] );
      $cacheHandler->set( $cacheKey, $category_sort );
    }
    else
    {
      return unserialize( $objectData );
    }

    return $category_sort;
  }

  public static function category_name( $category_id )
  {
    global $mdb, $lang_id;

    $cacheHandler = new \Shared\Cache\CacheHandler();
    $cacheKey = 'category_name' . $lang_id . '_' . $category_id;
    $objectData = $cacheHandler->get( $cacheKey );

    if ( !$objectData )
    {
      $category_name = $mdb -> get( 'pp_shop_categories_langs', 'title', [ 'AND' => [ 'category_id' => (int)$category_id, 'lang_id' => $lang_id ] ] );

      $cacheHandler->set( $cacheKey, $category_name );
    }
    else
    {
      return unserialize( $objectData );
    }

    return $category_name;
  }

  public static function category_url( $category_id ) {

    $category = self::category_details( $category_id );

    $category['language']['seo_link'] ? $url = '/' . $category['language']['seo_link'] : $url = '/k-' . $category['id'] . '-' . \Shared\Helpers\Helpers::seo( $category['language']['title'] );

    if ( \Shared\Helpers\Helpers::get_session( 'current-lang' ) != ( new \Domain\Languages\LanguagesRepository( $GLOBALS['mdb'] ) )->defaultLanguage() and $url != '#' )
      $url = '/' . \Shared\Helpers\Helpers::get_session( 'current-lang' ) . $url;

    return $url;
  }

  public static function blog_category_products( $category_id, $lang_id, $limit )
  {
    global $mdb;

    $cacheHandler = new \Shared\Cache\CacheHandler();
    $cacheKey = "\front\factory\ShopCategory::blog_category_products:$category_id:$lang_id:$limit";

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

    if ( !$objectData )
    {
      $results = $mdb -> query( 'SELECT * FROM ( '
                                      . 'SELECT '
                                          . 'psp.id, date_modify, date_add, o, '
                                          . '( CASE '
                                            . 'WHEN copy_from IS NULL THEN name '
                                            . 'WHEN copy_from IS NOT NULL THEN ( '
                                              . 'SELECT '
                                                . 'name '
                                              . 'FROM '
                                                . 'pp_shop_products_langs '
                                              . 'WHERE '
                                                . 'lang_id = pspl.copy_from AND product_id = psp.id '
                                              . ') '
                                          . 'END ) AS name '
                                      . 'FROM '
                                        . 'pp_shop_products_categories AS pspc '
                                        . 'INNER JOIN pp_shop_products AS psp ON psp.id = pspc.product_id '
                                        . 'INNER JOIN pp_shop_products_langs AS pspl ON pspl.product_id = pspc.product_id '
                                      . 'WHERE '
                                        . 'status = 1 AND category_id = ' . (int)$category_id . ' AND lang_id = \'' . $lang_id . '\' '
                                  . ') AS q1 '
                                  . 'WHERE '
                                    . 'q1.name IS NOT NULL '
                                  . 'ORDER BY '
                                    . 'RAND() '
                                  . 'LIMIT ' . (int)$limit ) -> fetchAll();
      if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
        $output[] = $row['id'];

      $cacheHandler -> set( $cacheKey, $output );
    }
    else
    {
      return unserialize( $objectData );
    }

    return $output;
  }

  public static function category_products_count( $category_id, $lang_id )
  {
    global $mdb;

    $cacheHandler = new \Shared\Cache\CacheHandler();
    $cacheKey = "\front\factory\ShopCategory::category_products_count:$category_id:$lang_id";

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

    if ( !$objectData )
    {

      $results = $mdb -> query( 'SELECT COUNT(0) FROM ( '
                                      . 'SELECT '
                                          . 'psp.id, '
                                          . '( CASE '
                                            . 'WHEN copy_from IS NULL THEN name '
                                            . 'WHEN copy_from IS NOT NULL THEN ( '
                                              . 'SELECT '
                                                . 'name '
                                              . 'FROM '
                                                . 'pp_shop_products_langs '
                                              . 'WHERE '
                                                . 'lang_id = pspl.copy_from AND product_id = psp.id '
                                              . ') '
                                           . 'END ) AS name '
                                      . 'FROM '
                                        . 'pp_shop_products_categories AS pspc '
                                        . 'INNER JOIN pp_shop_products AS psp ON psp.id = pspc.product_id '
                                        . 'INNER JOIN pp_shop_products_langs AS pspl ON pspl.product_id = pspc.product_id '
                                      . 'WHERE '
                                        . 'status = 1 AND category_id = ' . (int)$category_id . ' AND lang_id = \'' . $lang_id . '\' '
                                  . ') AS q1 '
                                  . 'WHERE '
                                    . 'q1.name IS NOT NULL' ) -> fetchAll();
      $products_count = $results[0][0];

      $cacheHandler -> set( $cacheKey, $products_count );
    }
    else
    {
      return unserialize( $objectData );
    }

    return $products_count;
  }

  public static function products_id( $category_id, $sort_type, $lang_id, $products_limit, $from )
  {
    global $mdb;

    switch ( $sort_type ):
      case 0:
        $order = 'q1.date_add ASC ';
      break;
      case 1:
        $order = 'q1.date_add DESC ';
      break;
      case 2:
        $order = 'q1.date_modify ASC ';
      break;
      case 3:
        $order = 'q1.date_modify DESC ';
      break;
      case 4:
        $order = 'q1.o ASC ';
      break;
      case 5:
        $order = 'q1.name ASC ';
      break;
      case 6:
        $order = 'q1.name DESC ';
      break;
    endswitch;

    $cacheHandler = new \Shared\Cache\CacheHandler();
    $cacheKey = "\front\factory\ShopCategory::products_id:$category_id:$sort_type:$lang_id:$products_limit:$from:$order";

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

    if ( !$objectData )
    {
      $results = $mdb -> query( 'SELECT * FROM ( '
                                  . 'SELECT '
                                      . 'psp.id, date_modify, date_add, o, '
                                      . '( CASE '
                                        . 'WHEN copy_from IS NULL THEN name '
                                        . 'WHEN copy_from IS NOT NULL THEN ( '
                                          . 'SELECT '
                                            . 'name '
                                          . 'FROM '
                                            . 'pp_shop_products_langs '
                                          . 'WHERE '
                                            . 'lang_id = pspl.copy_from AND product_id = psp.id '
                                          . ') '
                                      . 'END ) AS name, '
                                      . '( CASE '
                                          . 'WHEN new_to_date >= \'' . date( 'Y-m-d' ) . '\' THEN new_to_date '
                                          . 'WHEN new_to_date < \'' . date( 'Y-m-d' ) . '\' THEN null '
                                        . 'END ) '
                                        . 'AS new_to_date, '
                                      . '( CASE WHEN ( quantity + ( SELECT IFNULL(SUM(quantity),0) FROM pp_shop_products WHERE parent_id = psp.id ) ) > 0 THEN 1 ELSE 0 END ) AS total_quantity '
                                  . 'FROM '
                                    . 'pp_shop_products_categories AS pspc '
                                    . 'INNER JOIN pp_shop_products AS psp ON psp.id = pspc.product_id '
                                    . 'INNER JOIN pp_shop_products_langs AS pspl ON pspl.product_id = pspc.product_id '
                                  . 'WHERE '
                                    . 'status = 1 AND category_id = ' . (int)$category_id . ' AND lang_id = \'' . $lang_id . '\' '
                              . ') AS q1 '
                              . 'WHERE '
                                . 'q1.name IS NOT NULL '
                              . 'ORDER BY '
                                . $order
                              . 'LIMIT '
                                . (int)$from . ',' . (int)$products_limit ) -> fetchAll();
      if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
        $output[] = $row['id'];

      $cacheHandler -> set( $cacheKey, $output );
    }
    else
    {
      return unserialize( $objectData );
    }

    return $output;
  }

  public static function category_products( $category, $lang_id, $bs )
  {
    $count  = \front\factory\ShopCategory::category_products_count( $category['id'], $lang_id );
    $ls     = ceil( $count / 12 );

    if ( $bs < 1 )
      $bs = 1;
    else if ( $bs > $ls )
      $bs = $ls;

    $from = 12 * ( $bs - 1 );

    if ( $from < 0 )
      $from = 0;

    $results['products'] = \front\factory\ShopCategory::products_id( (int)$category['id'], $category['sort_type'], $lang_id, 12, $from );

    $results['ls'] = $ls;

    return $results;
  }

  public static function categories_details( $parent_id = null )
  {
    global $mdb;

    $cacheHandler = new \Shared\Cache\CacheHandler();
    $cacheKey = "\front\factory\ShopCategory::categories_details:$parent_id";

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

    if ( !$objectData )
    {
      $results = $mdb -> select( 'pp_shop_categories', 'id', [ 'parent_id' => $parent_id, 'ORDER' => [ 'o' => 'ASC' ] ] );
      if ( is_array( $results ) ) foreach ( $results as $row )
      {
        $category = \front\factory\ShopCategory::category_details( $row );
        $category['categories'] = \front\factory\ShopCategory::categories_details( $row );

        $categories[]= $category;
      }

      $cacheHandler -> set( $cacheKey, $categories );
    }
    else
    {
      return unserialize( $objectData );
    }

    return $categories;
  }

  public static function category_details( $category_id )
  {
    global $mdb, $lang_id;

    $cacheHandler = new \Shared\Cache\CacheHandler();
    $cacheKey = "\front\factory\ShopCategory::category_details:$category_id";

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

    if ( !$objectData )
    {
      $category = $mdb -> get( 'pp_shop_categories', '*', [ 'id' => (int)$category_id ] );
      $category['language'] = $mdb -> get( 'pp_shop_categories_langs', '*', [ 'AND' => [ 'category_id' => (int)$category_id, 'lang_id' => $lang_id ] ] );

      $cacheHandler -> set( $cacheKey, $category );
    }
    else
    {
      return unserialize( $objectData );
    }

    return $category;
  }
}
