<?php
$loader = $this->registry->get('load');
$loader->controller('api/controllerapi');

class ControllerApiCustomer extends ControllerApi{

  public function customerlogged(){
    parent::validaAutenticacao();

    if((parent::estaAutenticado()) && (!$this->customer->isLogged())){
      parent::setErro(Type_ApiReturn::Tp_CustomerNotLogged, Type_ApiReturn::getNickFromValue(Type_ApiReturn::Tp_CustomerNotLogged));
    }

    if(parent::haserror()){
       $retorno = parent::geterror();
    }else{
      $retorno = array(
        'email'     => $this->customer->getEmail(),
        'firstname' => $this->customer->getFirstName()
      );
    }

    $this->response->setOutputJson($retorno);
  }

  public function logincustomer(){
    parent::validaAutenticacao();
    parent::validFieldValue(Type_requisicao::Tp_reqGet, 'email');
    parent::validFieldValue(Type_requisicao::Tp_reqGet, 'senha');

    if(parent::haserror()){
      $retorno = parent::geterror();
    }else{
      $retorno = array();
      $email   = $this->request->get['email'];
      $senha   = $this->request->get['senha'];

      if($this->customer->login($email, $senha)){
        $retorno = $this->getReturnSuccessmethod(Type_ApiReturn::Tp_CustomerLoginSucesso, Type_ApiReturn::getNickFromValue(Type_ApiReturn::Tp_CustomerLoginSucesso));
      }else{
        $retorno = $this->getReturnSuccessmethod(Type_ApiReturn::Tp_CustomerLoginError, Type_ApiReturn::getNickFromValue(Type_ApiReturn::Tp_CustomerLoginError));
      }
    }

    $this->response->setOutputJson($retorno);
  }

  public function logoutCustomer(){
    parent::validaAutenticacao();

    $retorno = $this->getReturnSuccessmethod(Type_ApiReturn::Tp_CustomerNotLogged, Type_ApiReturn::getNickFromValue(Type_ApiReturn::Tp_CustomerNotLogged));

    if ($this->customer->isLogged()) {
      $this->customer->logout();
      $this->cart->clear();

      unset($this->session->data['wishlist']);
      unset($this->session->data['shipping_address_id']);
      unset($this->session->data['shipping_country_id']);
      unset($this->session->data['shipping_zone_id']);
      unset($this->session->data['shipping_postcode']);
      unset($this->session->data['shipping_method']);
      unset($this->session->data['shipping_methods']);
      unset($this->session->data['payment_address_id']);
      unset($this->session->data['payment_country_id']);
      unset($this->session->data['payment_zone_id']);
      unset($this->session->data['payment_method']);
      unset($this->session->data['payment_methods']);
      unset($this->session->data['comment']);
      unset($this->session->data['order_id']);
      unset($this->session->data['coupon']);
      unset($this->session->data['reward']);
      unset($this->session->data['voucher']);
      unset($this->session->data['vouchers']);

      $retorno = $this->getReturnSuccessmethod(Type_ApiReturn::Tp_CustomerLogoutSucesso, Type_ApiReturn::getNickFromValue(Type_ApiReturn::Tp_CustomerLogoutSucesso));

    }

    $this->response->setOutputJson($retorno);

  }

}
