<?php

class ModelTotalGetnet extends Model
{
  public function getTotal(&$total_data, &$total)
  {

    if (isset($this->session->data['payment_method']) && ($this->session->data['payment_method']['code'] == Type_GetnetConfigs::Tp_NameModule)) {

      $this->language->load('total/getnet');
      $this->load->helper('ftr_geral');
      $desconto = false;
      $totalOrder = $this->cart->getTotal();

      switch ($this->session->data['payment_method']['code_type']) {
        case Type_PaymentOption::Tp_Billet:
          if ($this->config->get('getnet_billet')) {
            $getnet_billet = $this->config->get('getnet_billet');
            $title = $this->language->get('text_discount_billet');
            $minValueToGetBilletDiscount = isset($getnet_billet['valor_minimo_discount']) ? $getnet_billet['valor_minimo_discount'] : 0;

            if ($totalOrder >= floatval($minValueToGetBilletDiscount)) {
              $desconto = ftr_geral::getPaymentDiscount(array($getnet_billet), $total, $this->cart->getTotal());
            }

          }
          break;
        case Type_PaymentOption::Tp_Pix:
          if ($this->config->get('getnet_pix')) {
            $getnet_pix = $this->config->get('getnet_pix');
            $title = $this->language->get('text_discount_pix');
            $minValueToGetPixDiscount = isset($getnet_pix['valor_minimo_discount']) ? $getnet_pix['valor_minimo_discount'] : 0;

            if ($totalOrder >= floatval($minValueToGetPixDiscount)) {
              $desconto = ftr_geral::getPaymentDiscount(array($getnet_pix), $total, $this->cart->getTotal());
            }
          }
          break;
        case Type_PaymentOption::Tp_Debit:
          if ($this->config->get('getnet_debit_card')) {
            $getnet_debit_card = $this->config->get('getnet_debit_card');
            $title = $this->language->get('text_discount_debit_card');
            $minValueToGetDebitCardDiscount = isset($getnet_debit_card['valor_minimo_discount']) ? $getnet_debit_card['valor_minimo_discount'] : 0;

            if ($totalOrder >= floatval($minValueToGetDebitCardDiscount)) {
              $desconto = ftr_geral::getPaymentDiscount(array($getnet_debit_card), $total, $this->cart->getTotal());
            }
          }
          break;
        default:
          return false;
      }

      if (!$desconto) {
        return false;
      }

      $total -= ($desconto * -1);

      $total_data[] = array(
        'code' => 'getnet',
        'title' => $title,
        'text' => $this->currency->format($desconto),
        'value' => $desconto,
        'sort_order' => $this->config->get('getnet_discount_order')
      );

    } else {
      return false;
    }

  }

}
