<?php

//disable warnings but enable errors
error_reporting(E_ALL ^ E_WARNING);

use Foodomaa\ThermalPrinter\FoodomaaThermalPrinter;

require_once __DIR__ . '/vendor/autoload.php';

try {

    echo "\n", '> Servidor de impressão Thru Delivery iniciado...', "\n\n";

    $websocket = new Hoa\Websocket\Server(
        new Hoa\Socket\Server('ws://127.0.0.1:6441')
    );

    $websocket->on('open', function (Hoa\Event\Bucket $bucket) {
        echo '> Connection Successful.', "\n\n";
        return;
    });

    $websocket->on('message', function (Hoa\Event\Bucket $bucket) {

        $data = $bucket->getData();
        $printData = json_decode($data['message']);

        if (!isset($printData->data->printerData->connector_descriptor) || empty($printData->data->printerData->connector_descriptor)) {
            echo '> Incorrect Printer Configs...', "\n\n";
            return;

        } else {

            $connector_type = $printData->data->printerData->connector_type;
            $connector_descriptor = $printData->data->printerData->connector_descriptor;
            try {

                $printer = new FoodomaaThermalPrinter();
                $printer->load($connector_type, $connector_descriptor);
                $printer->printInvoice($printData->data);

                if ($printData->data->print_type == null) {

                    //if null then print for both (depening on if KOT is enabled or not)
                    echo '---------------------------------------------------------', "\n";
                    echo '| Invoice Printed for Order ID: ' . $printData->data->order->unique_order_id . ' |', "\n";

                    if (empty($printData->data->printerData->print_kot)) {
                        echo '---------------------------------------------------------', "\n\n";
                    } else {
                        echo '---------------------------------------------------------', "\n";
                        echo '| KOT Printed for Order ID: ' . $printData->data->order->unique_order_id . ' |', "\n";
                        echo '-----------------------------------------------------', "\n\n";
                    }

                } elseif ($printData->data->print_type == 'invoice') {

                    echo '---------------------------------------------------------', "\n";
                    echo '| Invoice Printed for Order ID: ' . $printData->data->order->unique_order_id . ' |', "\n";
                    echo '---------------------------------------------------------', "\n\n";

                } elseif ($printData->data->print_type == 'kot') {

                    echo '-----------------------------------------------------', "\n";
                    echo '| KOT Printed for Order ID: ' . $printData->data->order->unique_order_id . ' |', "\n";
                    echo '-----------------------------------------------------', "\n\n";

                }

            } catch (Exception $e) {
                $bucket->getSource()->send($e->getMessage());
                echo '> Error occurred, unable to print. ', $e->getMessage(), "\n\n";
            }
            return;
        }
        return;

    });

    $websocket->on('close', function (Hoa\Event\Bucket $bucket) {
        echo '> Disconnected.', "\n\n";
        return;
    });

    try {
        echo '> Server Started.', "\n\n";
        $websocket->run();
    } catch (Exception $e) {
        echo '> Something went wrong! ', $e->getMessage(), "\n\n";
    }

} catch (Exception $e) {
    echo '> Error: ', $e->getMessage(), "\n\n";
}
