<?php
error_reporting( E_ALL & ~E_NOTICE );
function __autoload_my_classes( $classname )
{
  $q = explode( '\\' , $classname );
  $c = array_pop( $q );
  $f = 'autoload/' . implode( '/' , $q ) . '/class.' . $c . '.php';

  if ( file_exists( $f ) )
    require_once( $f );
  else
  {
    $f = 'autoload/' . implode( '/' , $q ) . '/' . $c . '.php';
    if ( file_exists( $f ) )
      require_once( $f );
  }
}
spl_autoload_register( '__autoload_my_classes' );
date_default_timezone_set( 'Europe/Warsaw' );

require_once 'config.php';
require_once 'libraries/medoo/medoo.php';
require_once 'libraries/phpmailer/class.phpmailer.php';
require_once 'libraries/phpmailer/class.smtp.php';

session_start();

$mdb = new medoo( [
                    'database_type' => 'mysql',
                    'database_name' => $database['name'],
                    'server'        => $database['host'],
                    'username'      => $database['user'],
                    'password'      => $database['password'],
                    'charset'       => 'utf8'
                ] );

$langRepo = new \Domain\Languages\LanguagesRepository( $mdb );

if ( !$lang_id = \Shared\Helpers\Helpers::get_session( 'current-lang' ) )
{
  $lang_id = $langRepo->defaultLanguage();
  \Shared\Helpers\Helpers::set_session( 'current-lang', $lang_id );
}

if ( !$lang = \Shared\Helpers\Helpers::get_session( 'lang' ) )
{
  $lang = $langRepo->translations();
  \Shared\Helpers\Helpers::set_session( 'lang', $lang );
}

\front\controls\Site::check_url_params();

if ( !$settings = \Shared\Helpers\Helpers::get_session( 'settings' ) )
{
  $settings = ( new \Domain\Settings\SettingsRepository( $mdb ) )->allSettings();
  \Shared\Helpers\Helpers::set_session( 'settings', $settings );
}

$a = \Shared\Helpers\Helpers::get( 'a' );

if ( $a == 'basket_change_transport' )
{
    \Shared\Helpers\Helpers::set_session( 'transport_id', \Shared\Helpers\Helpers::get( 'transport_id' ) );

    $basket         = \Shared\Helpers\Helpers::get_session( 'basket' );
    $basket_summary = \front\factory\ShopBasket::summary_price( $basket, null );
    $transport_cost = \front\factory\ShopTransport::transport_cost( \Shared\Helpers\Helpers::get( 'transport_id' ) );

    echo json_encode( [ 'summary' => \Shared\Helpers\Helpers::decimal( $basket_summary + $transport_cost ) . ' zł' ] );
    exit;
}

if ( $a == 'change_payment' )
{
    \Shared\Helpers\Helpers::set_session( 'payment_method_id', \Shared\Helpers\Helpers::get( 'payment_method_id' ) );
    $transports = \front\factory\ShopTransport::transports_list( \Shared\Helpers\Helpers::get( 'payment_method_id' ) );
    echo json_encode( [ 'transports' => $transports ] );
    exit;
}

if ( $a == 'contact_form' )
{
  if ( ( !\Shared\Helpers\Helpers::get( 'email' ) and \Shared\Helpers\Helpers::get( 'phone' ) ) )
    $response = [ 'status' => 'error' ];
  else
  {
    if ( \Shared\Helpers\Helpers::send_email( $settings['contact_email'],  \Shared\Helpers\Helpers::get( 'subject' ), \Shared\Helpers\Helpers::get( 'text' ) . '<p>Nr telefonu: ' . \Shared\Helpers\Helpers::get( 'phone' ) . '</p><p>Email: ' . \Shared\Helpers\Helpers::get( 'email' ) . '</p>', \Shared\Helpers\Helpers::get( 'email' ) ) )
      $response = [ 'status' => 'ok' ];
  }

  echo json_encode( $response );
  exit;
}

if ( $a == 'cookie_close' )
{
  setcookie( "cookie_information", "true", mktime( 0, 0, 0, 12, 31, 2115 ) );
  exit;
}

if ( $a == 'banner_close' )
{
  \Shared\Helpers\Helpers::set_session( 'banner_close', true );
}
?>