<?php
namespace front\controls;

class ShopBasket
{
  public static $title = [
      'main_view' => 'Koszyk'
  ];

  public static function basket_message_save()
  {
    \Shared\Helpers\Helpers::set_session( 'basket_message', \Shared\Helpers\Helpers::get( 'basket_message' ) );
    echo json_encode( [
        'result' => 'ok'
    ] );
    exit;
  }

  public static function basket_remove_product()
  {
    global $lang_id;

    $basket = \Shared\Helpers\Helpers::get_session( 'basket' );
    $coupon = \Shared\Helpers\Helpers::get_session( 'coupon' );
    $product_hash = \Shared\Helpers\Helpers::get( 'product_hash' );
    $basket_transport_method_id = \Shared\Helpers\Helpers::get_session( 'basket-transport-method-id' );

    unset( $basket[ $product_hash ] );

    $basket = \shop\Promotion::find_promotion( $basket );

    \Shared\Helpers\Helpers::set_session( 'basket', $basket );

    echo json_encode( [
        'basket'            => \Shared\Tpl\Tpl::view( 'shop-basket/basket-details', [
            'basket'  => $basket,
            'lang_id' => $lang_id,
            'coupon'  => $coupon
        ] ),
        'basket_mini_count' => \front\factory\ShopBasket::count_products_text( \front\factory\ShopBasket::count_products( $basket ) ),
        'basket_mini_value' => \front\factory\ShopBasket::summary_price( $basket, $coupon ),
        'products_count'    => count( $basket ),
        'transport_methods' => \Shared\Tpl\Tpl::view( 'shop-basket/basket-transport-methods', [
            'transports_methods' => \front\factory\ShopTransport::transport_methods( $basket, $coupon ),
            'transport_id'       => $basket_transport_method_id
        ] )
    ] );
    exit;
  }

  public static function basket_increase_quantity_product()
  {
    global $lang_id;

    $basket = \Shared\Helpers\Helpers::get_session( 'basket' );
    $coupon = \Shared\Helpers\Helpers::get_session( 'coupon' );
    $product_hash = \Shared\Helpers\Helpers::get( 'product_hash' );
    $basket_transport_method_id = \Shared\Helpers\Helpers::get_session( 'basket-transport-method-id' );
    $basket[ $product_hash ][ 'quantity' ]++;

    \shop\Basket::check_product_quantity_in_stock( $basket, false );

    $basket = \Shared\Helpers\Helpers::get_session( 'basket' );

    $basket = \shop\Promotion::find_promotion( $basket );

    \Shared\Helpers\Helpers::set_session( 'basket', $basket );

    echo json_encode( [
        'basket'            => \Shared\Tpl\Tpl::view( 'shop-basket/basket-details', [
            'basket'  => $basket,
            'lang_id' => $lang_id,
            'coupon'  => $coupon
        ] ),
        'basket_mini_count' => \front\factory\ShopBasket::count_products_text( \front\factory\ShopBasket::count_products( $basket ) ),
        'basket_mini_value' => \front\factory\ShopBasket::summary_price( $basket, $coupon ),
        'products_count'    => count( $basket ),
        'transport_methods' => \Shared\Tpl\Tpl::view( 'shop-basket/basket-transport-methods', [
            'transports_methods' => \front\factory\ShopTransport::transport_methods( $basket, $coupon ),
            'transport_id'       => $basket_transport_method_id
        ] )
      ]
    );
    exit;
  }

  public static function basket_decrease_quantity_product()
  {
    global $lang_id;

    $basket = \Shared\Helpers\Helpers::get_session( 'basket' );
    $coupon = \Shared\Helpers\Helpers::get_session( 'coupon' );
    $product_hash = \Shared\Helpers\Helpers::get( 'product_hash' );
    $basket_transport_method_id = \Shared\Helpers\Helpers::get_session( 'basket-transport-method-id' );

    $basket[ $product_hash ][ 'quantity' ]--;

    if ( $basket[ $product_hash ][ 'quantity' ] < 1 )
      unset( $basket[ $product_hash ] );

    $basket = \shop\Promotion::find_promotion( $basket );

    \Shared\Helpers\Helpers::set_session( 'basket', $basket );

    echo json_encode( [
        'basket'            => \Shared\Tpl\Tpl::view( 'shop-basket/basket-details', [
            'basket'  => $basket,
            'lang_id' => $lang_id,
            'coupon'  => $coupon
        ] ),
        'basket_mini_count' => \front\factory\ShopBasket::count_products_text( \front\factory\ShopBasket::count_products( $basket ) ),
        'basket_mini_value' => \front\factory\ShopBasket::summary_price( $basket, $coupon ),
        'products_count'    => count( $basket ),
        'transport_methods' => \Shared\Tpl\Tpl::view( 'shop-basket/basket-transport-methods', [
            'transports_methods' => \front\factory\ShopTransport::transport_methods( $basket, $coupon ),
            'transport_id'       => $basket_transport_method_id
        ] )
    ] );
    exit;
  }

  public static function basket_change_quantity_product()
  {
    global $lang_id;

    $basket = \Shared\Helpers\Helpers::get_session( 'basket' );
    $coupon = \Shared\Helpers\Helpers::get_session( 'coupon' );
    $product_hash = \Shared\Helpers\Helpers::get( 'product_hash' );
    $basket_transport_method_id = \Shared\Helpers\Helpers::get_session( 'basket-transport-method-id' );

    $basket[ $product_hash ][ 'quantity' ] = (int)\Shared\Helpers\Helpers::get( 'quantity' );

    if ( $basket[ $product_hash ][ 'quantity' ] < 1 )
      unset( $basket[ $product_hash ] );

    $basket = \shop\Promotion::find_promotion( $basket );

    \shop\Basket::check_product_quantity_in_stock( $basket, false );

    $basket = \Shared\Helpers\Helpers::get_session( 'basket' );

    echo json_encode( [
        'basket'            => \Shared\Tpl\Tpl::view( 'shop-basket/basket-details', [
            'basket'  => $basket,
            'lang_id' => $lang_id,
            'coupon'  => $coupon
        ] ),
        'basket_mini_count' => \front\factory\ShopBasket::count_products_text( \front\factory\ShopBasket::count_products( $basket ) ),
        'basket_mini_value' => \front\factory\ShopBasket::summary_price( $basket, $coupon ),
        'products_count'    => count( $basket ),
        'transport_methods' => \Shared\Tpl\Tpl::view( 'shop-basket/basket-transport-methods', [
            'transports_methods' => \front\factory\ShopTransport::transport_methods( $basket, $coupon ),
            'transport_id'       => $basket_transport_method_id
        ] )
    ] );
    exit;
  }

  static public function product_message_change()
  {
    $basket = \Shared\Helpers\Helpers::get_session( 'basket' );
    $basket[ \Shared\Helpers\Helpers::get( 'position_code' ) ]['message'] = \Shared\Helpers\Helpers::get( 'product_message' );
    \Shared\Helpers\Helpers::set_session( 'basket', $basket );
    exit;
  }

  public static function basket_add_product()
  {
    $basket = \shop\Basket::validate_basket( \Shared\Helpers\Helpers::get_session( 'basket' ) );
    $values_tmp = json_decode( \Shared\Helpers\Helpers::get( 'values' ), true );

    foreach( $values_tmp as $key => $val )
      $values[ $val['name'] ] = $val['value'];

    // sprawdzam pola pod kątem wybranych atrybutów
    foreach( $values as $key => $val )
    {
      if ( $key != 'product-id' and $key != 'quantity' and $key != 'product-message' and strpos( $key, 'custom_field' ) === false )
        $attributes[] = $val;
    }

    // stwórz tablicę dodatkowych pól wyszukując na podstawie custom_field[1], custom_field[2] itd.
    foreach( $values as $key => $val )
    {
      if ( strpos( $key, 'custom_field' ) !== false )
      {
        // extract number from custom_field[1], custom_field[2] etc.
        preg_match( '/\d+/', $key, $matches );
        $custom_field_id = $matches[0];

        $custom_fields[ $custom_field_id ] = $val;
      }
    }

    if ( \Shared\Helpers\Helpers::is_array_fix( $attributes ) )
    {
      $values['parent_id'] = $values[ 'product-id' ];
      $values['product-id'] = \shop\Product::get_product_id_by_attributes( $values[ 'product-id' ], $attributes );
      $values['attributes'] = $attributes;
    }


    $values['wp'] = \front\factory\ShopProduct::product_wp( $values[ 'product-id' ] );

    $attributes_implode = '';
    // generuj unikalny kod produktu dodanego do koszyka
    if ( is_array( $attributes ) )
      $attributes_implode = implode( '|', $attributes );

    $product_code = md5( $values['product-id'] . $attributes_implode . $values['product-message'] . json_encode( $custom_fields ) );

    if ( isset( $basket[ $product_code ] ) )
      $basket[ $product_code ][ 'quantity' ] += $values[ 'quantity' ];
    else
      $basket[ $product_code ] = $values;

    $basket[ $product_code ]['message'] = $values['product-message'];
    $basket[ $product_code ]['custom_fields'] = $custom_fields;

    $basket = \shop\Promotion::find_promotion( $basket );

    \Shared\Helpers\Helpers::set_session( 'basket', $basket );

    $coupon = \Shared\Helpers\Helpers::get_session( 'coupon' );

    echo json_encode( [
        'result' => 'ok',
        'basket_mini_count' => \front\factory\ShopBasket::count_products_text( \front\factory\ShopBasket::count_products( $basket ) ),
        'basket_mini_value' => \front\factory\ShopBasket::summary_price( $basket, $coupon ),
        'product_sets' => \shop\Product::product_sets_when_add_to_basket( (int)$values['product-id'] )
    ] );
    exit;
  }

  // sprawdzam czy została wybrana forma wysylki inpost i czy został wybrany paczkomat
  static public function transport_method_inpost_check()
  {
    if ( \Shared\Helpers\Helpers::get_session( 'basket-transport-method-id' ) === '2' or \Shared\Helpers\Helpers::get_session( 'basket-transport-method-id' ) === '1' )
    {
      if ( !\Shared\Helpers\Helpers::get_session( 'basket-inpost-info' ) )
      {
        echo json_encode( [
          'result' => 'bad'
        ] );
        exit;
      }
    }

    if ( \Shared\Helpers\Helpers::get_session( 'basket-transport-method-id' ) === '9' )
    {
      if ( !\Shared\Helpers\Helpers::get_session( 'basket_orlen_point_id' ) )
      {
        echo json_encode( [
          'result' => 'bad'
        ] );
        exit;
      }
    }

    echo json_encode( [
        'result' => 'ok'
    ] );
    exit;
  }

  // sprawdzam czy został wybrany paczkomat
  static public function inpost_check() {
    if ( !\Shared\Helpers\Helpers::get_session( 'basket-inpost-info' ) )
      echo json_encode( [
          'result' => 'bad'
      ] );
    else
      echo json_encode( [
          'result' => 'ok'
      ] );
    exit;
  }

  static public function orlen_save()
  {
    \Shared\Helpers\Helpers::set_session( 'basket_orlen_point_id', \Shared\Helpers\Helpers::get( 'orlen_point_id' ) );
    \Shared\Helpers\Helpers::set_session( 'basket_orlen_point_info', \Shared\Helpers\Helpers::get( 'orlen_point_name' ) );
    echo json_encode( [
      'result' => 'ok'
    ] );
    exit;
  }

  public static function inpost_save()
  {
    \Shared\Helpers\Helpers::set_session( 'basket-inpost-info', \Shared\Helpers\Helpers::get( 'paczkomat' ) );
    echo json_encode( [
        'result' => 'ok'
    ] );
    exit;
  }

  public static function basket_payment_method_set()
  {
    \Shared\Helpers\Helpers::set_session( 'basket-payment-method-id', \Shared\Helpers\Helpers::get( 'payment_method_id' ) );
    echo json_encode( [
        'result' => 'ok'
    ] );
    exit;
  }

  public static function basket_transport_method_set()
  {
    \Shared\Helpers\Helpers::set_session( 'basket-transport-method-id', \Shared\Helpers\Helpers::get( 'transport_method_id' ) );
    echo json_encode( [
        'result' => 'ok'
    ] );
    exit;
  }

  public static function basket_payments_methods()
  {
    \Shared\Helpers\Helpers::set_session( 'basket-transport-method-id', \Shared\Helpers\Helpers::get( 'transport_method_id' ) );

    echo json_encode( [
        'result'          => 'ok',
        'payment_methods' => \front\view\ShopPaymentMethod::basket_payment_methods(
          \front\factory\ShopPaymentMethod::payment_methods_by_transport( \Shared\Helpers\Helpers::get( 'transport_method_id' ) ),
          \Shared\Helpers\Helpers::get( 'payment_method_id' )
        )
    ] );
    exit;
  }

  public static function summary_view()
  {
    global $lang_id, $settings;

    if ( \shop\Basket::check_product_quantity_in_stock( \Shared\Helpers\Helpers::get_session( 'basket' ) ) )
    {
      header( 'Location: /koszyk' );
      exit;
    }

    $client = \Shared\Helpers\Helpers::get_session( 'client' );

    return \Shared\Tpl\Tpl::view( 'shop-basket/summary-view', [
          'lang_id'        => $lang_id,
          'client'         => \Shared\Helpers\Helpers::get_session( 'client' ),
          'basket'         => \Shared\Helpers\Helpers::get_session( 'basket' ),
          'transport'      => \front\factory\ShopTransport::transport( \Shared\Helpers\Helpers::get_session( 'basket-transport-method-id' ) ),
          'payment_method' => \front\factory\ShopPaymentMethod::payment_method( \Shared\Helpers\Helpers::get_session( 'basket-payment-method-id' ) ),
          'addresses'      => \front\factory\ShopClient::client_addresses( $client[ 'id' ] ),
          'settings'       => $settings,
          'coupon'         => \Shared\Helpers\Helpers::get_session( 'coupon' ),
          'basket_message' => \Shared\Helpers\Helpers::get_session( 'basket_message' )
      ] );
  }

  // zapisanie koszyka jako zamówienie
  static public function basket_save()
  {
    $client = \Shared\Helpers\Helpers::get_session( 'client' );
    $payment_method = \Shared\Helpers\Helpers::get_session( 'basket-payment-method-id' );

    if ( \shop\Basket::check_product_quantity_in_stock( \Shared\Helpers\Helpers::get_session( 'basket' ) ) )
    {
      header( 'Location: /koszyk' );
      exit;
    }

    if ( $order_id = \front\factory\ShopOrder::basket_save(
        $client[ 'id' ],
        \Shared\Helpers\Helpers::get_session( 'basket' ),
        \Shared\Helpers\Helpers::get_session( 'basket-transport-method-id' ),
        \Shared\Helpers\Helpers::get_session( 'basket-payment-method-id' ),
        \Shared\Helpers\Helpers::get( 'email', true ),
        \Shared\Helpers\Helpers::get( 'phone', true ),
        \Shared\Helpers\Helpers::get( 'name', true ),
        \Shared\Helpers\Helpers::get( 'surname', true ),
        \Shared\Helpers\Helpers::get( 'street' ),
        \Shared\Helpers\Helpers::get( 'postal_code', true ),
        \Shared\Helpers\Helpers::get( 'city', true ),
        \Shared\Helpers\Helpers::get( 'firm_name', true ),
        \Shared\Helpers\Helpers::get( 'firm_street', true ),
        \Shared\Helpers\Helpers::get( 'firm_postal_code', true ),
        \Shared\Helpers\Helpers::get( 'firm_city', true ),
        \Shared\Helpers\Helpers::get( 'firm_nip', true ),
        \Shared\Helpers\Helpers::get_session( 'basket-inpost-info' ),
        \Shared\Helpers\Helpers::get_session( 'basket_orlen_point_id' ),
        \Shared\Helpers\Helpers::get_session( 'basket_orlen_point_info' ),
        \Shared\Helpers\Helpers::get_session( 'coupon' ),
        \Shared\Helpers\Helpers::get_session( 'basket_message' )
      ) )
    {
      \Shared\Helpers\Helpers::alert( \Shared\Helpers\Helpers::lang( 'zamowienie-zostalo-zlozone-komunikat' ) );
      \Shared\Helpers\Helpers::delete_session( 'basket' );
      \Shared\Helpers\Helpers::delete_session( 'basket-transport-method-id' );
      \Shared\Helpers\Helpers::delete_session( 'basket-payment-method-id' );
      \Shared\Helpers\Helpers::delete_session( 'basket-inpost-info' );
      \Shared\Helpers\Helpers::delete_session( 'basket_orlen_point_id' );
      \Shared\Helpers\Helpers::delete_session( 'basket_orlen_point_info' );
      \Shared\Helpers\Helpers::delete_session( 'coupon' );
      \Shared\Helpers\Helpers::delete_session( 'basket_message' );

      \Shared\Helpers\Helpers::set_session( 'piksel_purchase', true );
      \Shared\Helpers\Helpers::set_session( 'google-adwords-purchase', true );
      \Shared\Helpers\Helpers::set_session( 'google-analytics-purchase', true );
      \Shared\Helpers\Helpers::set_session( 'ekomi-purchase', true );

      $redis = \Shared\Cache\RedisConnection::getInstance() -> getConnection();
      if ( $redis )
        $redis -> flushAll();

      header( 'Location: /zamowienie/' . \front\factory\ShopOrder::order_hash( $order_id ) );
      exit;
    }
    else
    {
      \Shared\Helpers\Helpers::error( \Shared\Helpers\Helpers::lang( 'zamowienie-zostalo-zlozone-komunikat-blad' ) );
      header( 'Location: /koszyk' );
      exit;
    }
  }

  public static function main_view()
  {
    global $lang_id, $page, $settings;

    $page[ 'language' ][ 'meta_title' ] = 'Koszyk';

    $basket = \Shared\Helpers\Helpers::get_session( 'basket' );
    $coupon = \Shared\Helpers\Helpers::get_session( 'coupon' );
    $payment_method_id = \Shared\Helpers\Helpers::get_session( 'payment_method_id' );
    $basket_transport_method_id = \Shared\Helpers\Helpers::get_session( 'basket-transport-method-id' );

    if ( \shop\Basket::check_product_quantity_in_stock( $basket ) )
    {
      header( 'Location: /koszyk' );
      exit;
    }

    $basket = \shop\Promotion::find_promotion( $basket );

    return \Shared\Tpl\Tpl::view( 'shop-basket/basket', [
          'basket'            => $basket,
          'coupon'            => $coupon,
          'transport_id'      => \Shared\Helpers\Helpers::get_session( 'basket-transport-method-id' ),
          'transport_methods' => \Shared\Tpl\Tpl::view( 'shop-basket/basket-transport-methods', [
              'transports_methods' => \front\factory\ShopTransport::transport_methods( $basket, $coupon ),
              'transport_id'       => $basket_transport_method_id
          ] ),
          'payment_method_id' => $payment_method_id,
          'basket_details'    => \Shared\Tpl\Tpl::view( 'shop-basket/basket-details', [
              'basket'         => $basket,
              'lang_id'        => $lang_id,
              'coupon'         => $coupon,
              'basket_message' => \Shared\Helpers\Helpers::get_session( 'basket_message' ),
              'settings' => $settings
          ] )
      ] );
  }

}