<?php

namespace Lc\ShopBundle\Services ;

use Doctrine\ORM\EntityManagerInterface;
use Lc\ShopBundle\Context\DocumentInterface;
use Lc\ShopBundle\Context\MerchantUtilsInterface;
use Lc\ShopBundle\Model\Document ;

class DocumentUtils
{
        protected $em ;
        protected $merchantUtils ;
        protected $documentRepository ;

        public function __construct(EntityManagerInterface $em, MerchantUtilsInterface $merchantUtils)
        {
                $this->em = $em ;
                $this->documentRepository = $this->em->getRepository($this->em->getClassMetadata(DocumentInterface::class)->getName()) ;
                $this->merchantUtils = $merchantUtils ;
        }

        public function generateReference($documentType)
        {
                $prefix = '';
                if($documentType == Document::TYPE_DELIVERY_NOTE) {
                        $prefix = 'BL';
                }
                elseif($documentType == Document::TYPE_PURCHASE_ORDER) {
                        $prefix = 'BC';
                }
                elseif($documentType == Document::TYPE_INVOICE) {
                        $prefix = 'FA';
                }
                elseif($documentType == Document::TYPE_QUOTATION) {
                        $prefix = 'DE';
                }

                $oneDocumentExist = $this->documentRepository->findOneBy([
                        'status' => 1,
                        'type' => $documentType,
                        'merchant' => $this->merchantUtils->getMerchantCurrent()
                ], [
                        'reference' => 'DESC'
                ]);

                if ($oneDocumentExist) {
                        $reference = $oneDocumentExist->getReference();
                        $pattern = '#([A-Z]+)?([0-9]+)#';
                        preg_match($pattern, $reference, $matches, PREG_OFFSET_CAPTURE);
                        $sizeNumReference = strlen($matches[2][0]);
                        $numReference = ((int)$matches[2][0]) + 1;
                        $numReference = str_pad($numReference, $sizeNumReference, '0', STR_PAD_LEFT);

                        return $prefix . $numReference;
                }
                else {
                        return $prefix . '00001';
                }
        }

        public function createDocument($params = [])
        {
                $documentClass = $this->em->getClassMetadata(DocumentInterface::class)->getName();
                $document = new $documentClass ;

                if(isset($params['merchant'])) {
                        $document->setMerchant($params['merchant']) ;
                }
                else {
                        $document->setMerchant($params['order_shops'][0]->getMerchant()) ;
                }

                foreach($params['order_shops'] as $orderShop) {
                        $document->addOrderShop($orderShop) ;
                }

                $document->setType($params['type']) ;
                $document->setTitle($params['title']) ;
                $document->setStatus((isset($params['status'])) ? $params['status'] : 1) ;
                $document->setReference($this->generateReference($params['type'])) ;
                $document->setMerchantAddress($params['merchant_address']) ;
                $document->setBuyerAddress($params['buyer_address']) ;
                $document->setMerchantAddressText($params['merchant_address']->getSummary()) ;
                $document->setBuyerAddressText($params['buyer_address']->getSummary()) ;
                $document->setCreatedBy($params['created_by']) ;
                $document->setUpdatedBy($params['created_by']) ;

                $this->em->persist($document);
                $this->em->flush() ;

                return $document ;
        }
}