<?php

namespace App\Classes\Modules\Orders\Processors;

use App\Classes\Modules\Addresses\DataTransferObjects\AddressObject;
use App\Classes\Modules\Addresses\Services\CreatesAddress;
use App\Classes\Modules\Addresses\Services\FetchesAddress;
use App\Classes\Modules\Orders\DataTransferObjects\OrderObject;
use App\Classes\Modules\Orders\Services\CreatesOrder;
use App\Classes\Modules\Orders\Standards\Rules\CanCreateOrder;
use App\Classes\Modules\Orders\Processors\ConfirmOrderProcessor;
use App\Classes\Modules\OrderSteps\Processors\CreateOrderStepsProcessor;
use App\Classes\Modules\Steps\Processors\CreateStepsProcessor;
use App\Classes\Modules\Unity\Services\CreatesContract;
use App\Classes\ValueObjects\Constants\OrderStatus;
use App\Classes\ValueObjects\Constants\OrderSteps;
use App\Classes\ValueObjects\Constants\OrderType;
use App\Classes\Modules\Unity\Processors\CreateContractProcessor;
use App\Classes\Modules\Unity\Processors\ActivateContractProcessor;
use App\Classes\Modules\Unity\Processors\CreateContractEntityProcessor;
use App\Classes\Modules\Unity\Processors\AssignContractEntityProcessor;
use App\Classes\Modules\Orders\Processors\CreateOrderRolesProcessor;
use App\Classes\Modules\Addresses\Processors\CreateOrderAddressProcessor;
use Illuminate\Http\Request;

class CreateOrderProcessorOld
{
    /** @var CanCreateOrder */
    private $canCreateOrder;

    /** @var CreatesOrder */
    private $createsOrder;

    /** @var ConfirmOrderProcessor */
    private $confirmOrderProcessor;

    /** @var CreateStepsProcessor */
    private $createStepsProcessor;

    /** @var CreatesContract */
    private $unityCreateContract;

    /** @var AssignContractEntityProcessor */
    private $unityAssignContractEntity;

    /** @var CreateContractEntityProcessor */
    private $unityCreateContractEntity;

    /** @var ActivateContractProcessor */
    private $unityActivateContract;

    /** @var CreateOrderRolesProcessor */
    private $createOrderRoleProcessor;

    /** @var CreatesAddress */
    private $createAddress;

    /** @var FetchesAddress */
    private $fetchesAddress;

    /**
     * CreateOrderProcessorOld constructor.
     * @param CanCreateOrder $canCreateOrder
     * @param CreatesOrder $createsOrder
     * @param \App\Classes\Modules\Orders\Processors\ConfirmOrderProcessor $confirmOrderProcessor
     * @param CreateStepsProcessor $createStepsProcessor
     * @param CreatesContract $unityCreateContract
     * @param AssignContractEntityProcessor $unityAssignContractEntity
     * @param CreateContractEntityProcessor $unityCreateContractEntity
     * @param ActivateContractProcessor $unityActivateContract
     * @param \App\Classes\Modules\Orders\Processors\CreateOrderRolesProcessor $createOrderRoleProcessor
     * @param CreatesAddress $createAddress
     * @param FetchesAddress $fetchesAddress
     */
    public function __construct(CanCreateOrder $canCreateOrder, CreatesOrder $createsOrder, \App\Classes\Modules\Orders\Processors\ConfirmOrderProcessor $confirmOrderProcessor, CreateStepsProcessor $createStepsProcessor, CreatesContract $unityCreateContract, AssignContractEntityProcessor $unityAssignContractEntity, CreateContractEntityProcessor $unityCreateContractEntity, ActivateContractProcessor $unityActivateContract, \App\Classes\Modules\Orders\Processors\CreateOrderRolesProcessor $createOrderRoleProcessor, CreatesAddress $createAddress, FetchesAddress $fetchesAddress)
    {
        $this->canCreateOrder = $canCreateOrder;
        $this->createsOrder = $createsOrder;
        $this->confirmOrderProcessor = $confirmOrderProcessor;
        $this->createStepsProcessor = $createStepsProcessor;
        $this->unityCreateContract = $unityCreateContract;
        $this->unityAssignContractEntity = $unityAssignContractEntity;
        $this->unityCreateContractEntity = $unityCreateContractEntity;
        $this->unityActivateContract = $unityActivateContract;
        $this->createOrderRoleProcessor = $createOrderRoleProcessor;
        $this->createAddress = $createAddress;
        $this->fetchesAddress = $fetchesAddress;
    }


    public function execute(Request $request){

        $reference_no = rand(1000000, 9999999);

        //Call Unity to create contract and get contract reference no (and all the obligations)
        $contract = $this->unityCreateContract->execute();
        $contract_reference_no = $contract->hash_id;
        $contract_obligation_list = $contract->contract_obligation_list;

        //Activate Contract (if not activated then cannot update status)
        $activated = $this->unityActivateContract->execute($contract_reference_no);

        //Create Order on Shipping Platform
        $order_object = new OrderObject($request->get('company_module_id'), $reference_no, $contract_reference_no, OrderType::SHARED_CONTAINER, OrderStatus::PROCESSING, OrderSteps::PROCESSING, 0, $request->get('address_id'), $request->get('warehouse_id'));
        $this->canCreateOrder->passes($order_object);
        $order = $this->createsOrder->execute($order_object);

        $address = $this->fetchesAddress->execute(['id' => $order_object->getAddressId()]);

        $addressObject = new AddressObject($address->street_one, $address->street_two, $address->country_id, $address->state_id, $address->district_id, $address->postcode,$address->reference);

        //Add address (polymorphic)
        $this->createAddress->execute($order, $addressObject);

        //Add order roles - Allow entity to process this order
        //This processor calls Unity to create Contract Entity ID
        $orderRoles = $this->createOrderRoleProcessor->execute($request, $order->id, $contract_reference_no);

        //Call Unity to assign contract entity to obligation
        $this->unityAssignContractEntity->execute($request, $orderRoles, $contract_obligation_list);

        //Add processing to order step
        //Note: For processing apointee_id is CIEF Freight Forward , id is 1, reserved in company table
        $this->createStepsProcessor->execute($order, OrderSteps::PROCESSING, 1);

        //Pre-approved order, no need Admin to process it
        //This processor then calls Generate Order Steps
        $this->confirmOrderProcessor->execute($order->id,  $contract_obligation_list, $orderRoles);

        return $order;
    }
}
