<?php
class ControllerAccountWishList extends Controller
{
	public function index()
	{
		if (!$this->customer->isLogged()) {
			$this->session->data['redirect'] = $this->url->link('account/wishlist', '', 'SSL');

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

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

		$this->load->model('catalog/product');

		$this->load->type('tipocomum');

		$this->load->dmodel('tool/image');

		if (!isset($this->session->data['wishlist'])) {
			$this->session->data['wishlist'] = array();
		}

		if (isset($this->request->get['remove'])) {
			$key = array_search($this->request->get['remove'], $this->session->data['wishlist']);

			if ($key !== false) {
				unset($this->session->data['wishlist'][$key]);
			}

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

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

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

		$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/wishlist'),
			'separator' => $this->language->get('text_separator')
		);

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

		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['text_empty'] = $this->language->get('text_empty');
		$this->data['column_model'] = $this->language->get('column_model');
		$this->data['button_continue'] = $this->language->get('button_continue');
		$this->data['button_cart'] = $this->language->get('button_cart');
		$this->data['button_remove'] = $this->language->get('button_remove');
		$this->data['back'] = $this->url->link('account/account', '', 'SSL');

		//adicionar para que se caso o clique em comprar e o produto for grade redirecionar para a tela do produto
		$this->data['layout_id'] = Type_IdLayout::Tp_Idcategory;

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

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

		foreach ($this->session->data['wishlist'] as $key => $product_id) {
			$product_info = $this->model_catalog_product->getProduct($product_id);

			if ($product_info) {
				if ($product_info['image']) {
					$watermark_config_date_number = $this->dmodel_tool_image->getWatermarkCacheNumber();
					$image_version_number = '?version=' . preg_replace("/[^0-9]/", "", $product_info['data_atualizacao_replicacao']) . $watermark_config_date_number;

					$image = $this->dmodel_tool_image->resizeWithWatermark($product_info['image'], Type_MiniatureMedium::Tp_width, Type_MiniatureMedium::Tp_height) . $image_version_number;
				} else {
					$image = $this->dmodel_tool_image->resize('data/sem-imagem.png', Type_MiniatureMedium::Tp_width, Type_MiniatureMedium::Tp_height);
				}

				if ($product_info['qty'] <= 0) {
					$stock = $this->language->get('text_soldout');
					$stock_css = 'background-warning text-warning border-warning';
				} else {
					$stock = $this->language->get('text_instock');
					$stock_css = 'background-success text-success border-success';
				}

				if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
					$price = $this->currency->format($product_info['price']);
				} else {
					$price = false;
				}

				if ((float) $product_info['special']) {
					$special = $this->currency->format($product_info['special']);
				} else {
					$special = false;
				}

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

				//KIT
				$this->load->dmodel('catalog/product');
				$itens_kit = $this->dmodel_catalog_product->getProductKitItens($product_info['product_kit_id']);
				//KIT

				$this->data['products'][] = array(
					'product_id' => $product_info['product_id'],
					'thumb' => $image,
					'name' => $product_info['name'],
					'model' => $product_info['model'],
					'stock' => $stock,
					'stock_css' => $stock_css,
					'price' => $price,
					'special' => $special,
					'href' => $this->url->link('product/product', 'product_id=' . $product_info['product_id']),
					'remove' => $this->url->link('account/wishlist', 'remove=' . $product_info['product_id']),
					'notification_value' => isset($this->cart->getProducts()[$product_info['product_id'] . '::']) ? $this->cart->getProducts()[$product_info['product_id'] . '::']['quantity'] : false,
					'itens_kit' => $itens_kit
				);
			} else {
				unset($this->session->data['wishlist'][$key]);
			}
		}

		if ($this->config->get('wg24_seo_fer_facebook_status') == 'enable') {
			if (!$this->config->get('wg24_seo_fer_facebook_pixel') == '') {
				$this->data['facebook_pixel'] = html_entity_decode("<script> fbq('track', 'AddToWishlist'); </script>", ENT_QUOTES, 'UTF-8');
			}
		}

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

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/wishlist.twig')) {
			$this->template = $this->config->get('config_template') . '/template/account/wishlist.twig';
		} else {
			$this->template = 'default/template/account/wishlist.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->document->addScript([
			'catalog/view/theme/vista/public/js/account/wishlist.js',
			'public/js/menuAccount.js',
		]);


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

	public function add()
	{
		$this->language->load('account/wishlist');
		$this->load->helper('ftr_geral');

		$json = array();

		if (!isset($this->session->data['wishlist'])) {
			$this->session->data['wishlist'] = array();
		}

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

		$this->load->model('catalog/product');

		$product_info = $this->model_catalog_product->getProduct($product_id);

		if ($product_info) {
			if (!in_array($this->request->post['product_id'], $this->session->data['wishlist'])) {
				$this->session->data['wishlist'][] = $this->request->post['product_id'];
			}

			if ($this->customer->isLogged()) {
				$json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('account/wishlist'));
			} else {
				$json['success'] = sprintf($this->language->get('text_login'), $this->url->link('account/register', '', 'SSL'), $this->url->link('account/register', '', 'SSL'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('account/wishlist'));
			}

			$json['total'] = sprintf($this->language->get('text_wishlist'), (isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0));

			$json['total_qtd'] = (isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0);
		}

		$this->response->setOutput(json_encode($json));
	}

	public function toggle()
	{
		$this->language->load('account/wishlist');

		if (!isset($this->session->data['wishlist'])) {
			$this->session->data['wishlist'] = array();
		}

		$json = array();

		if (isset($this->request->post['product_id'])) {
			$this->load->model('catalog/product');

			$product_info = $this->model_catalog_product->getProduct($this->request->post['product_id']);

			if ($product_info) {
				if ($this->customer->isLogged()) {
					$key = array_search($this->request->post['product_id'], $this->session->data['wishlist']);

					if ($key !== false) {
						unset($this->session->data['wishlist'][$key]);

						$json['success'] = sprintf($this->language->get('text_success_remove'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('account/wishlist'));
						$json['toggle'] = true;
					} else if (isset($this->request->post['product_id']) && !in_array($this->request->post['product_id'], $this->session->data['wishlist'])) {
						$this->session->data['wishlist'][] = $this->request->post['product_id'];

						$json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('account/wishlist'));
						$json['toggle'] = true;
					} else {
						$json['toggle'] = false;
					}

				} else {
					$json['success'] = sprintf($this->language->get('text_login'), $this->url->link('account/register', '', 'SSL'), $this->url->link('account/register', '', 'SSL'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('account/wishlist'));
				}

				$json['total'] = sprintf($this->language->get('text_wishlist'), (isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0));

				$json['total_qtd'] = (isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0);
			}

			$this->response->setOutput(json_encode($json));
		}
	}
}