<?php

$loader = $this->registry->get('load');
$loader->controller('payment/payment_info');

class ControllerPaymentYapayLib extends ControllerPaymentPaymentInfo
{

  public function load()
  {
    $this->load->dmodel('checkout/order');

    $language = $this->language->load('payment/yapay');
    $this->data = array_merge($this->data, $language);

    $this->data['back'] = $this->url->link('checkout/checkout', '', 'SSL');
    $this->data['continue'] = $this->url->link('checkout/success');
    $this->data['environment'] = ($this->config->get('yapay_ambiente') == Type_Environment::Tp_Sandbox) ? json_encode(['env' => 'sandbox']) : '';
  }

  public function getOrder()
  {

    $this->load->dmodel('checkout/order');

    $this->order_info = $this->dmodel_checkout_order->getOrder($this->session->data['order_id']);

    $this->dmodel_checkout_order->confirm($this->order_info['order_id'], $this->config->get('yapay_order_status_waiting'), '');

    return $this->order_info;
  }

  public function getTransactionInfo()
  {

    parent::setPaymentInfo();
    parent::setPaymentProducts();

    $customerData = array(
      'email' => $this->paymentInfo->getEmail(),
      'name' => $this->paymentInfo->getNome(),
    );

    if($this->paymentInfo->hasParam('cpf')){
      $customerData['cpf'] = preg_replace("/[^0-9]/", "", $this->paymentInfo->getParam('cpf'));
    }

    switch ($this->paymentInfo->getDocumento()->tipo){
      case Type_Document::Tp_Fisico:
        $customerData['cpf'] = $this->paymentInfo->getDocumento()->numero;
        break;
      case Type_Document::Tp_Juridico:
        $customerData['trade_name'] = $this->paymentInfo->getNome();
        $customerData['company_name'] = $this->paymentInfo->getRazaoSocial();
        $customerData['cnpj'] = $this->paymentInfo->getDocumento()->numero;
        break;
    }


    $customerData['contacts'][] = array(
      'type_contact' => $this->getTypeContact($this->paymentInfo->getTelefone()->tipo),
      'number_contact' => preg_replace("/[^0-9]/", "", $this->paymentInfo->getTelefone()->completo)
    );

    $customerData['addresses'][] = array(
      'type_address' => Type_FormPgtoYapayAddress::Tp_Entrega,
      'postal_code' => $this->paymentInfo->getShippingAddress()->getPostcode(),
      'street' => $this->paymentInfo->getShippingAddress()->getRua(),
      'number' => $this->paymentInfo->getShippingAddress()->getNumero(),
      'completion' => $this->paymentInfo->getShippingAddress()->getComplemento(),
      'neighborhood' => $this->paymentInfo->getShippingAddress()->getBairro(),
      'city' => $this->paymentInfo->getShippingAddress()->getCidade(),
      'state' => $this->paymentInfo->getShippingAddress()->getUf()
    );

    foreach ($this->paymentInfo->getProducts() as $product) {
      $productData[] = array(
        'description' => $product->getDescricao(),
        'quantity' => $product->getQuantity(),
        'price_unit' => number_format($product->getPrice(), 2, '.', '')
      );
    }

    $transaction = array(
      'available_payment_methods' => $this->paymentInfo->getParam('available_payment_methods'),
      'customer_ip' => $this->order_info['ip'],
      'shipping_type' => $this->order_info['shipping_method'],
      'shipping_price' => number_format($this->paymentInfo->getFrete(), 2, '.', ''),
      'url_notification' => $this->url->link('payment/yapay/callback')
    );

    if($this->paymentInfo->hasParam('price_additional')){
      $transaction['price_additional'] = number_format($this->paymentInfo->getParam('price_additional'), 2, '.', '');
    }

    if($this->paymentInfo->hasParam('price_discount')){
      $transaction['price_discount'] = number_format($this->paymentInfo->getParam('price_discount'), 2, '.', '');
    }

    $payment = array(
      'payment_method_id' => $this->paymentInfo->getParam('payment_method_id')
    );

    return array(
      'token_account' => $this->config->get('yapay_token'),
      'payment'=> $payment,
      'customer' => $customerData,
      'transaction_product' => $productData,
      'transaction' => $transaction
    );

  }

  public function getTypeContact($value){

    switch ($value){
      case Type_PaymentPhone::Tp_Celular:
        return Type_FormPgtoYapayContato::Tp_Celular;
        break;
      case Type_PaymentPhone::Tp_Telefone:
        return Type_FormPgtoYapayContato::Tp_Residencial;
        break;
      case Type_PaymentPhone::Tp_Fax:
        return Type_FormPgtoYapayContato::Tp_Comercial;
        break;
    }

  }

}
