<?php
namespace shop;

class Promotion
{
  private $data = [];

  public static $condition_type = [
    1 => 'Rabat procentowy na produkty z kategorii 1 jeżeli w koszyku jest produkt z kategorii 2',
    2 => 'Rabat procentowy na produkty z kategorii 1 i 2',
    3 => 'Najtańszy produkt w koszyku (z wybranych kategorii) za X zł',
    4 => 'Rabat procentowy na cały koszyk',
    5 => 'Rabat procentowy na produkty z kategorii 1 lub 2',
  ];

  public static $discount_type = [ 1 => 'Rabat procentowy' ];

  public function __construct( $id )
  {
    global $mdb;

    if ( $id )
    {
      $result = $mdb->get( 'pp_shop_promotion', '*', [ 'id' => $id ] );
      if ( is_array( $result ) )
        $this->data = $result;
    }
  }

  public function __get( $variable )
  {
    if ( isset( $this->data[$variable] ) )
      return $this->data[$variable];
    return null;
  }

  public static function get_active_promotions()
  {
    global $mdb;

    $cacheHandler = new \Shared\Cache\CacheHandler();
    $cacheKey = "\shop\Promotion::get_active_promotions";

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

    if ( !$objectData )
    {
      $results = $mdb -> select( 'pp_shop_promotion', 'id', [ 'AND' => [ 'status' => 1, 'OR #date_from' => [ 'date_from' => null, 'date_from[<=]' => date( 'Y-m-d' ) ], 'OR #date_to' => [ 'date_to' => null, 'date_to[>=]' => date( 'Y-m-d' ) ] ], 'ORDER' => [ 'id' => 'DESC' ] ] );

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

    return $results;
  }

  public static function find_promotion( $basket )
  {
    global $mdb;

    foreach ( $basket as $key => $val )
    {
      unset( $basket[$key]['discount_type'] );
      unset( $basket[$key]['discount_amount'] );
      unset( $basket[$key]['discount_include_coupon'] );
      unset( $basket[$key]['include_product_promo'] );
    }

    $basket_tmp = $basket;

    $results = self::get_active_promotions();
    if ( is_array( $results ) and count( $results ) )
    {
      foreach ( $results as $row )
      {
        $promotion = new \shop\Promotion( $row );

        // Promocja na cały koszyk
        if ( $promotion -> condition_type == 4 )
          return \front\factory\ShopPromotion::promotion_type_05( $basket_tmp, $promotion );

        // Najtańszy produkt w koszyku (z wybranych kategorii) za X zł
        if ( $promotion -> condition_type == 3 )
          return \front\factory\ShopPromotion::promotion_type_04( $basket_tmp, $promotion );

        //  Rabat procentowy na produkty z kategorii 1 lub kategorii 2
        if ( $promotion -> condition_type == 5 )
          return \front\factory\ShopPromotion::promotion_type_03( $basket_tmp, $promotion );

        //  Rabat procentowy na produkty z kategorii I i kategorii II
        if ( $promotion -> condition_type == 2 )
          return \front\factory\ShopPromotion::promotion_type_02( $basket_tmp, $promotion );

        // Rabat procentowy na produkty z kategorii I jeżeli w koszyku jest produkt z kategorii II
        if ( $promotion -> condition_type == 1 )
          return \front\factory\ShopPromotion::promotion_type_01( $basket_tmp, $promotion );
      }
    }
    return $basket;
  }
}
