<?php

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

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

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

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

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

    $method_data = array();

    if ($status) {

      $quote_data = array();
      if($this->config->get('yapay_credit_card')){
        $parcela_minima = $this->config->get('yapay_parcela_minima');

        $credit_card = true;
        $creditCardStatus = true;
        if ($this->config->get('yapay_config_parcelamento') == Type_ConfigParcelamento::PorParcela) {
            $creditCardStatus = $this->ftrgeralregistry->validateMinInstallment($total,$parcela_minima,$this->config->get('yapay_parcelamento_configs'));
        }elseif ($this->config->get('yapay_config_parcelamento') == Type_ConfigParcelamento::PorBandeira) {
            $credit_card = ftr_geral::getPaymentMethods($this->config->get('yapay_credit_card'), $total);
        }

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

      }

      if($this->config->get('yapay_billet')){

        $billet = ftr_geral::getPaymentMethods($this->config->get('yapay_billet'), $total);
        if($billet){
          $quote_data[Type_PaymentOption::Tp_Billet] = array(
            'code' => 'yapay',
            'code_type' => Type_PaymentOption::Tp_Billet,
            'title' => (!empty($billet[Type_FormPgtoYapayBoleto::Tp_Boleto]['title'])) ? $billet[Type_FormPgtoYapayBoleto::Tp_Boleto]['title'] : $this->language->get('text_title_billet') ,
            'sort_order' => $this->config->get('yapay_order')
          );
        }

      }

      if($this->config->get('yapay_bank_transfer')){

        $bank_transfer = ftr_geral::getPaymentMethods($this->config->get('yapay_bank_transfer'), $total);
        if($bank_transfer){
          $quote_data[Type_PaymentOption::Tp_BankTransfer] = array(
            'code' => 'yapay',
            'code_type' => Type_PaymentOption::Tp_BankTransfer,
            'title' => ($this->config->get('yapay_bank_transfer_title')) ? $this->config->get('yapay_bank_transfer_title') : $this->language->get('text_title_bank_transfer') ,
            'sort_order' => $this->config->get('yapay_order')
          );
        }

      }

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

    return $method_data;

  }


  public function setStatusOrder($order_id, $yapay_status_id){

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

    switch ($yapay_status_id){
      case Type_StatusTransactionYapay::Tp_AguardandoPagamento:
        $order_status_id = $this->config->get('yapay_order_status_waiting');
        break;
      case Type_StatusTransactionYapay::Tp_EmProcessamento:
        $order_status_id = $this->config->get('yapay_order_status_processing');
        break;
      case Type_StatusTransactionYapay::Tp_Aprovada:
        $order_status_id = $this->config->get('yapay_order_status_aproved');
        break;
      case Type_StatusTransactionYapay::Tp_Cancelada:
        $order_status_id = $this->config->get('yapay_order_status_canceled');
        break;
      case Type_StatusTransactionYapay::Tp_EmContestacao:
        $order_status_id = $this->config->get('yapay_order_status_disputed');
        break;
      case Type_StatusTransactionYapay::Tp_EmMonitoramento:
        $order_status_id = $this->config->get('yapay_order_status_monitoring');
        break;
      case Type_StatusTransactionYapay::Tp_Reprovada:
        $order_status_id = $this->config->get('yapay_order_status_reproved');
        break;
      default:
        $order_status_id = $this->config->get('yapay_order_status_waiting');
    }

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

  }

}
