<?php

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

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

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

    $this->load->language('payment/pagseguro_api');
    $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('pagseguro_api_region') . "'
        AND country_id = '" . (int)$address['country_id'] . "'
        AND (zone_id = '" . (int)$address['zone_id'] . "' OR zone_id = '0')");

    if (!$this->config->get('pagseguro_api_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('pagseguro_api_credit_card'), 'status'));
      if ($status_credit_card){
        $parcela_minima = $this->config->get('pagseguro_api_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('pagseguro_api_credit_card'), $total);
        }

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

      }

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

        $billet = ftr_geral::getPaymentMethods(array($billet_config), $total);
        if($billet){
          $quote_data[Type_PaymentOption::Tp_Billet] = array(
            'code' => 'pagseguro_api',
            '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('pagseguro_api_order')
          );
        }

      }

//      if($this->config->get('pagseguro_api_pix')){
//        $pix_config = $this->config->get('pagseguro_api_pix');
//
//        $pix = ftr_geral::getPaymentMethods(array($pix_config), $total);
//        if($pix){
//          $quote_data[Type_PaymentOption::Tp_Pix] = array(
//            'code' => 'pagseguro_api',
//            '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('pagseguro_api_order')
//          );
//        }
//      }

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

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

    return $method_data;

  }

}
