<?php

namespace App\Services\ThirdPartyApiServices;

use Exception;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

abstract class BaseApiCall
{
    protected $apiUrl;
    protected $queryData;
    protected $credential;
    protected $contractAddress;

    /**
     * @return mixed
     */
    protected function getApiUrl()
    {
        return $this->apiUrl;
    }

    /**
     * @param $url
     * @return mixed
     */
    protected function setApiUrl($url)
    {
        Log::info($url);

        return $this->apiUrl = $url;
    }

    /**
     * @return mixed
     */
    protected function getQueryData()
    {
        return $this->queryData;
    }

    /**
     * @param $query
     * @param null $data
     * @param null $identifier
     * @return array|null[]
     */
    protected function setQueryData($query, $data = null, $identifier = null)
    {
        if (!is_array($query)) {
            $this->queryData = [$query => $data];
        } else {
            if ($identifier === 'eth') {
                $this->queryData = collect($query)->merge($data)->toArray();
            } else {
                $this->queryData = collect($query)->combine($data)->toArray();
            }
        }
        if ($identifier === 'eth') {
            if ($this->credential) {
                $this->queryData['apiKey'] = $this->getCredential();
            }

            if ($this->contractAddress) {
                $this->queryData['contractaddress'] = $this->getContractAddress();
            }
        } else {
            if ($this->credential) {
                $this->queryData['access_key'] = $this->getCredential();
            }
        }

        return $this->queryData;
    }

    /**
     * @return mixed
     */
    protected function getCredential()
    {
        return $this->credential;
    }

    /**
     * @param $credential
     * @return mixed
     */
    protected function setCredential($credential)
    {
        return $this->credential = $credential;
    }

    /**
     * @return mixed
     */
    protected function getContractAddress()
    {
        return $this->contractAddress;
    }

    /**
     * @param $contractAddress
     * @return mixed
     */
    protected function setContractAddress($contractAddress)
    {
        return $this->contractAddress = $contractAddress;
    }

    /**
     * Api Call
     *
     * @param null $param
     * @param string $method
     * @return array|mixed|void
     */
    protected function callToApi($param = null, string $method = 'get')
    {
        Log::info("new log node");
        Log::info("$this->apiUrl{$param}");
        Log::info("$param");
        Log::info("$this->apiUrl");
        Log::info(app()->environment());
        if (app()->environment() == 'local') {
            try {
                $response = Http::withoutVerifying()
                    ->retry(3);

                if ($method === 'post') {
                    return $response->post("$this->apiUrl{$param}", $this->queryData)->json();
                } else {
                    return $response->get("$this->apiUrl{$param}", $this->queryData)->json();
                }
            } catch (Exception $exception) {
                reportLog($exception);
                return [];
            }
        } elseif (app()->environment() == 'production') {
            Log::info("in else if");
            Log::info($method);
            Log::info("in prod $this->apiUrl{$param}");

            // try {
                $response = Http::retry(3);
                if ($method === 'post') {
                    $res = $response->get("$this->apiUrl{$param}");
                    Log::info($res);
                    Log::info($res->json());
                    return $res->json();

                } else {
                    return $response->get("$this->apiUrl{$param}", $this->queryData)->json();
                }
            // } catch (Exception $exception) {
            //     Log::info("boom");
            //     reportLog($exception);
            //     return [];
            // }
        }
    }
}
