1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 
<?php

/**
 * Copyright (C) Marcelle Hövelmanns, art solution - All Rights Reserved
 *
 * @file        Client.php
 * @author      Marcelle Hövelmanns
 * @site        http://www.artsolution.de
 * @date        02.10.17
 */

namespace AffiliconApiClient;

use AffiliconApiClient\Exceptions\ConfigurationInvalid;
use AffiliconApiClient\Services\ConfigService;
use AffiliconApiClient\Services\HttpService;
use AffiliconApiClient\Services\AuthService;
use AffiliconApiClient\Traits\Singleton;

/**
 * Class Client
 * @package AffiliconApiClient
 */
class Client
{
    /** @var string */
    public $clientId;

    /** @var string */
    public $countryId;

    /** @var string */
    public $userLanguage;

    /** @var string */
    private $secretKey;

    /** @var  AuthService */
    protected $auth;

    /** @var  HttpService */
    protected $http;

    /** @var  ConfigService */
    protected $config;

    /** @var array */
    protected $options;

    /** @var object */
    protected $environment;

    use Singleton;


    /**
     * Initializes the Client
     * @return $this
     */
    public function init()
    {
        if (!$this->environment) {
            $this->setEnv('production');
        }

        $this->http = new HttpService($this->environment->service_url);

        $this->auth = (new AuthService($this))
            ->anonymous()
            ->authenticate();

        return $this;
    }

    /**
     * Sets the environment, default 'production'
     * @param string $env
     * @return $this
     * @throws ConfigurationInvalid
     */
    public function setEnv($env)
    {
        $this->config = new ConfigService();

        $environment = $this->config
            ->get("environment.$env");

        if (!$environment) {
            throw new ConfigurationInvalid("Configuration for given environment not found");
        }

        $this->environment = (object) $environment;

        return $this;
    }

    /**
     * Gets the environment
     * @return object
     */
    public function getEnv()
    {
        return $this->environment;
    }

    public function http()
    {
        return $this->http;
    }

    public function auth()
    {
        return $this->auth;
    }

    public function config()
    {
        return $this->config;
    }

    /**
     * Sets the Client ID, previously called Vendor ID
     * @param string $id
     * @return $this
     */
    public function setClientId($id)
    {
        $this->clientId = $id;
        return $this;
    }

    /**
     * Gets the Client ID, previously called Vendor ID
     * @return string
     */
    public function getClientId()
    {
        return $this->clientId;
    }

    /**
     * Gets the specified country code
     * @return string
     */
    public function getCountryId()
    {
        return $this->countryId;
    }

    /**
     * Sets the country code, eg. "en-US"
     * @param $countryId
     * @return $this
     */
    public function setCountryId($countryId)
    {
        $this->countryId = $countryId;
        return $this;
    }

    /**
     * Gets the specified user language
     * @return string
     */
    public function getUserLanguage()
    {
        return $this->userLanguage;
    }

    /**
     * Sets the user language, eg. "en"
     * @param string $userLanguage
     * @return $this
     */
    public function setUserLanguage($userLanguage)
    {
        $this->userLanguage = $userLanguage;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getToken()
    {
        return $this->auth()->getToken();
    }

    /**
     * Sets the api secret key
     * @param $secretKey
     * @return $this
     */
    public function setSecretKey($secretKey)
    {
        $this->secretKey = $secretKey;
        return $this;
    }

    /**
     * Gets the api secret key
     * @return string
     */
    public function getSecretKey()
    {
        return $this->secretKey;
    }

}