<?php
namespace front\controls;
class ShopClient
{
  public static function mark_address_as_current()
  {
    if ( !$client = \S::get_session( 'client' ) )
      return false;

    \front\factory\ShopClient::mark_address_as_current( $client['id'], \S::get( 'address_id' ) );
    exit;
  }

  public static function address_delete()
  {
    if ( !$client = \S::get_session( 'client' ) )
    {
      header( 'Location: /logowanie' );
      exit;
    }

    $address = \front\factory\ShopClient::address_details( \S::get( 'id' ) );
    if ( $address['client_id'] != $client['id'] )
    {
      header( 'Location: /panel-klienta/adresy' );
      exit;
    }

    if ( \front\factory\ShopClient::address_delete( \S::get( 'id' ) ) )
      \S::alert( \S::lang( 'adres-usuniety-komunikat' ) );
    else
      \S::error( \S::lang( 'adres-usuniety-blad' ) );
    header( 'Location: /panel-klienta/adresy' );
    exit;
  }

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

    $page['language']['meta_title'] = \S::lang( 'meta-title-edycja-adresu' ) . ' | ' . $settings['firm_name'];

    if ( !$client = \S::get_session( 'client' ) )
    {
      header( 'Location: /logowanie' );
      exit;
    }

    $address = \front\factory\ShopClient::address_details( \S::get( 'id' ) );
    if ( $address['client_id'] != $client['id'] )
      unset( $address );

    return \front\view\ShopClient::address_edit( [
              'address' => \front\factory\ShopClient::address_details( \S::get( 'id' ) )
            ] );
  }

  public static function address_save()
  {
    if ( !$client = \S::get_session( 'client' ) )
    {
      header( 'Location: /logowanie' );
      exit;
    }

    if ( \front\factory\ShopClient::address_save( $client['id'], \S::get( 'address_id' ), \S::get( 'name', true ), \S::get( 'surname', true ), \S::get( 'firm', true ), \S::get( 'street' ), \S::get( 'postal_code', true ), \S::get( 'city', true ), \S::get( 'phone', true ) ) )
    {
      \S::get( 'address_id' ) ? \S::alert( \S::lang( 'zmiana-adresu-sukces' ) ) : \S::alert( \S::lang( 'dodawanie-nowego-adresu-sukces' ) );
    }
    else
    {
      \S::get( 'address_id' ) ? \S::error( \S::lang( 'zmiana-adresu-blad' ) ) : \S::error( \S::lang( 'dodawanie-nowego-adresu-blad' ) );
    }

    header( 'Location: /panel-klienta/adresy' );
    exit;
  }

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

    $page['language']['meta_title'] = \S::lang( 'meta-title-lista-adresow' ) . ' | ' . $settings['firm_name'];

    if ( !$client = \S::get_session( 'client' ) )
    {
      header( 'Location: /logowanie' );
      exit;
    }

    return \front\view\ShopClient::client_addresses( [
              'client' => $client,
              'addresses' => \front\factory\ShopClient::client_addresses( $client['id'] )
            ] );
  }

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

    $page['language']['meta_title'] = \S::lang( 'meta-title-historia-zamowien' ) . ' | ' . $settings['firm_name'];

    if ( !$client = \S::get_session( 'client' ) )
    {
      header( 'Location: /logowanie' );
      exit;
    }

    return \front\view\ShopClient::client_orders( [
              'client'    => $client,
              'orders'    => \front\factory\ShopClient::client_orders( $client['id'] ),
              'statuses'  => \shop\Order::order_statuses()
            ] );
  }

  public static function new_password()
  {
    if ( \front\factory\ShopClient::new_password( \S::get( 'hash' ) ) )
      \S::alert( \S::lang( 'nowe-haslo-zostalo-wyslane-na-twoj-adres-email' ) );

    header( 'Location: /logowanie' );
    exit;
  }

  public static function send_email_password_recovery()
  {
    if ( \front\factory\ShopClient::send_email_password_recovery( \S::get( 'email' ) ) )
      \S::alert( \S::lang( 'odzyskiwanie-hasla-link-komunikat' ) );
    else
      \S::alert( \S::lang( 'odzyskiwanie-hasla-blad' ) );
    header( 'Location: /logowanie' );
    exit;
  }

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

    $page['language']['meta_title'] = \S::lang( 'meta-title-odzyskiwanie-hasla' ) . ' | ' . $settings['firm_name'];

    return \front\view\ShopClient::recover_password();
  }

  public static function logout()
  {
    \S::delete_session( 'client' );
    header( 'Location: /' );
    exit;
  }

  public static function login()
  {
    if ( !\front\factory\ShopClient::login( \S::get( 'email' ), \S::get( 'password' ) ) )
      header( 'Location: /logowanie' );
    else
    {
      $client = \S::get_session( 'client' );
      if ( $redirect = \S::get( 'redirect' ) )
        header( 'Location: ' . $redirect );
      else
        header( 'Location: /panel-klienta' );
    }
    exit;
  }

  public static function confirm()
  {
    if ( \front\factory\ShopClient::register_confirm( \S::get( 'hash' ) ) )
      \S::alert( \S::lang( 'rejestracja-potwierdzenie' ) );

    header( 'Location: /logowanie' );
    exit;
  }

  public static function signup()
  {
    $result = \front\factory\ShopClient::signup( \S::get( 'email' ), \S::get( 'password' ), \S::get( 'agremment_marketing' ) );
    echo json_encode( $result );
    exit;
  }

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

    $page['language']['meta_title'] = \S::lang( 'meta-title-logowanie' ) . ' | ' . $settings['firm_name'];
    $page['class'] = 'page-login-form';

    if ( $client = \S::get_session( 'client' ) )
    {
      header( 'Location: /panel-klienta/zamowienia' );
      exit;
    }

    return \front\view\ShopClient::login_form();
  }

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

    $page['language']['meta_title'] = \S::lang( 'meta-title-rejestracja' ) . ' | ' . $settings['firm_name'];

    if ( $client = \S::get_session( 'client' ) )
    {
      header( 'Location: /panel-klienta/zamowienia' );
      exit;
    }

    return \front\view\ShopClient::register_form();
  }
}
