<?php

$loader = $this->registry->get('load');
$loader->controller('payment/payment_default');

class ControllerPaymentPagamentoDinamico extends ControllerPaymentPaymentDefault
{

  protected function index()
  {
    parent::index();

    $this->load->language('payment/pagamentodinamico');
    $this->app();

    $this->data['back'] = $this->url->link('checkout/checkout', '', 'SSL');

    $pagamentodinamico_modules = $this->config->get('pagamentodinamico_modules');
    $index = array_search($this->session->data['payment_method']['title'], array_column($pagamentodinamico_modules, 'pagamentodinamico_opcao'));
    if($pagamentodinamico_modules[$index]){
      $this->data['text_message'] = $pagamentodinamico_modules[$index]['pagamentodinamico_message'];
    }else{
      $this->data['text_message'] = false;
    }

    if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/pagamentodinamico.tpl')) {
      $this->template = $this->config->get('config_template') . '/template/payment/pagamentodinamico.tpl';
    } else {
      $this->template = 'default/template/payment/pagamentodinamico.tpl';
    }

    $this->render('payment/pagamentodinamico');
  }


  public function confirm()
  {
    $json = array();
    $json['error'] = $this->validate();
    
    if (!$json['error']) {	     
      $this->load->dmodel('checkout/order');
      $this->dmodel_checkout_order->confirm($this->session->data['order_id'], $this->config->get('pagamentodinamico_status_iniciado'), '');

      if (isset($this->session->data['order_id'])) {
        $this->cart->clear();

        unset($this->session->data['shipping_method']);
        unset($this->session->data['shipping_methods']);
        unset($this->session->data['payment_methods']);
        unset($this->session->data['guest']);
        unset($this->session->data['comment']);
        unset($this->session->data['coupon']);
        unset($this->session->data['reward']);
        unset($this->session->data['voucher']);
        unset($this->session->data['vouchers']);
      }
    }
    $this->response->setOutput(json_encode($json));      
  }

  public function success()
  {

    if (isset($this->session->data['payment_session']['code']) && ($this->session->data['payment_session']['code'] == Type_PaymentModule::Tp_PtgDinaminco)) {

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

        $pagamentodinamico_modules = $this->config->get('pagamentodinamico_modules');

        $index = array_search($this->session->data['payment_session']['title'], array_column($pagamentodinamico_modules, 'pagamentodinamico_opcao'));

        if($pagamentodinamico_modules[$index]){
          $pagamentodinamico_information = $pagamentodinamico_modules[$index]['pagamentodinamico_information'];
          $pagamentodinamico_image_success = $pagamentodinamico_modules[$index]['pagamentodinamico_image_success'];
        }

      }else{
        //retro compatibilidade
        $pagamentodinamico_information = $this->config->get('pagamentodinamico_information');
        $pagamentodinamico_image_success = $this->config->get('pagamentodinamico_image_success');
      }


      if($pagamentodinamico_image_success){
        $this->load->dmodel('tool/image');
        $pagamentodinamico_image_success = $this->dmodel_tool_image->proportionatelyResize($pagamentodinamico_image_success, 500);
      }

      $this->data['pagamentodinamico_information'] = $pagamentodinamico_information;
      $this->data['pagamentodinamico_image_success'] = $pagamentodinamico_image_success;

      if(!$this->data['pagamentodinamico_information'] && !$this->data['pagamentodinamico_image_success']){
        return false;
      }

      $this->template = $this->config->get('config_template') . '/template/success/' . $this->session->data['payment_session']['code'] . '.twig';
      $this->response->setOutput($this->render());

    } else {
      return false;
    }

  }

  public function validate(){
    return parent::validate();
  }

  public function app()
  {
    parent::app();
  }
}
