<?php

namespace Modules\TPConnector\Observers;

use App\Transaction;
use Exception;
use Illuminate\Support\Facades\Broadcast;
use Modules\TPConnector\Events\TransactionCreatedEvent;
use Modules\TPConnector\Events\TransactionDeletedEvent;
use Modules\TPConnector\Events\TransactionUpdatedEvent;
use Spatie\Activitylog\Models\Activity;

class ActivityObserver
{
    /**
     * Handle the transaction "created" event.
     *
     * @param Activity $activity
     * @return void
     * @noinspection SpellCheckingInspection
     */
    public function created(Activity $activity)
    {
        if (
            $activity->subject_type === Transaction::class &&
            in_array($activity->description, ['edited', 'shipping_edited', 'sell_deleted', 'added'])
        ) {
            /** @var Transaction $transaction */
            $transaction = Transaction::query()->with([
                'table', 'tax', 'sell_lines', 'sell_lines.line_tax', 'sell_lines.product',
                'sell_lines.product', 'sell_lines.variations',
                'sell_lines.product.category', 'sell_lines.product.sub_category',
                'payment_lines'
            ])->find($activity->subject_id);
            if (!is_null($transaction) && $transaction->status === 'final') {
                try {
                    config([
                        'broadcasting' => [
                            'connections' => [
                                'pusher-server' => [
                                    'driver' => 'pusher',
                                    'app_id' => env('TPCONNECTOR_SOCKET_APP_ID'),
                                    'key' => env('TPCONNECTOR_SOCKET_APP_KEY'),
                                    'secret' => env('TPCONNECTOR_SOCKET_APP_SECRET'),
                                    'options' => [
                                        'timeout' => 2,
                                        'encrypted' => true,
                                        'host' => '167.71.128.112',
                                        'port' => 80,
                                        'scheme' => 'http'
                                    ],
                                ]
                            ]
                        ]
                    ]);
                    Broadcast::setDefaultDriver('pusher-server');
                    Broadcast::connection('pusher-server');
                    if ($activity->description === 'added')
                        broadcast(new TransactionCreatedEvent($transaction));
                    if ($activity->description === 'sell_deleted' || ($activity->description === 'edited' &&
                            $transaction->sell_lines->count() === 1 &&
                            $transaction->sell_lines->filter(function ($sell) {
                                return $sell->product->sku === '00000';
                            })->count() > 0))
                        broadcast(new TransactionDeletedEvent($transaction));
                    else if ($activity->description === 'edited' || $activity->description === 'shipping_edited')
                        broadcast(new TransactionUpdatedEvent($transaction));
                } catch (Exception $exception) {
                }
            }
        }
    }
}
