<?php
class ControllerProductAviseme extends Controller {

	public function index($setting=null){

		if(!$this->ftrgeralregistry->getStatusAviseme() || (!is_null($setting) && ($setting['quantity'] > 0))) {
			return false;
		}

		$language = $this->language->load('product/avise_me');
		$this->data = array_merge($this->data, $language);
		$this->data['product_id'] 		= (!is_null($setting)) ? $setting['product_id'] : $this->request->get['product_id'];
		$this->data['product_name']   = (!is_null($setting)) ? $setting['product_name'] : $this->request->get['product_name'];
    $this->data['site_key'] 			= $this->config->get('google_recaptcha_site_key');
		$this->data['google_captcha'] = ($this->data['site_key'] && $this->config->get('google_recaptcha_secret_key') && $this->config->get('wg24_google_recaptcha_status')) ? 1 : 0;

		if ($this->customer->isLogged()) {
			$this->data['login_name'] = $this->customer->getFirstName() . ' ' . $this->customer->getLastName();
			$this->data['login_email'] = $this->customer->getEmail();
		} else {
			$this->data['login_name'] = '';
			$this->data['login_email'] = '';
		}

		if(isset($this->request->get['display'])){
			$this->data['display'] = true;
		}else{
			$this->data['display'] = false;
		}


    $this->response->setOutput($this->render('product/avise_me'));

	}

	public function send() {

		$this->language->load('product/avise_me');
		$this->load->model('catalog/avise_me');
		$this->load->helper('ftr_geral');

		$json = array();

    if ((utf8_strlen($this->request->post['name']) < 1) || (utf8_strlen($this->request->post['name']) > 32)) {
      $json['error']['name'] = $this->language->get('error_name');
    }

		if ((utf8_strlen($this->request->post['email']) > 96) || !preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['email']) || !filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL) || !preg_match('/^[a-zA-Z0-9\._-]+@/', $this->request->post['email'])) {
			$json['error']['email'] = $this->language->get('error_email');
		}

    if (!$this->config->get('google_recaptcha_site_key') || !$this->config->get('google_recaptcha_secret_key') || !$this->config->get('wg24_google_recaptcha_status')) {
      if (empty($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
        $json['error']['captcha'] = $this->language->get('error_captcha');
      }
    }

    if ($this->config->get('google_recaptcha_site_key') && $this->config->get('google_recaptcha_secret_key') && $this->config->get('wg24_google_recaptcha_status')) {
      $recaptcha = $this->ftrgeralregistry->validateGoogleCaptha($this->config->get('google_recaptcha_secret_key'), isset($this->request->post['g-recaptcha-response']) ? $this->request->post['g-recaptcha-response'] : '', $this->request->server['REMOTE_ADDR']);
      if (!$recaptcha["success"]) {
        $json['error']['google_recaptcha'] = 'Erro de validação no reCaptcha! Efetue a validação para continuar';
      }
    } else {
      if (empty($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
        $json['error']['captcha'] = 'O código de verificação não coincide com a imagem!';
      }
    }

		if(!$json){
			if (($this->request->server['REQUEST_METHOD'] == 'POST') ) {
				$product_id=preg_replace("/[^0-9]/", "",$this->request->post['product_id']);
				$this->data['link']=$this->url->link('product/product','product_id='.$product_id);
				if ($this->data['link'] != "UTF-8"):
					$this->data['link']=utf8_encode($this->data['link']);
				endif;

				$this->model_catalog_avise_me->AddNotify($this->request->post, $this->data['link']);
				$json['success']=$this->language->get('text_success');
			}
		}

		$this->response->setOutputJson($json);

	}

	public function captcha() {
		$this->load->library('captcha');

		$captcha = new Captcha();

		$this->session->data['captcha'] = $captcha->getCode();

		$captcha->showImage();
	}

}
