<?php
namespace front\factory;
class ShopTransport
{
  // get_sellasist_transport_id
  static public function get_sellasist_transport_id( $transport_method_id ) {
    global $mdb;
    return $mdb -> get( 'pp_shop_transports', 'sellasist_shipment_method_id', [ 'id' => $transport_method_id ] );
  }

  // get_apilo_carrier_account_id
  static public function get_apilo_carrier_account_id( $transport_method_id )
  {
    global $mdb;
    return $mdb -> get( 'pp_shop_transports', 'apilo_carrier_account_id', [ 'id' => $transport_method_id ] );
  }

  public static function transport_methods( $basket, $coupon )
  {
    global $mdb, $settings;

    $cacheHandler = new \CacheHandler();
    $cacheKey = "\front\factory\ShopTransport::transport_methods";

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

    if ( !$objectData )
    {
      $results = $mdb -> query( 'SELECT '
                                    . 'pst.id, name, name_visible, description, cost, max_wp, pst.default, delivery_free '
                                  . 'FROM '
                                    . 'pp_shop_transports AS pst '
                                  . 'WHERE '
                                      . 'status = 1' ) -> fetchAll( \PDO::FETCH_ASSOC );
      if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
        $transports_tmp[] = $row;

      $cacheHandler -> set( $cacheKey, $transports_tmp );
    }
    else
    {
      $transports_tmp = unserialize( $objectData );
    }

    $wp_summary = \front\factory\ShopBasket::summary_wp( $basket );

    foreach ( $transports_tmp as $tr )
    {
      if ( $tr['max_wp'] == null )
        $transports[] = $tr;
      elseif ( $tr['max_wp'] != null and $wp_summary <= $tr['max_wp'] )
        $transports[] = $tr;
    }


    if ( \S::normalize_decimal( \front\factory\ShopBasket::summary_price( $basket, $coupon ) ) >= \S::normalize_decimal( $settings['free_delivery'] ) )
    {
      for ( $i = 0; $i < count( $transports ); $i++ ){
        if($transports[ $i ]['delivery_free'] == 1) {
          $transports[ $i ]['cost'] = 0.00;
        }
      }
    }

    return $transports;
  }

  public static function transport_cost( $transport_id )
  {
      global $mdb;

      if ( !$cost = \Cache::fetch( 'transport_cost_' . $transport_id ) )
      {
        $cost = $mdb -> get( 'pp_shop_transports', 'cost', [
                          'AND' => [
                              'id' => $transport_id,
                              'status' => 1
                          ] ] );

        \Cache::store( 'transport_cost_' . $transport_id, $cost );
      }
      return $cost;
  }

  public static function transport( $transport_id )
  {
    global $mdb;

    if ( !$transport = \Cache::fetch( 'transport' . $transport_id ) )
    {
      $transport = $mdb -> get( 'pp_shop_transports', '*', [
                        'AND' => [
                            'id' => $transport_id,
                            'status' => 1
                        ] ] );

      \Cache::store( 'transport' . $transport_id, $transport );
    }
    return $transport;
  }
}
