<?php

namespace App\EventListener;


use Contao\CoreBundle\ServiceAnnotation\Hook;
use Contao\FrontendTemplate;
use Contao\Module;
use App\ExactApi\ApiExact;
use Isotope\Model\Address;
use Isotope\Model\Product;
use Isotope\Model\ProductCollection\Order;
use Isotope\Model\ProductCollectionItem;

class PostCheckoutListener
{
    const WAREHOUSE_ID = 'd8a6a9b8-d0ac-4d36-8d00-bd420d0d81f5';

    /**
     * @Hook("postCheckout")
     */
	//	public function __invoke(int $userId, array $userData, Module $module): void
  	public function __invoke(Order $order, $item): void
    {
			//$a = $order->getBillingAddress();
			
//			ob_start();
//			var_dump($order);
//			file_put_contents('flo.txt', ob_get_contents());
//			ob_end_clean();
//
//			ob_start();
//			var_dump($item);
//			file_put_contents('dan.txt', ob_get_contents());
//			ob_end_clean();
			
      //  throw new \Exception('ARRRRRRRGGGGGGGGGG');
        $apiExact = new ApiExact();

        ob_start();
        /** @var ProductCollectionItem $orderItem */
        foreach($order->getItems() as $orderItem) {
            /** @var Product $orderItem */
            $product = $orderItem->getProduct();
            var_dump($orderItem->id);
            var_dump($orderItem->sku);
            var_dump($product->id);
            var_dump($product->name);
            var_dump($product->is_set);
        }
        var_dump($apiExact->test());
//        var_dump($order->getItems());
        //var_dump($order);
        file_put_contents('dan.txt', ob_get_contents());
        ob_end_clean();



//        $shippingCustomer = null;
//        if ($this->compareAddresses() === false) {
//            $shippingCustomer = $apiExact->createCustomer(
//                $this->createCustomerApiData($order->getShippingAddress())
//            );
//        }
//
//        $billingCustomer = $apiExact->createCustomer(
//            $this->createCustomerApiData($order->getBillingAddress())
//        );
//
//        $this->createSalesOrderData($order, $billingCustomer, $shippingCustomer);

//        $billingFirstName = $order->getBillingAddress()->firstname;
//        $billingLastName = $order->getBillingAddress()->lastname;
//        $billingStreet = $order->getBillingAddress()->street_1;
//        $billingPostcode = $order->getBillingAddress()->postal;
//        $billingCity = $order->getBillingAddress()->city;
//        $billingCompany = $order->getBillingAddress()->company;
//        $billingPhone = $order->getBillingAddress()->phone;
//        $billingEmail = $order->getBillingAddress()->email;
//
//        $shippingFirstName = $order->getShippingAddress()->firstname;
//        $shippingLastName = $order->getShippingAddress()->lastname;
//        $shippingStreet = $order->getShippingAddress()->street_1;
//        $shippingPostcode = $order->getShippingAddress()->postal;
//        $shippingCity = $order->getShippingAddress()->city;
//        $shippingCompany = $order->getShippingAddress()->company;
//        $shippingPhone = $order->getShippingAddress()->phone;
//        $shippingEmail = $order->getShippingAddress()->email;

    }

    private function compareAddresses(Order $order)
    {
        return
            $order->getBillingAddress()->firstname === $order->getShippingAddress()->firstname &&
            $order->getBillingAddress()->lastname === $order->getShippingAddress()->lastname &&
            $order->getBillingAddress()->street_1 === $order->getShippingAddress()->street_1 &&
            $order->getBillingAddress()->postal === $order->getShippingAddress()->postal &&
            $order->getBillingAddress()->city === $order->getShippingAddress()->city &&
            $order->getBillingAddress()->company === $order->getShippingAddress()->company &&
            $order->getBillingAddress()->phone === $order->getShippingAddress()->phone &&
            $order->getBillingAddress()->email === $order->getShippingAddress()->email;
    }

    private function createCustomerApiData(Address $address)
    {
        return [
            'Name' => $address->firstname . ' ' . $address->lastname,
            'AddressLine1' => $address->street_1,
            'AddressLine2' => $address->company,
            'City' => $address->city,
            'Postcode' => $address->postal,
            'Email' => $address->email,
            'Country' => 'DE',
            'Phone' => $address->phone,
            'Status' => 'C',
        ];
    }

    private function createSalesOrderData(Order $order, $billingCustomer, $shippingCustomer)
    {
        $date = new \DateTime('now');

        $orderItems = $order->getItems();

        return [
            'Description' => $billingCustomer['Name'],
            'OrderDate' => $date->format('m/d/Y H:i:s'),
            'OrderedBy' => $billingCustomer['ID'],
            'WarehouseID' => self::WAREHOUSE_ID,
            'SalesOrderLines' => [
                [
                    'Description' => 'TEST: Reinigungstuch grün',
                    'Item' => '9dcd8b50-5de6-4f15-a51a-c51282292383',
                    'UnitPrice' => '2.45',
                    'Quantity' => '3'
                ],
                [
                    'Description' => 'TEST: McQuade´s E-Bike Kettenrückführ - Tool',
                    'Item' => '0e04113f-1299-4d3c-b65e-01e83ca5b3b2',
                    'UnitPrice' => '476.00',
                    'Quantity' => '1'
                ],
            ]
        ];
    }
}