<?php

$loader = $this->registry->get('load');
$loader->controller('catalog/product');

use system\library\marketplace\MktPriceAdjustments;

class ControllerModuleGaleriapageProduct extends ControllerCatalogProduct
{

    public function __construct($registry)
    {
        parent::__construct($registry);
        $this->setTypeMarketplace(Type_MarketPlace::Tp_Galeriapage);
        $this->document->setTitle('Produtos Galeria Pagé');

        $this->data['status_anuncio_marketplace'] = array(
            TypeGaleriaPage_Status::Tp_Habilitado => TypeGaleriaPage_Status::getNickFromValue(TypeGaleriaPage_Status::Tp_Habilitado),
            TypeGaleriaPage_Status::Tp_Desabilitado => TypeGaleriaPage_Status::getNickFromValue(TypeGaleriaPage_Status::Tp_Desabilitado),
        );

        $this->mktPriceAdjustments = new MktPriceAdjustments($registry, ['tipo_integracao' => Type_MarketPlace::Tp_Galeriapage]);

    }

    public function index()
    {
        $this->document->addScript(array(
            'view/public/js/page-mkt-vincular.js',
            '../catalog/view/theme/vista/js/accordion-futura.js',
            'view/javascript/page.products.galeriapage.js',
        ));

        parent::index();
    }

    public function mktInfo($setting)
    {
        $this->load->helper('ftr_validacao');
        $setting = current($setting);
        $url = parent::getLinkUrl($this->request->get);

        if ($setting['IsGrid']) {
            $type_product = Type_ProdutoMkt::Tp_MktProd_Grade;
        } else {
            $type_product = Type_ProdutoMkt::Tp_MktProd_Normal;
        }

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

        $paramKey = '&product_id=' . $setting['product_key_id'] . '&type_product=' . $type_product . '&type_marketplace=' . $this->getTypeMarketplace();
        $syn_url = $this->url->link('marketplace/Marketplacecalls/synchronize', 'token=' . $this->session->data['token'] . $paramKey . $url, 'SSL');
        $unbind_url = $this->url->link('marketplace/Marketplacecalls/desynchronize', 'token=' . $this->session->data['token'] . $paramKey . $url, 'SSL');

        $mkt_bloq_opcoes = false;
        if (!$setting['codigo_barra']) {
            $this->session->data['mkt_warning'][$setting['product_id']] = $this->language->get('warning_mkt_codigo_barras');
            $mkt_bloq_opcoes = true;
        }

        if ($setting['IsGrid']) {
            $mkt_data = $this->dmodel_marketplace_mktproduto->getMarketplacesUniqueProductsFromGrid($setting['product_grid_id'], $this->getTypeMarketplace());
            $this->load->dmodel('marketplace/mktproduto' . strtolower(Type_MarketPlace::getNickFromConstValue($this->getTypeMarketplace())));
            $grid_data = $this->{'dmodel_marketplace_mktproduto' . strtolower(Type_MarketPlace::getNickFromConstValue($this->getTypeMarketplace()))}->getProductsFromGrid($setting['product_grid_id']);
        } else {
            $mkt_data = $this->dmodel_marketplace_mktproduto->getMarketplacesProducts($setting['IsGrid'] ? $setting['product_grid_id'] : $setting['product_id'], $type_product, $this->getTypeMarketplace());
        }

        $dadosMktp = array(
            'error' => $this->ftrgeralregistry->getMktError('mkt_error', $setting['product_key_id']),
            'warning' => $this->ftrgeralregistry->getMktError('mkt_warning', $setting['product_key_id']),
            'sincronizar' => $syn_url,
            'bloq_opcoes_mkt' => $mkt_bloq_opcoes
        );

        if ($mkt_data) {
            $dadosMktp['mktvinculo'] = true;
            $dadosMktp['unsync'] = $unbind_url;
            $dadosMktp['IsGrid'] = $setting['IsGrid'];

            if ($setting['IsGrid']) {
                foreach ($mkt_data as $key => $mkt_data_info) {

                    $mktPrice = array(
                        'marketplace_price_id' => $mkt_data_info['marketplace_price_id'],
                        'product_id' => $mkt_data_info['product_key_id'],
                        'price' => $grid_data[$key]['price']
                    );

                    $price = $this->mktPriceAdjustments->getMktPrice($mktPrice);

                    $dadosMktp['products'][] = array(
                        'price' => $price,
                        'formatedPrice' => $this->currency->format($price),
                        'marketId' => $mkt_data_info['marketId'],
                        'status' => $mkt_data_info['status'],
                        'url' => (!empty($mkt_data_info['url']) && is_object(json_decode($mkt_data_info['url']))) ? json_decode($mkt_data_info['url'], true) : $mkt_data_info['url'],
                        'grid_data' => $grid_data[array_search($mkt_data_info['product_key_id'], array_column($grid_data, 'product_id'))],
                        'product_key_id' => $mkt_data_info['product_key_id'],
                        'warning' => $this->ftrgeralregistry->getMktError('mkt_warning', $mkt_data_info['product_key_id'])
                    );

                }
            } else {

                $setting['marketplace_price_id'] = $mkt_data['marketplace_price_id'];
                $mkt_price = $this->mktPriceAdjustments->getMktPrice($setting);

                $dadosMktp['price'] = $mkt_price;
                $dadosMktp['formatedPrice'] = $this->currency->format($mkt_price);
                $dadosMktp['products'][] = array(
                    'marketId' => $mkt_data['marketId'],
                    'status' => $mkt_data['status'],
                    'url' => (!empty($mkt_data['url']) && is_object(json_decode($mkt_data['url']))) ? json_decode($mkt_data['url'], true) : $mkt_data['url'],
                    'product_key_id' => $setting['product_key_id'],
                );
            }
        } else {
            $dadosMktp['marketId'] = false;
            $dadosMktp['status'] = false;
            $dadosMktp['url'] = false;
            $dadosMktp['unsync'] = false;
            $dadosMktp['desvinc'] = false;
            $dadosMktp['mktvinculo'] = false;
        }

        $this->data['marketplace'] = $dadosMktp;
        $this->data['not_list_mkt'] = $this->language->get('text_not_list_mkt');
        $this->data['product_status'] = ($setting['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled'));
        $this->data['not_list_mkt_can'] = $this->language->get('not_list_mkt_can');
        $this->data['lang_syn'] = $this->language->get('text_synchronize');
        $this->data['lang_desyn'] = $this->language->get('text_desynchronize');
        $this->data['lang_vinc'] = $this->language->get('lang_vinc');
        $this->data['view_ad'] = $this->language->get('view_ad');
        $this->data['text_see_original'] = $this->language->get('text_see_original');
        $this->data['edit_datasheet'] = $this->language->get('edit_datasheet');
        $this->data['lang_bulk_product'] = $this->language->get('text_edit_bulksetting');
        $this->data['name'] = $setting['name'];
        $this->data['type_product'] = $type_product;
        $this->data['product_key_id'] = $setting['product_key_id'];
        $this->data['type_marketplace'] = $this->getTypeMarketplace();

        $this->response->setOutput($this->render('module/galeriapage/product_list'));

    }


}