<?php

class ControllerPaymentPayPal extends Controller
{
    private $error = array();

    public function index()
    {

        $language = $this->load->language('payment/paypal');
        $this->data = array_merge($this->data, $language);

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('setting/setting');
        $this->load->model('setting/extension');

        $this->document->addTypesForJs(array(
            Type_ImageUpload::getTypeForJs(),
        ));

        $this->document->addScript(array(
            'public/js/payment/paypal.js'
        ));

        $this->data['config_adm_ssl'] = $this->config->get('config_adm_ssl');

        $this->data['Type_DisplayModuleOption'] = $this->load->typeForTwig('Type_DisplayModuleOption');

        $this->data['ftr_geral'] = new ftr_geral();

        $this->error = array();

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
            unset($this->request->post['paypal_module']);

            $this->removeMasks();
            $this->model_setting_setting->editSetting('paypal', $this->request->post);
            $this->session->data['success'] = $this->language->get('text_success');
            $this->redirect($this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL'));
        } else {
            $this->data['error'] = @$this->error;
        }


        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_payment'),
            'href' => $this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('payment/paypal', 'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 'SSL'),
            'separator' => ' :: '
        );

        //button actions
        $this->data['action'] = $this->url->link('payment/paypal', 'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 'SSL');
        $this->data['cancel'] = $this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL');
        $this->data['url_validate_credentials'] = $this->url->link('payment/paypal/validateCredentials', 'token=' . $this->session->data['token'], 'SSL');

        if (isset($this->request->post['paypal_client_id'])) {
            $this->data['paypal_client_id'] = $this->request->post['paypal_client_id'];
        } else {
            $this->data['paypal_client_id'] = $this->config->get('paypal_client_id');
        }

        if (isset($this->request->post['paypal_client_secret'])) {
            $this->data['paypal_client_secret'] = $this->request->post['paypal_client_secret'];
        } else {
            $this->data['paypal_client_secret'] = $this->config->get('paypal_client_secret');
        }


        if (isset($this->request->post['paypal_test'])) {
            $this->data['paypal_test'] = $this->request->post['paypal_test'];
        } else {
            $this->data['paypal_test'] = $this->config->get('paypal_test');
        }


        //Contig - Botões de checkout
        if (isset($this->request->post['paypal_checkout_buttons'])) {
            $this->data['paypal_checkout_buttons'] = $this->request->post['paypal_checkout_buttons'];
        } else {
            $this->data['paypal_checkout_buttons'] = $this->config->get('paypal_checkout_buttons');
        }
        $this->data['paypal_checkout_buttons_status_list'] = ftr_geral::getArrayTypes(Type_Status::getEnumList(), $this->data['paypal_checkout_buttons'], Type_Status::Tp_Desabilitado);

        if (isset($this->request->post['paypal_total_checkout'])) {
            $this->data['paypal_total_checkout'] = $this->request->post['paypal_total_checkout'];
        } elseif ($this->config->get('paypal_total_checkout')) {
            $this->data['paypal_total_checkout'] = $this->config->get('paypal_total_checkout');
        } else {
            $this->data['paypal_total_checkout'] = 0;
        }

        if (isset($this->request->post['paypal_title_checkout'])) {
            $this->data['paypal_title_checkout'] = $this->request->post['paypal_title_checkout'];
        } elseif ($this->config->get('paypal_title_checkout')) {
            $this->data['paypal_title_checkout'] = $this->config->get('paypal_title_checkout');
        } else {
            $this->data['paypal_title_checkout'] = $this->language->get('text_title');
        }

        if (isset($this->request->post['paypal_descricao_compra'])) {
            $this->data['paypal_descricao_compra'] = $this->request->post['paypal_descricao_compra'];
        } elseif ($this->config->get('paypal_descricao_compra')) {
            $this->data['paypal_descricao_compra'] = $this->config->get('paypal_descricao_compra');
        } else {
            $this->data['paypal_descricao_compra'] = substr($this->config->get('config_name'), 0, 127);
        }

        if (isset($this->request->post['paypal_descricao_fatura'])) {
            $this->data['paypal_descricao_fatura'] = $this->request->post['paypal_descricao_fatura'];
        } elseif ($this->config->get('paypal_descricao_fatura')) {
            $this->data['paypal_descricao_fatura'] = $this->config->get('paypal_descricao_fatura');
        } else {
            $this->data['paypal_descricao_fatura'] = substr($this->config->get('config_name'), 0, 15);
        }

        if (isset($this->request->post['paypal_descricao_empresa'])) {
            $this->data['paypal_descricao_empresa'] = $this->request->post['paypal_descricao_empresa'];
        } elseif ($this->config->get('paypal_descricao_empresa')) {
            $this->data['paypal_descricao_empresa'] = $this->config->get('paypal_descricao_empresa');
        } else {
            $this->data['paypal_descricao_empresa'] = substr($this->config->get('config_name'), 0, 127);
        }
        //Contig - Botões de checkout

        if (isset($this->request->post['paypal_debug'])) {
            $this->data['paypal_debug'] = $this->request->post['paypal_debug'];
        } else {
            $this->data['paypal_debug'] = $this->config->get('paypal_debug');
        }


        $this->load->model('localisation/order_status');

        $this->data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();


        if (isset($this->request->post['paypal_order_completed_status_id'])) {
            $this->data['paypal_order_completed_status_id'] = $this->request->post['paypal_order_completed_status_id'];
        } else {
            $this->data['paypal_order_completed_status_id'] = $this->config->get('paypal_order_completed_status_id');
        }

        if (isset($this->request->post['paypal_order_saved_status_id'])) {
            $this->data['paypal_order_saved_status_id'] = $this->request->post['paypal_order_saved_status_id'];
        } else {
            $this->data['paypal_order_saved_status_id'] = $this->config->get('paypal_order_saved_status_id');
        }

        if (isset($this->request->post['paypal_order_approved_status_id'])) {
            $this->data['paypal_order_approved_status_id'] = $this->request->post['paypal_order_approved_status_id'];
        } else {
            $this->data['paypal_order_approved_status_id'] = $this->config->get('paypal_order_approved_status_id');
        }

        if (isset($this->request->post['paypal_order_voided_status_id'])) {
            $this->data['paypal_order_voided_status_id'] = $this->request->post['paypal_order_voided_status_id'];
        } else {
            $this->data['paypal_order_voided_status_id'] = $this->config->get('paypal_order_voided_status_id');
        }

        if (isset($this->request->post['paypal_order_created_status_id'])) {
            $this->data['paypal_order_created_status_id'] = $this->request->post['paypal_order_created_status_id'];
        } else {
            $this->data['paypal_order_created_status_id'] = $this->config->get('paypal_order_created_status_id');
        }

        if (isset($this->request->post['paypal_display'])) {
            $this->data['paypal_display'] = $this->request->post['paypal_display'];
        } else {
            $this->data['paypal_display'] = $this->config->get('paypal_display');
        }


        if (isset($this->request->post['paypal_geo_zone_id'])) {
            $this->data['paypal_geo_zone_id'] = $this->request->post['paypal_geo_zone_id'];
        } else {
            $this->data['paypal_geo_zone_id'] = $this->config->get('paypal_geo_zone_id');
        }

        $this->load->model('localisation/geo_zone');
        $this->data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();

        if (isset($this->request->post['paypal_status'])) {
            $this->data['paypal_status'] = $this->request->post['paypal_status'];
        } else {
            $this->data['paypal_status'] = $this->config->get('paypal_status');
        }

        $this->data['paypal_status_list'] = ftr_geral::getArrayTypes(Type_Status::getEnumList(), $this->data['paypal_status'], Type_Status::Tp_Desabilitado);

        if (isset($this->request->post['paypal_sort_order'])) {
            $this->data['paypal_sort_order'] = $this->request->post['paypal_sort_order'];
        } else {
            $this->data['paypal_sort_order'] = $this->config->get('paypal_sort_order');
        }


        $this->children = array(
            'common/header',
            'common/footer'
        );
        $this->response->setOutput($this->render('payment/paypal'));
    }

    protected function removeMasks(){
        if(isset($this->request->post['paypal_total_checkout']) && $this->request->post['paypal_total_checkout']){
            $this->request->post['paypal_total_checkout'] = ftr_geral::removeMaskToDouble($this->request->post['paypal_total_checkout']);
        }
    }

    protected function validate()
    {
        if (!$this->user->hasPermission('modify', 'payment/paypal')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }


        if (!$this->error) {
            return true;
        } else {
            return false;
        }

    }

    public function validateCredentials()
    {
        $this->load->language('payment/paypal');

        $paypal_client_id = $this->request->get['paypal_client_id'];
        $paypal_client_secret = $this->request->get['paypal_client_secret'];

        $requestPaypal = new paypal($this->registry);

        if (!empty($paypal_client_id) && !empty($paypal_client_secret)) {
            $result = $requestPaypal->validateAccesToken($paypal_client_id, $paypal_client_secret);

            $this->response->setOutputJson([
                'type' => $result['type'] == 'success' ? 'success' : 'error',
                'message' => $result['type'] == 'success' ? $this->language->get('text_success_validate') : $this->language->get('text_error_validate')
            ]);
        } else {
            $this->response->setOutputJson([
                'type' => 'error',
                'message' => $this->language->get('text_error_validate')
            ]);
        }
    }


}
