<?php
namespace Leadlion\AutogedalOrderFlow\Helper;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Store\Model\ScopeInterface;
use Magento\Catalog\Model\ProductFactory;
use Magento\Framework\App\Helper\Context;
use Leadlion\Autogedal\Helper\Data as CustomHelper;
use Eadesigndev\Awb\Model\AwbRepository;

class Data extends AbstractHelper
{
    public $productFactory;
    protected $customHelper;
    protected $awbRepository;

    public function __construct(
        Context $context,
        ProductFactory $productFactory,
        CustomHelper $customHelper,
        AwbRepository $awbRepository
    ) {
        $this->customHelper = $customHelper;
		$this->productFactory = $productFactory;
        $this->awbRepository = $awbRepository;
        parent::__construct($context);
    }

	public function getAwbStatusCode()
    {
        return $this->scopeConfig->getValue(
            'orderflow/order_status/awb_order_status', ScopeInterface::SCOPE_STORE
        );
    }

	public function getInvoicedStatusCode()
    {
        return $this->scopeConfig->getValue(
            'orderflow/order_status/invoice_order_status', ScopeInterface::SCOPE_STORE
        );
    }

	public function getSmartbillPdfSeries()
    {
        return $this->scopeConfig->getValue(
            'smartbill_integration/smartbill_settings/vat_code', ScopeInterface::SCOPE_STORE
        );
    }

    public function getOrderItemsDpd($order)
    {
        if (isset($order) ) {
            $orderItem = [];
            foreach($order->getAllItems() as $item) {
                if ($item->getParentItem()) {
                    //skip produse cu parents
                    continue;
                }
                $qty = (int) $item->getQtyOrdered();
                $productId = $item->getProductId();
                $_product = $this->productFactory->create()->load($productId);
                $attributeSetsIds = $this->customHelper->getAttributesSetsIdsForShowingSku() ?
                                    explode(',', $this->customHelper->getAttributesSetsIdsForShowingSku()) : [];
                if (in_array($_product->getAttributeSetId(), $attributeSetsIds)) {
                    $skuFz = $_product->getSku();
                } else {
                    $skuFz = $_product->getSkuFz();
                }
                
                $orderItem[] = $qty .'X'.$skuFz;
            }
        }
        return implode(',', $orderItem);
    }

    public function getOrderItems($order)
    {
        if (isset($order) ) {
            $orderItem = [];
            foreach($order->getAllItems() as $item) {
                if ($item->getParentItem()) {
                    //skip produse cu parents
                    continue;
                }
                $qty = (int) $item->getQtyOrdered();
                $name = $item->getName();
                $productId = $item->getProductId();
                $_product = $this->productFactory->create()->load($productId);
                $attributeSetsIds = $this->customHelper->getAttributesSetsIdsForShowingSku() ?
                                    explode(',', $this->customHelper->getAttributesSetsIdsForShowingSku()) : [];
                if (in_array($_product->getAttributeSetId(), $attributeSetsIds)) {
                    $skuFz = $_product->getSku();
                } else {
                    $skuFz = $_product->getSkuFz();
                }
                $orderItem[] = $qty .'X'.$skuFz.' - '.$name;
            }
        }
        return implode(',', $orderItem);
    }

    public function getProductWeight($order)
    {
        if (empty($order->getData('weight')) || $order->getData('weight') <= 0) {
            $weight = 0;
            foreach($order->getAllItems() as $item) {
                $productId = $item->getProductId();
                $_product = $this->productFactory->create()->load($productId);
                if ($_product->getGreutate()) {
                    $productWeight = (int)trim(str_replace("kg", "", $_product->getGreutate()));
                    $weight += $productWeight;
                }
            }
        } else {
            $weight = $order->getData('weight');
        }
        return $weight;
    }

    public function getApiUrl()
    {
        return $this->scopeConfig->getValue(
            'carriers/packeta/url_api', ScopeInterface::SCOPE_STORE
        );
    }

    public function getApiPassword()
    {
        return $this->scopeConfig->getValue(
            'carriers/packeta/parola_api', ScopeInterface::SCOPE_STORE
        );
    }

    public function getAwbDocument($identifier, $pickupId = null)
    {
        $courierNumber = $this->generateCourierNumber($identifier);
        $awb = $this->generateAwbLabel($identifier, $courierNumber);

        return $awb;
    }

    public function generateCourierNumber($packetId)
    {
        try {
            $apiPassword = $this->getApiPassword();

            $client = new \SoapClient($this->getApiUrl());
            return $client->packetCourierNumberV2($apiPassword, $packetId)->courierNumber;
        } catch (\SoapFault $e) {
            echo $e->getMessage();
        }
        
    }
    
    public function generateAwbLabel($packetId, $courierNumber)
    {
        $apiPassword = $this->getApiPassword();

        try {
            $client = new \SoapClient($this->getApiUrl());
            $pdf = $client->packetCourierLabelPdf($apiPassword, $packetId, $courierNumber);
            return $pdf;
        } catch (\SoapFault $e) {
            echo $e->getMessage();
        }    
    }
}
