<?php
$loader = $this->registry->get('load');
$loader->controller('base/base');

class ControllerAccountPassword extends ControllerBaseBase {
	
	private $error = array();
	protected $referencePathFile = 'account/password';

	protected function loadScripts(){

		$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',
			'catalog/view/theme/vista/public/js/account/password.js',
			'public/js/menuAccount.js',
		]);
	}

	protected function breadcrumbs(){

		$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']);
	}
	
	protected function loadLanguage(){

		$language = $this->language->load('account/password');
		$this->data = array_merge($this->data, $language);
	}

	protected function setError(){

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

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

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

	protected function setInfo(){
		if (isset($this->request->post['password'])) {
			$this->dataVue['password'] = $this->request->post['password'];
		} else {
			$this->dataVue['password'] = '';
		}

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

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

	protected function loadTemplate(){

		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'
    );
	}

	public function save(){

		$json = [];
		if(($this->request->server['REQUEST_METHOD'] == 'POST')) {
			$password = $this->request->post['password'];
			$current_password = $this->request->post['current_password'];
			$confirm = $this->request->post['confirm'];
			$sucess = '';
			$link = '';
			$error = $this->validateInfo($password, $current_password, $confirm);

			if($error == null || $error == '' ){
				$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');
				$sucess = $this->session->data['success'];
				$link = $this->url->link('account/account', '', 'SSL');
			}

			$json = ['sucess' => $sucess, 'link' => $link, 'error' => $error];
			return $this->response->setOutput(json_encode($json));;
		}
	}
	public function index() {

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

		$this->loadLanguage();
		$this->loadScripts();
		$this->breadcrumbs();

		$this->load->model('account/customer');
		$this->load->helper('ftr_geral');
		$this->load->type('tipocomum');

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

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

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

	protected function validateInfo($password, $current_password, $confirm) {

		$this->load->model('account/customer');
		$this->loadLanguage();
		extract($this->request->post);

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

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

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

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

		if ($this->error) {
			return $this->error;
		} 
	}
}
