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

class ControllerModuleBuyLater extends ControllerProductProductList {
    protected $referencePathFile = 'module/buy_later';
    protected $fileName;
    protected $pathName;


    public function __construct($registry)
    {
        parent::__construct($registry);

        $referenceArray = explode('/', $this->referencePathFile);
        $this->fileName = $referenceArray[1];
        $this->pathName = $referenceArray[0];

    }

    public function index($setting = array()) {

        if ($this->buylater->countProducts() <= 0) {
            return false;
        }

        if (isset($setting['hide_title'])) {
            $heading_title = false;
        } else {
            $heading_title = $this->config->get('buy_later_title') ?? $this->language->get('heading_title');
        }

        $language = $this->language->load($this->referencePathFile);
        $this->data = array_merge($this->data, $language);
        $this->isAjax = isset($this->request->get['ajax']) && $this->request->get['ajax'];
        $this->data['heading_title'] = $heading_title;
        $this->data['viewall']       = $this->language->get('viewall');
        $this->data['grid']             = (isset($setting['entry_type'])) ? $setting['entry_type'] : 0;
        $this->data['product_list_title_style'] =  $this->config->get('product_list_title_style');
        $this->data['Type_ProductListTitleStyle'] = $this->load->typeForTwig('Type_ProductListTitleStyle');
        $this->data['Type_ModulePosition'] = $this->load->typeForTwig('Type_ModulePosition');

        $setting['cart'] = isset($this->request->get['cart']) && $this->request->get['cart'];
        $this->childrenData = array(
            'product/productList/moduleBuyLater' => $setting
        );


        if ($this->isAjax) {
            $this->render($this->referencePathFile);
            $this->response->setOutputJson($this->data);
        } else {
            $this->render($this->referencePathFile);
        }
    }

    public function add()
    {
        $this->language->load($this->referencePathFile);
        $this->load->model('catalog/product');
        $this->load->type('tipocomum');

        $json = array();

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

        $product_info = $this->model_catalog_product->getProduct($product_id);
        if ($product_info) {
            if (!isset($this->request->get['quantity']) || ($this->request->get['quantity'] == '' || $this->request->get['quantity'] <= 0)) {
                $quantity = 1;
            } else {
                $quantity = $this->request->get['quantity'];
            }

            $this->buylater->add($product_id, $quantity);
            $json['success'] = $this->language->get('text_add_success');
        }

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

    public function remove()
    {
        $this->language->load($this->referencePathFile);
        $this->load->model('catalog/product');
        $this->load->type('tipocomum');

        $json = array();

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

        $this->buylater->remove($product_id);
        $json['success'] = true;

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

    public function reloadBuyLaterList()
    {
        return $this->index();
    }
}
