<?php

final class MktPlace
{

    public $registry;
    public $logArray;
    private $marketplaces;
    private $errorMkt;

    public function __construct($registry)
    {
        $this->registry = $registry;
        $this->marketplaces[Type_MarketPlace::Tp_Mercadolivre] = new Mercadolivre($this->registry);
        $this->marketplaces[Type_MarketPlace::Tp_SkyHub] = new SkyHub($this->registry);
        $this->marketplaces[Type_MarketPlace::Tp_Galeriapage] = new GaleriaPage($this->registry);
        $this->marketplaces[Type_MarketPlace::Tp_Shopee] = new Shopee($this->registry);
        $logArray = array();
    }

    public function setAtualizarProduto(MktProduto $produto, $marketplace, $data = array())
    {
        try {
            return $this->marketplaces[$marketplace]->setAtualizarProduto($produto, $data);
        } catch (ExceptionProductNotExists $e) {
            throw new ExceptionProductNotExists($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        } catch (ExceptionErrorShow $e) {
            throw new ExceptionErrorShow($this->marketplaces[$marketplace]->TranslateError($e->getMessage(), $data));
        } catch (Exception $e) {
            throw new Exception($this->marketplaces[$marketplace]->TranslateError($e->getMessage(), $data), 0, $e);
        }
    }

    public function setDesincronizarProduto($marketId, $id, $marketplace, $product = null)
    {
        try {
            return $this->marketplaces[$marketplace]->setDesincronizarProduto($marketId, $id, $product);
        } catch (Exception $e) {
            throw new Exception($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        }
    }

    public function setAtualizarEstoquePreco(MktProduto $produto, $mkt_product_id, $marketplace)
    {
        try {
            return $this->marketplaces[$marketplace]->setAtualizarEstoquePreco($produto, $mkt_product_id);
        } catch (ExceptionProductNotExists $e) {
            throw new ExceptionProductNotExists($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        } catch (Exception $e) {
            throw new Exception($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        }
    }

    public function setAtualizarEstoquePrecoStatus(MktProduto $produto, $mkt_product_id, $marketplace)
    {
        try {
            return $this->marketplaces[$marketplace]->setAtualizarEstoquePrecoStatus($produto, $mkt_product_id);
        } catch (ExceptionProductNotExists $e) {
            throw new ExceptionProductNotExists($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        } catch (Exception $e) {
            throw new Exception($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        }
    }

    public function setAtualizarEstoque(MktProduto $produto, $mkt_product_id, $marketplace)
    {
        try {
            return $this->marketplaces[$marketplace]->setAtualizarEstoque($produto, $mkt_product_id);
        } catch (ExceptionProductNotExists $e) {
            throw new ExceptionProductNotExists($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        } catch (Exception $e) {
            throw new Exception($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        }
    }

    public function setAtualizarEstoqueFromProdutos(MktProduto $produto, $produtoRetorno, $marketplace)
    {
        try {
            return $this->marketplaces[$marketplace]->setAtualizarEstoqueFromProdutos($produto, $produtoRetorno);
        } catch (Exception $e) {
            throw new Exception($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        }
    }

    public function getProduto($marketplace, $mkt_product)
    {
        try {
            return $this->marketplaces[$marketplace]->getProduto($mkt_product);
        } catch (Exception $e) {
            throw new Exception($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        }
    }

    public function getOrder($marketplace, $mktOrderId)
    {
        try {
            return $this->marketplaces[$marketplace]->getOrder($mktOrderId);
        } catch (Exception $e) {
            throw new Exception($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        }
    }

    public function getOrders($marketplace, $data)
    {
        try {
            return $this->marketplaces[$marketplace]->getOrders($data);
        } catch (Exception $e) {
            throw new Exception($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        }
    }

    public function deleteOrderQueue($marketplace, $mktOrderId)
    {
        try {
            return $this->marketplaces[$marketplace]->deleteOrderQueue($mktOrderId);
        } catch (Exception $e) {
            throw new Exception($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        }
    }

    public function setEnviarNFEData($marketplace, $mktOrderId, $data)
    {
        try {
            return $this->marketplaces[$marketplace]->setEnviarNFEData($mktOrderId, $data);
        } catch (Exception $e) {
            throw new Exception($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        }
    }

    public function setEnviarNFE($marketplace, $mktOrderId, $fiscalKey, $qty_vols = 1, $nfe_number = "")
    {
        try {
            return $this->marketplaces[$marketplace]->setEnviarNFE($mktOrderId, $fiscalKey, $qty_vols, $nfe_number);
        } catch (Exception $e) {
            throw new Exception($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        }
    }

    public function setEnviarNFEDirect($marketplace, $mktOrderId, $xml_arquivo, $qty_vols, $data_hora_emissao)
    {
        try {
            return $this->marketplaces[$marketplace]->setEnviarNFEDirect($mktOrderId, $xml_arquivo, $qty_vols, $data_hora_emissao);
        } catch (Exception $e) {
            throw new Exception($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        }
    }

    public function setPedidoEnvio($marketplace, $mktOrderId, $datasend)
    {
        try {
            return $this->marketplaces[$marketplace]->setPedidoEnvio($mktOrderId, $datasend);
        } catch (Exception $e) {
            throw new Exception($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        }
    }

    public function setPedidoEntregue($marketplace, $mktOrderId)
    {
        try {
            return $this->marketplaces[$marketplace]->setPedidoEntregue($mktOrderId);
        } catch (Exception $e) {
            throw new Exception($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        }
    }

    public function setCancelOrder($marketplace, $mktOrderId)
    {
        try {
            return $this->marketplaces[$marketplace]->setCancelOrder($mktOrderId);
        } catch (Exception $e) {
            throw new Exception($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        }
    }

    public function setOrderShipmentException($marketplace, $mktOrderId, $dataSend)
    {
        try {
            return $this->marketplaces[$marketplace]->setOrderShipmentException($mktOrderId, $dataSend);
        } catch (Exception $e) {
            throw new Exception($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        }
    }

    public function isValidMarketPlaceType($marketplace)
    {
        if (is_null($marketplace)) {
            return false;
        }
        foreach (Type_MarketPlace::getEnumListConst() as $value) {
            if ($value == $marketplace)
                return true;
        }

        return false;
    }

    public function splitEtiqueta($marketplace, $mktOrderId)
    {
        try {
            return $this->marketplaces[$marketplace]->splitEtiqueta($mktOrderId);
        } catch (Exception $e) {
            throw new Exception($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        }
    }

    public function sendPictures($marketplace, $pictures, $marketId)
    {
        try {
            return $this->marketplaces[$marketplace]->sendPictures($pictures, $marketId);
        } catch (Exception $e) {
            throw new Exception($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        }
    }

    public function pauseProduct($marketplace, $marketId)
    {
        try {
            return $this->marketplaces[$marketplace]->pauseProduct($marketId);
        } catch (Exception $e) {
            throw new Exception($this->marketplaces[$marketplace]->TranslateError($e->getMessage()));
        }
    }

}