<?php

class ControllerPaymentErede extends Controller
{

	/**
	 * Array Error
	 * @var array
	 */
	private $error = array();

	/**
	 *Regex validate e-mail
	 * @var regex
	 */
	private $pattern = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';

	public function index()
	{
		$this->_addRedeLibrary();
		$this->language->load('payment/erede');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('setting/setting');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_setting_setting->editSetting('erede', $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL'));
		}

		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->_createInput();
		$this->_createText();
		$this->_createRadio();
		$this->_createButtons();
		$this->_createBreadcrumbs();
		$this->_createLink();
		$this->_createError();

		$this->template = 'payment/erede.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render());
	}

	private function _addRedeLibrary()
	{
		include_once DIR_CATALOG . 'controller/payment/RedeLibrary/RedeLibrary.php';
	}

	private function _createInput()
	{

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

	private function _createText()
	{

		$this->data['enable_module'] = $this->language->get('enable_module');
		$this->data['text_module'] = $this->language->get('text_module_erede');

		$this->data['display_order'] = $this->language->get('display_order');
		$this->data['text_order'] = $this->language->get('text_order');

		$this->data['title_numero_estabelecimento'] = $this->language->get('title_numero_estabelecimento');
		$this->data['text_numero_estabelecimento'] = $this->language->get('text_numero_estabelecimento');

		$this->data['title_cidade_estabelecimento'] = $this->language->get('title_cidade_estabelecimento');
		$this->data['text_cidade_estabelecimento'] = $this->language->get('text_cidade_estabelecimento');

		$this->data['title_url_webservice'] = $this->language->get('title_url_webservice');
		$this->data['text_url_webservice'] = $this->language->get('text_url_webservice');

		$this->data['title_url_post'] = $this->language->get('title_url_post');
		$this->data['text_url_post'] = $this->language->get('text_url_post');

		$this->data['title_url_retorno'] = $this->language->get('title_url_retorno');
		$this->data['text_url_retorno'] = $this->language->get('text_url_retorno');

		$this->data['title_status_pedido'] = $this->language->get('title_status_pedido');
		$this->data['title_status_pedido_pendente'] = $this->language->get('title_status_pedido_pendente');

		$this->data['title_qtd_dias_vencimento'] = $this->language->get('title_qtd_dias_vencimento');

		$this->data['title_qtd_parcelas'] = $this->language->get('title_qtd_parcelas');

		$this->data['title_parcela_minima'] = $this->language->get('title_parcela_minima');
		$this->data['text_parcela_minima'] = $this->language->get('text_parcela_minima');

		$this->data['title_servicos'] = $this->language->get('title_servicos');

		$this->data['title_juros_por_dia'] = $this->language->get('title_juros_por_dia');

		$this->data['title_multa_atraso'] = $this->language->get('title_multa_atraso');

		$this->data['title_servicos'] = $this->language->get('title_servicos');
		$this->data['title_servicosCredito'] = $this->language->get('title_servicosCredito');
		$this->data['title_servicosDebito'] = $this->language->get('title_servicosDebito');
		$this->data['title_servicosBoleto'] = $this->language->get('title_servicosBoleto');
		$this->data['title_servicosAnalise'] = $this->language->get('title_servicosAnalise');

		$this->data['title_instrucoes_pagamento'] = $this->language->get('title_instrucoes_pagamento');

		$this->data['title_notas_pagamento'] = $this->language->get('title_notas_pagamento');

		$this->data['text_a_vista'] = $this->language->get('text_a_vista');
		$this->data['title_banco_cedente'] = $this->language->get('title_banco_cedente');
	}

	private function _createRadio()
	{
		$this->data['text_yes'] = $this->language->get('text_yes');
		$this->data['text_no'] = $this->language->get('text_no');
	}

	private function _createButtons()
	{
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_logs'] = $this->language->get('button_logs');
		$this->data['button_risk'] = $this->language->get('button_risk');
	}

	private function _createBreadcrumbs()
	{

		$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->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL'),
			'separator'	 => ' :: '
		);

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

	/**
	 * Create Link's
	 */
	private function _createLink()
	{
		$this->data['action'] = $this->url->link('payment/erede', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['cancel'] = $this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['logs'] = $this->url->link('erede/logs', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['risk'] = $this->url->link('erede/risco', 'token=' . $this->session->data['token'], 'SSL');
	}

	/**
	 * Create Error
	 */
	private function _createError()
	{
		$this->data['error'] = '';

		if (!empty($this->error))
		{
			$this->data['error'] = 1;
			$this->data['errors'] = $this->error;
		}
	}

	/**
	 * Validate
	 * @return boolean
	 */
	protected function validate()
	{

		$this->_permission();
		$this->_validateForm();

		return (empty($this->error )) ? true : false;
	}

	/**
	 * Validate Permisson
	 */
	private function _permission()
	{
		if (!$this->user->hasPermission('modify', 'payment/pp_standard'))
			$this->error['warning'] = $this->language->get('error_permission');
	}

	private function _validateForm()
	{
		if (empty($this->request->post['numero_estabelecimento']))
			$this->error[] = $this->language->get('erro_numero_estabelecimento');
		if (empty($this->request->post['cidade_estabelecimento']))
			$this->error[] = $this->language->get('erro_cidade_estabelecimento');
		if (empty($this->request->post['url_webservice']))
			$this->error[] = $this->language->get('erro_url_webservice');
		if (empty($this->request->post['url_post']))
			$this->error[] = $this->language->get('erro_url_post');
		if (empty($this->request->post['url_retorno']))
			$this->error[] = $this->language->get('erro_url_retorno');
		if (empty($this->request->post['servicos_Credito']) && 
            empty($this->request->post['servicos_Debito']) && 
            empty($this->request->post['servicos_Boleto']) && 
            empty($this->request->post['servicos_Analise']))
         	$this->error[] = $this->language->get('erro_servicos');

        if ($this->request->post['qtd_parcelas'] > 0)
        {
            if ($this->request->post['parcela_minima'] && !is_numeric($this->request->post['parcela_minima']))
              $this->error[] = $this->language->get('erro_parcela_minima');  
        }

        if ($this->request->post['servicos_Boleto'])
        {
            if (empty($this->request->post['banco_cedente']))
              $this->error[] = $this->language->get('erro_banco_cedente');
            if (empty($this->request->post['qtd_dias_vencimento']) || !is_numeric($this->request->post['qtd_dias_vencimento']))
              $this->error[] = $this->language->get('erro_qtd_dias_vencimento');
            if ($this->request->post['multa_atraso'] == "" || !is_numeric($this->request->post['multa_atraso']))
              $this->error[] = $this->language->get('erro_multa_atraso');
            if ($this->request->post['juros_por_dia'] == "" || !is_numeric($this->request->post['juros_por_dia']))
              $this->error[] = $this->language->get('erro_juros_por_dia');
            if (empty($this->request->post['instrucoes_pagamento']))
              $this->error[] = $this->language->get('erro_instrucoes_pagamento');
            if (empty($this->request->post['notas_pagamento']))
              $this->error[] = $this->language->get('erro_notas_pagamento');
        }
	}

	/**
	 * Validate if value is not null
	 * @param type $value
	 * @return boolean
	 */
	private function _isNotNull($value)
	{

		if ($value != null && $value != "")
			return TRUE;

		return false;
	}

	public function install() {
        $this->load->model('payment/erede');
        $this->load->model('setting/setting');
        $this->model_payment_erede->install();
        //$this->model_setting_setting->editSetting('erede', array('erede_status'=>1));
    }
    
    public function uninstall() {
        $this->load->model('payment/erede');
        $this->model_payment_erede->uninstall();
        $this->model_setting_setting->editSetting('erede', array('erede_status'=>0));
    }
}
?>