<?php

namespace common\logic\Document\DeliveryNote\Service;

use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Document\DeliveryNote\Event\DeliveryNoteCreateEvent;
use common\logic\Document\DeliveryNote\Model\DeliveryNote;
use common\logic\Document\DeliveryNote\Repository\DeliveryNoteRepository;
use common\logic\Document\Document\Service\DocumentBuilder;
use common\logic\Order\Order\Model\Order;
use common\logic\Order\Order\Service\OrderSolver;
use common\logic\PointSale\PointSale\Model\PointSale;
use common\logic\User\User\Repository\UserRepository;
use common\logic\User\User\Service\UserSolver;
use yii\base\ErrorException;

class DeliveryNoteBuilder extends DocumentBuilder
{
    protected UserSolver $userSolver;
    protected UserRepository $userRepository;
    protected DeliveryNoteRepository $deliveryNoteRepository;
    protected OrderSolver $orderSolver;

    public function loadDependencies(): void
    {
        parent::loadDependencies();
        $this->userSolver = $this->loadService(UserSolver::class);
        $this->userRepository = $this->loadService(UserRepository::class);
        $this->deliveryNoteRepository = $this->loadService(DeliveryNoteRepository::class);
        $this->orderSolver = $this->loadService(OrderSolver::class);
    }

    public function instanciateDeliveryNote(): DeliveryNote
    {
        $deliveryNote = new DeliveryNote();

        $this->initDocumentProducer($deliveryNote);
        $this->initTaxCalculationMethod($deliveryNote);

        return $deliveryNote;
    }

    public function createDeliveryNoteForPointSale(PointSale $pointSale, Distribution $distribution, array $idOrders): DeliveryNote
    {
        $deliveryNote = $this->instanciateDeliveryNote();
        $this->initDeliveryNoteNameByPointSale($deliveryNote, $distribution, $pointSale);
        $this->initDeliveryNoteUserFromPointSale($deliveryNote, $pointSale);
        $this->initDeliveryNoteAddress($deliveryNote);
        $deliveryNote->save();

        $this->triggerDeliveryNoteCreateEvent($deliveryNote, $idOrders);

        return $deliveryNote;
    }

    public function createDeliveryNoteForOrder(Order $order): DeliveryNote
    {
        $deliveryNote = $this->deliveryNoteRepository->findOneDeliveryNoteByOrder($order);
        $deliveryNote = $this->deleteDeliveryNoteIfStatusDraft($deliveryNote);

        if(!$deliveryNote) {
            $deliveryNote = $this->instanciateDeliveryNote();
            $this->initDeliveryNoteNameByOrder($deliveryNote, $order);
            $this->initDeliveryNoteUserFromOrder($deliveryNote, $order);
            $this->initDeliveryNoteAddress($deliveryNote);
            $deliveryNote->save();

            $this->triggerDeliveryNoteCreateEvent($deliveryNote, [$order->id]);
        }

        return $deliveryNote;
    }

    public function triggerDeliveryNoteCreateEvent(DeliveryNote $deliveryNote, array $idOrders)
    {
        $deliveryNoteCreateEvent = new DeliveryNoteCreateEvent();
        $deliveryNoteCreateEvent->deliveryNote = $deliveryNote;
        $deliveryNoteCreateEvent->idOrders = $idOrders;
        $deliveryNote->trigger(DeliveryNote::EVENT_CREATE, $deliveryNoteCreateEvent);
    }

    public function initDeliveryNoteUserFromPointSale(DeliveryNote $deliveryNote, PointSale $pointSale)
    {
        $deliveryNote->id_user = $pointSale->id_user;
    }

    public function initDeliveryNoteUserFromOrder(DeliveryNote $deliveryNote, Order $order)
    {
        $deliveryNote->id_user = $order->id_user;
    }

    public function initDeliveryNoteNameByPointSale(DeliveryNote $deliveryNote, Distribution $distribution, PointSale $pointSale)
    {
        $deliveryNote->name = 'Bon de livraison ' . $pointSale->name .
            ' (' . date('d/m/Y', strtotime($distribution->date)) . ')';
    }

    public function initDeliveryNoteNameByOrder(DeliveryNote $deliveryNote, Order $order)
    {
        $deliveryNote->name = 'Bon de livraison ' . $this->orderSolver->getOrderUsername($order) .
            ' (' . date('d/m/Y', strtotime($order->distribution->date)) . ')';
    }

    public function initDeliveryNoteAddress(DeliveryNote $deliveryNote)
    {
        $user = $this->userRepository->findOneUserById($deliveryNote->id_user);
        if($user) {
            $deliveryNote->address = $this->userSolver->getFullAddress($user);
        }
        else {
            throw new ErrorException("L'utilisateur du bon de livraison n'est pas défini.");
        }
    }

    public function deleteDeliveryNoteIfStatusDraft(DeliveryNote $deliveryNote = null): ?DeliveryNote
    {
        if ($deliveryNote && $this->documentSolver->isStatusDraft($deliveryNote)) {
            $deliveryNote->delete();
            $deliveryNote = null;
        }

        return $deliveryNote;
    }
}