<?php


namespace app\model\services;


use Buzz\Browser;
use MatthiasNoback\MicrosoftOAuth\AccessTokenProvider;
use MatthiasNoback\MicrosoftTranslator\MicrosoftTranslator;

class TranslatorApiService
{
    protected $key;
    protected $secret;

    /** @var  MicrosoftTranslator */
    protected $translator;

    /**
     * BattleNetApiService constructor.
     * @param $key
     * @param $secret
     */
    public function __construct($key, $secret)
    {
        $this->key = $key;
        $this->secret = $secret;
    }

    public function translate($from, $to, $string)
    {
        if (is_array($string)) {
            return $this->getTranslator()->translateArray($string, $to, $from);
        }

        return $this->getTranslator()->translate($string, $to, $from);
    }

    protected function getTranslator()
    {
        if ($this->translator === null) {
            $browser = new Browser();
            $accessTokenProvider = new AccessTokenProvider($browser, $this->key, $this->secret);
            $this->translator = new MicrosoftTranslator($browser, $accessTokenProvider);
        }

        return $this->translator;
    }
}