<?php

class ModelPaymentGetnet extends Model
{
  public function getMethod($address, $total)
  {

    if(!$this->config->get('getnet_status')){
      return false;
    }

    if (ftr_geral::isIsset($this->config->get('getnet_display')) && (!$this->ftrgeralregistry->exibeMuduloLayout($this->config->get('getnet_display')))){
      return false;
    }

    $this->load->language('payment/getnet');
    $this->load->helper('ftr_geral');

    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$this->config->get('getnet_region') . "' AND country_id = '" . (int)$address['country_id'] . "' AND (zone_id = '" . (int)$address['zone_id'] . "' OR zone_id = '0')");

    if (!$this->config->get('getnet_region')) {
      $status = true;
    } elseif ($query->num_rows) {
      $status = true;
    } else {
      $status = false;
    }

    $method_data = array();

    if ($status) {

      $quote_data = array();
      $status_credit_card = array_filter(array_column($this->config->get('getnet_credit_card'), 'status'));
      if ($status_credit_card){
        $parcela_minima = $this->config->get('getnet_parcela_minima');

        $credit_card_payment = true;
        $creditCardStatus = true;
        if ($this->config->get('config_parcelamento') == Type_ConfigParcelamento::PorParcela) {
            $creditCardStatus = $this->ftrgeralregistry->validateMinInstallment($total, $parcela_minima, $this->config->get('parcelamento_configs'));
        }elseif ($this->config->get('config_parcelamento') == Type_ConfigParcelamento::PorBandeira) {
            $credit_card_payment = ftr_geral::getPaymentMethods($this->config->get('getnet_credit_card'), $total);
        }

        if($credit_card_payment && ($total >= $parcela_minima) && $creditCardStatus){
          $quote_data[Type_PaymentOption::Tp_CreditCard] = array(
            'code' => 'getnet',
            'code_type' => Type_PaymentOption::Tp_CreditCard,
            'title' => ($this->config->get('getnet_credit_card_title')) ? $this->config->get('getnet_credit_card_title') : $this->language->get('text_title_credit_card') ,
            'sort_order' => $this->config->get('getnet_order')
          );
        }

      }

      if($this->config->get('getnet_billet')){
        $billet_config = $this->config->get('getnet_billet');

        $billet = ftr_geral::getPaymentMethods(array($billet_config), $total);
        if($billet){
          $quote_data[Type_PaymentOption::Tp_Billet] = array(
            'code' => 'getnet',
            'code_type' => Type_PaymentOption::Tp_Billet,
            'title' => (!empty(current($billet)['title'])) ? current($billet)['title'] : $this->language->get('text_title_billet') ,
            'sort_order' => $this->config->get('getnet_order')
          );
        }

      }

//      if($this->config->get('getnet_pix')){
//        $pix_config = $this->config->get('getnet_pix');
//
//        $pix = ftr_geral::getPaymentMethods(array($pix_config), $total);
//        if($pix){
//          $quote_data[Type_PaymentOption::Tp_Pix] = array(
//            'code' => 'getnet',
//            'code_type' => Type_PaymentOption::Tp_Pix,
//            'title' => (!empty(current($pix)['title'])) ? current($pix)['title'] : $this->language->get('text_title_pix') ,
//            'sort_order' => $this->config->get('getnet_order')
//          );
//        }
//      }

      if (empty($quote_data)){
        return false;
      }

      $method_data = array(
        'code' => 'getnet',
        'quote' => $quote_data,
        'title' => $this->language->get('text_title'),
        'terms' => '',
        'sort_order' => $this->config->get('getnet_order')
      );
    }

    return $method_data;

  }


  public function setStatusOrder($order_id, $getnet_status_id){

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

    switch ($getnet_status_id){
      case Type_GetnetPagamentoStatus::Tp_Aprovado:
        $order_status_id = $this->config->get('getnet_order_status_approved');
        break;
      case Type_GetnetPagamentoStatus::Tp_Reprovado:
        $order_status_id = $this->config->get('getnet_order_status_reproved');
        break;
      case Type_GetnetPagamentoStatus::Tp_Pendente:
        $order_status_id = $this->config->get('getnet_order_status_pending');
        break;
      case Type_GetnetPagamentoStatus::Tp_Pago:
        $order_status_id = $this->config->get('getnet_order_status_paid');
        break;
      case Type_GetnetPagamentoStatus::Tp_Cancelado:
        $order_status_id = $this->config->get('getnet_order_status_canceled');
        break;
      case Type_GetnetPagamentoStatus::Tp_Erro:
        $order_status_id = $this->config->get('getnet_order_status_error');
        break;
     case Type_GetnetPagamentoStatus::Tp_Negado:
        $order_status_id = $this->config->get('getnet_order_status_denied');
        break;
      default:
        $order_status_id = $this->config->get('getnet_order_status_pending');
    }

    $this->dmodel_checkout_order->update($order_id, $order_status_id, '', true);

  }

}
