<?php

namespace Modules\Bitrix\Models;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Modules\Bitrix\Enums\BitrixWebhooksEnum;
use Modules\Bitrix\Models\BitrixWebhooks;

class BitrixSender
{
    protected $callbackUrl = false;
    protected $data;
    public $resultData;
    public function __construct()
    {
        $this->data = [];
    }
    public function setCallbackUrl($callbackUrl)
    {
        $this->callbackUrl = $callbackUrl;
    }

    public function addDataItem($key, $value) {
        $this->data[$key] = $value;
    }
    protected function getUrl()
    {
        $webhookEnumItem = false;
        $childClass = get_class($this);
        switch ( $childClass )
        {
            case SendAgent::class:
                $webhookEnumItem = BitrixWebhooksEnum::CREATE_AGENT;
                break;
            case SendClient::class:
                $webhookEnumItem = BitrixWebhooksEnum::CREATE_CONTACT;
                break;
            case SendCompany::class:
                $webhookEnumItem = BitrixWebhooksEnum::CREATE_COMPANY;
                break;
            case SendDeal::class:
                $webhookEnumItem = BitrixWebhooksEnum::CREATE_DEAL;
                break;
            case SendDealAgent::class:
                $webhookEnumItem = BitrixWebhooksEnum::ADD_CLIENT_TO_DEAL;
                break;
            case SendDealClient::class:
                $webhookEnumItem = BitrixWebhooksEnum::ADD_CLIENT_TO_DEAL;
                break;
        }
        if ($webhookEnumItem)
        {
            return BitrixWebhooks::getUrlByName($webhookEnumItem);
        }
        else
        {
            throw new \Exception('Undefined url of webhook for ' . $childClass . ' class');
        }
    }
    public function send()
    {
        if ($this->callbackUrl)
        {
            $this->data['callbackUrl'] = $this->callbackUrl;
        }
        $postdata = http_build_query(
            $this->data
        );
        $opts = array(
            'ssl'  => array(
                'verify_peer'     => true,
                'verify_peername' => true
            ),
            'http' => array(
                'method'  => 'POST',
                'header'  =>
                    'Content-type: application/x-www-form-urlencoded' . "\r\n" .
                    '',
                'content' => $postdata
            )
        );
        try
        {
            $context = stream_context_create($opts);
            $result = file_get_contents($this->getUrl(), false, $context);
            $result = json_decode($result, $associative = true);
            $this->resultData = $result;
            //dd($result);
            if (is_array($this->resultData) && array_key_exists('result', $result))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (\Exception $e)
        {
            Log::build([
                'driver' => 'single',
                'path'   => storage_path('logs/bitrix.log'),
            ])->error($e->getMessage());
            return false;
        }
    }
}

