<?php

namespace App\Bundle\Calltouch;

use App\Bundle\Infoclinica\Region;
use App\Bundle\Infoclinica\Client;

class Request
{
    private $client;
    private $clientApiId;

    public function __construct()
    {
        $this->client = Client::init('https://api.calltouch.ru/');

        $this->changeRegion(Region::getId());
    }

    public function changeRegion($regionId)
    {
    	switch ($regionId) {
        	case '92':
        		$this->clientApiId = 'iaCkzrxUYemoqAactN83KsOUPP6IU4QO89Q9XQHwjKZsw';
        		$this->clientSiteId = '17285';
        		$this->client
        			->setHeader('Access-Token', $this->clientApiId)
        			->setHeader('SiteId', $this->clientSiteId);
        		return $this;
        		
				// Волгоград
				// volgograd.sovamed.ru

				// site_id - 17285
				// mod_id - 886a1412
				// ID личного кабинета 17285
				// Токен iaCkzrxUYemoqAactN83KsOUPP6IU4QO89Q9XQHwjKZsw
				// ID счетчика (mod_id)  886a1412

        		break;
        	
        	case '93':
        		$this->clientApiId = 'mNgA1yKdoJD2ZESfX2v4gFB/6mrgGnIOsbs7ICR7cuXC6';	
        		$this->clientSiteId = '16650';
        		$this->client
        			->setHeader('Access-Token', $this->clientApiId)
        			->setHeader('SiteId', $this->clientSiteId);
        		return $this;

				// Воронеж
				// voronezh.sovamed.ru:
				// site_id - 16650
				// mod_id - ce64c813
				// ID личного кабинета 16650
				// Токен mNgA1yKdoJD2ZESfX2v4gFB/6mrgGnIOsbs7ICR7cuXC6
				// ID счетчика (mod_id)  ce64c813
        		break;
        	
        	case '94':
				$this->clientApiId = 'WAh7BnATs/6KBcsblRiUijb.U0lmiUX.b6yL7TR7GSVps';
				$this->clientSiteId = '41767';
				$this->client
        			->setHeader('Access-Token', $this->clientApiId)
        			->setHeader('SiteId', $this->clientSiteId);
				return $this;

				// Краснодар
				// wmtmed.ru
				// site_id - 41767
				// mod_id - yde3clnb
				// ID личного кабинета 41767
				// Токен WAh7BnATs/6KBcsblRiUijb.U0lmiUX.b6yL7TR7GSVps
				// ID счетчика (mod_id)  yde3clnb
        		break;
        	
        	case '95':
				$this->clientApiId = 'yJbKffJdPo0nt0aG5GdrEeZB8g90XWAOjbnB9PZ8qUHBq';
				$this->clientSiteId = '52294';
				$this->client
        			->setHeader('Access-Token', $this->clientApiId)
        			->setHeader('SiteId', $this->clientSiteId);
				return $this;

				// Саратов
				// sovenok.sovamed.ru
				// site_id - 52294
				// mod_id - e9bu10ds
				// ID личного кабинета 52294
				// Токен yJbKffJdPo0nt0aG5GdrEeZB8g90XWAOjbnB9PZ8qUHBq
				// ID счетчика (mod_id)  e9bu10ds
        		break;

        	case '96':
				$this->clientApiId = 'ekz0aK2iVJHNpbupQmWUYiIGZi9Be9ReHpBAXktxeuRPm';
				$this->clientSiteId = '43907';
				$this->client
        			->setHeader('Access-Token', $this->clientApiId)
        			->setHeader('SiteId', $this->clientSiteId);
				return $this;

				// Комфорт
				// comfort.sovamed.ru
				// ID личного кабинета (site_id)
				// 43907
				// ID счетчика (mod_id)
				// 4p0md55a
				// API токен (access_token)
				// ekz0aK2iVJHNpbupQmWUYiIGZi9Be9ReHpBAXktxeuRPm
	     		break;

        	default:
				$this->clientApiId = 'eMpN4lhkNVQ/js63VpbUh/fIjcCxooEuGh2pOYictz5ZO';
				$this->clientSiteId = '16205';
        		$this->client
        			->setHeader('Access-Token', $this->clientApiId)
        			->setHeader('SiteId', $this->clientSiteId);
        		return $this;
				
				// Саратов
				// sovamed.ru:
				// site_id - 16205
				// mod_id - 95271cfe
				// ID личного кабинета 16205
				// Токен eMpN4lhkNVQ/js63VpbUh/fIjcCxooEuGh2pOYictz5ZO
				// ID счетчика (mod_id)  95271cfe
        		break;
        }
    }

    public function create($requests)
    {
    	$data = $this->client->setBodyParams(['requests' => [$requests]])
            ->setPath('/lead-service/v1/api/request/create')
            ->exec()
            ->dataResponse;

        return $data;
	}


	public function selectcCalls($params = [])
    {
    	$path = '/calls-service/RestAPI/'. $this->clientSiteId .'/calls-diary/calls';
		$path .= '?clientApiId=' . $this->clientApiId;
		$path .= '&dateFrom=' . $params['dateFrom'];
		$path .= '&dateTo=' . $params['dateTo'];
		$path .= '&withCallTags=true';
		
		if (isset($params['withMapVisits'])) {
			$path .= '&withMapVisits=' . $params['withMapVisits'];
		}

		if (isset($params['withYandexDirect'])) {
			$path .= '&withYandexDirect=' . $params['withYandexDirect'];
		}

		$path .= '&page=1&limit=500';
    	$data = $this->client
            ->clearParams()
            ->setPath($path)
            ->exec()->dataResponse;

        return $data;
	}

	public function selectRequests($params = [])
    {
    	$path = '/calls-service/RestAPI/requests/';
		$path .= '?clientApiId=' . $this->clientApiId;
		$path .= '&dateFrom=' . $params['dateFrom'];
		$path .= '&dateTo=' . $params['dateTo'];

		if (isset($params['withMapVisits'])) {
			$path .= '&withMapVisits=' . $params['withMapVisits'];
		}

		if (isset($params['withYandexDirect'])) {
			$path .= '&withYandexDirect=' . $params['withYandexDirect'];
		}

    	$data = $this->client
            ->clearParams()
            ->setPath($path)
            ->exec()->dataResponse;

        return $data;
	}	
}
