<?php

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

use system\library\marketplace\MktPriceAdjustments;

class ControllerModuleSkyhubProduct extends ControllerCatalogProduct
{

    public function __construct($registry)
    {
        parent::__construct($registry);
        $this->setTypeMarketplace(Type_MarketPlace::Tp_SkyHub);
        $this->document->setTitle('Produtos Skyhub');

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

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

    }

    public function index()
    {
        $this->document->addScript(array(
            'view/javascript/page.products.skyhub.js',
            'view/public/js/page-mkt-vincular.js',
        ));

        parent::index();
    }

    public function mktInfo($setting)
    {
        $this->load->helper('ftr_validacao');
        $this->load->dmodel('skyhub/skyproduto');
        $this->load->type('tiposkyhub');
        $setting = current($setting);
        $url = parent::getLinkUrl($this->request->get);
        $mkt_error_grade = false;

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

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

        $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_data = $this->dmodel_marketplace_mktproduto->getMarketplacesProducts($setting['IsGrid'] ? $setting['product_grid_id'] : $setting['product_id'], $type_product, $this->getTypeMarketplace());

        if ($mkt_data) {
            $setting['marketplace_price_id'] = $mkt_data['marketplace_price_id'];
        }

        $mkt_price = $this->mktPriceAdjustments->getMktPrice($setting);

        $dadosMktp = array(
            'price' => $mkt_price,
            'formatedPrice' => $this->currency->format($mkt_price),
            'error' => $this->ftrgeralregistry->getMktError('mkt_error', $setting['product_key_id']),
            'warning' => $this->ftrgeralregistry->getMktError('mkt_warning', $setting['product_key_id']),
            'sincronizar' => $syn_url,
            'codigo_barra' => ($setting['codigo_barra']) ? $setting['codigo_barra'] : false,
            'bloq_opcoes_mkt' => false
        );


        if ($mkt_data) {
            $dadosMktp['marketId'] = $mkt_data['marketId'];
            $dadosMktp['status'] = $mkt_data['status'];
            $dadosMktp['url'] = (!empty($mkt_data['url']) && is_object(json_decode($mkt_data['url']))) ? json_decode($mkt_data['url'], true) : $mkt_data['url'];
            $dadosMktp['unsync'] = $unbind_url;
            $dadosMktp['mktvinculo'] = true;//indica que há um vinculo com a tabela, para ter uma variavel de controle que não dependa de valor da base...
        } else {
            $dadosMktp['marketId'] = false;
            $dadosMktp['status'] = false;
            $dadosMktp['url'] = false;
            $dadosMktp['unsync'] = false;
            $dadosMktp['desvinc'] = false;
            $dadosMktp['mktvinculo'] = false;
        }

        if ($mkt_error_grade) {
            $dadosMktp['mkt_error_grade'][$setting['product_id']] = $mkt_error_grade;
            $dadosMktp['unsync'] = $unbind_url;
            $dadosMktp['mktvinculo'] = true;
        }

        $platforms = $this->dmodel_skyhub_skyproduto->getSkyhubPlatforms($setting['product_key_id'], $type_product);
        foreach ($platforms as $key => $value) {
            switch ($value['status']) {
                case TypeSkyHubAssociationStatus::Tp_Linked:
                case TypeSkyHubAssociationStatus::Tp_Resume:
                case TypeSkyHubAssociationStatus::Tp_Associate:
                    $platforms[$key]['tag_class'] = 'background-success text-success';
                    break;
                case TypeSkyHubAssociationStatus::Tp_UnLink:
                    $platforms[$key]['tag_class'] = 'background-error text-error';
                    break;
                default:
                    $platforms[$key]['tag_class'] = 'background-gray text-gray';
                    break;
            }
        };

        $this->data['marketplace'] = $dadosMktp;
        $this->data['not_list_mkt'] = sprintf($this->language->get('not_list_mkt'), Type_MarketPlace::getNickFromConstValue($this->getTypeMarketplace()));
        $this->data['produc_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('lang_syn');
        $this->data['lang_desyn'] = $this->language->get('lang_desyn');
        $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['lang_bulk_product'] = $this->language->get('lang_bulk_product');
        $this->data['name'] = $setting['name'];
        $this->data['product_key_id'] = $setting['product_key_id'];
        $this->data['type_product'] = $type_product;
        $this->data['platforms'] = $platforms;
        $this->data['type_marketplace'] = $this->getTypeMarketplace();

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

    }


}