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

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

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

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

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

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

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

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

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

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

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

    if ( \front\factory\ShopClient::address_save( $client['id'], \Shared\Helpers\Helpers::get( 'address_id' ), \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( 'phone', true ) ) )
    {
      \Shared\Helpers\Helpers::get( 'address_id' ) ? \Shared\Helpers\Helpers::alert( \Shared\Helpers\Helpers::lang( 'zmiana-adresu-sukces' ) ) : \Shared\Helpers\Helpers::alert( \Shared\Helpers\Helpers::lang( 'dodawanie-nowego-adresu-sukces' ) );
    }
    else
    {
      \Shared\Helpers\Helpers::get( 'address_id' ) ? \Shared\Helpers\Helpers::error( \Shared\Helpers\Helpers::lang( 'zmiana-adresu-blad' ) ) : \Shared\Helpers\Helpers::error( \Shared\Helpers\Helpers::lang( 'dodawanie-nowego-adresu-blad' ) );
    }

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

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

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

    if ( !$client = \Shared\Helpers\Helpers::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'] = \Shared\Helpers\Helpers::lang( 'meta-title-historia-zamowien' ) . ' | ' . $settings['firm_name'];

    if ( !$client = \Shared\Helpers\Helpers::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( \Shared\Helpers\Helpers::get( 'hash' ) ) )
      \Shared\Helpers\Helpers::alert( \Shared\Helpers\Helpers::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( \Shared\Helpers\Helpers::get( 'email' ) ) )
      \Shared\Helpers\Helpers::alert( \Shared\Helpers\Helpers::lang( 'odzyskiwanie-hasla-link-komunikat' ) );
    else
      \Shared\Helpers\Helpers::alert( \Shared\Helpers\Helpers::lang( 'odzyskiwanie-hasla-blad' ) );
    header( 'Location: /logowanie' );
    exit;
  }

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

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

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

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

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

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

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

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

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

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

    if ( $client = \Shared\Helpers\Helpers::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'] = \Shared\Helpers\Helpers::lang( 'meta-title-rejestracja' ) . ' | ' . $settings['firm_name'];

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

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