<?php

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

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

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

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

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

    $method_data = array();

    if ($status) {

      $quote_data = array();
      if($this->config->get('wirecard_credit_card')){

        $credit_card = ftr_geral::getPaymentMethods($this->config->get('wirecard_credit_card'), $total);
        $parcela_minima = $this->config->get('wirecard_parcela_minima');

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

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

      }

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

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

      }

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

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

      }

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

    return $method_data;

  }


  public function setStatusOrder($order_id, $wirecard_status_id){

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

    switch ($wirecard_status_id){
      case Type_StatusTransactionwirecard::Tp_Created:
        $order_status_id = $this->config->get('wirecard_order_status_created');
        break;
      case Type_StatusTransactionwirecard::Tp_InAnalysis:
        $order_status_id = $this->config->get('wirecard_order_status_inanalysis');
        break;
      case Type_StatusTransactionwirecard::Tp_PreAuthorized:
        $order_status_id = $this->config->get('wirecard_order_status_preauthorized');
        break;
      case Type_StatusTransactionwirecard::Tp_Authorized:
        $order_status_id = $this->config->get('wirecard_order_status_authorized');
        break;
      case Type_StatusTransactionwirecard::Tp_Cancelled:
        $order_status_id = $this->config->get('wirecard_order_status_cancelled');
        break;
      case Type_StatusTransactionwirecard::Tp_Refunded:
        $order_status_id = $this->config->get('wirecard_order_status_refunded');
        break;
      case Type_StatusTransactionwirecard::Tp_Reversed:
        $order_status_id = $this->config->get('wirecard_order_status_reversed');
        break;
      case Type_StatusTransactionwirecard::Tp_Settled:
        if(!$this->config->get('wirecard_order_status_settled')){
          return false;
        }
        $order_status_id = $this->config->get('wirecard_order_status_settled');
        break;
      default:
        $order_status_id = $this->config->get('wirecard_order_status_waiting');
    }

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

  }

}
