<?php
class ControllerAccountPassword extends Controller {
	private $error = array();

	public function index() {

		$this->document->addScriptApp(array(
			'catalog/view/theme/vista/js/jquery/jquery.validate.js',
      'catalog/view/theme/vista/js/jquery/additional-methods.js',
      'catalog/view/theme/vista/js/jquery/jquery.validate.futura-methods.js',
			'catalog/view/theme/vista/js/jquery/localization/messages_pt_BR.js',
			'catalog/view/theme/appmobile/public/js/medidor-de-senha.js',
      'catalog/view/theme/appmobile/js/page.password.js'
    ));

		// Adiciona o Plugin que mostra o qualidade da senha - por Henrique R. Pohl
		$this->document->addScript('catalog/view/theme/vista/public/js/medidor-de-senha.js');

		if (!$this->customer->isLogged()) {
			$this->session->data['redirect'] = $this->url->link('account/password', '', 'SSL');

			$this->redirect($this->url->link('account/register', '', 'SSL'));
		}

		$this->language->load('account/password');

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

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->load->model('account/customer');

			$this->model_account_customer->editPassword($this->customer->getEmail(), $this->request->post['password']);

			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect($this->url->link('account/account', '', 'SSL'));
		}

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

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

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_account'),
			'href'      => $this->url->link('account/account', '', 'SSL'),
			'separator' => $this->language->get('text_separator')
		);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('account/password', '', 'SSL'),
			'separator' => $this->language->get('text_separator')
		);

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

		$this->data['heading_title']   = $this->language->get('heading_title');
		$this->data['text_password']   = $this->language->get('text_password');
		$this->data['entry_password']  = $this->language->get('entry_password');
		$this->data['entry_confirm']   = $this->language->get('entry_confirm');
		$this->data['entry_current_password']   = $this->language->get('entry_current_password');

		$this->data['entry_password_placeholder'] 			 	 = $this->language->get('entry_password_placeholder');
		$this->data['entry_confirm_placeholder']  				 = $this->language->get('entry_confirm_placeholder');
		$this->data['entry_current_password_placeholder']  = $this->language->get('entry_current_password_placeholder');

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

		if (isset($this->error['current_password'])) {
			$this->data['error_current_password'] = $this->error['current_password'];
		} else {
			$this->data['error_current_password'] = '';
		}


		if (isset($this->error['password'])) {
			$this->data['error_password'] = $this->error['password'];
		} else {
			$this->data['error_password'] = '';
		}

		if (isset($this->error['confirm'])) {
			$this->data['error_confirm'] = $this->error['confirm'];
		} else {
			$this->data['error_confirm'] = '';
		}

		$this->data['action'] = $this->url->link('account/password', '', 'SSL');

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

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

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

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

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/password.twig')) {
			$this->template = $this->config->get('config_template') . '/template/account/password.twig';
		} else {
			$this->template = 'default/template/account/password.twig';
		}

		$this->children = array(
			'common/breadcrumbs',
			'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
      'common/header'
    );

    if (!$this->ftrgeralregistry->isApp()){
      array_push($this->children, 'module/account');
    }

		$this->response->setOutput($this->render('account/password'));
	}

	protected function validate() {

		$this->load->model('account/customer');

		if ((utf8_strlen($this->request->post['password']) < 4) || (utf8_strlen($this->request->post['password']) > 20)) {
			$this->error['password'] = $this->language->get('error_password');
		}

		if ((utf8_strlen($this->request->post['current_password']) < 4) || (utf8_strlen($this->request->post['current_password']) > 20)) {
			$this->error['current_password'] = $this->language->get('error_password');
		}

		if(isset($this->request->post['current_password']) && (!$this->model_account_customer->getValidaPassword($this->request->post['current_password']))){
			$this->error['current_password'] = $this->language->get('error_current_password');
		}

		if($this->request->post['current_password'] == $this->request->post['password']){
      $this->error['password'] = $this->language->get('error_current_password_equals');
    }

		if ($this->request->post['confirm'] != $this->request->post['password']) {
			$this->error['confirm'] = $this->language->get('error_confirm');
		}

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

}
