<?php

namespace App\Http\Controllers;

use App\Models\anagrafica;
use App\Models\contatto;
use Illuminate\Http\Request;
use Google\Client;
Use Google\Service\PeopleService;

class AnagraficaController extends Controller
{
    // GOOGLE CONTACTS PART

    // private $google_client;

   /* public function getToken()
    {
        $client = new Client();
        $client->setApplicationName('gestionale.lavorain.cloud');
        if(!is_null($client->getAccessToken())){}
        elseif(!is_null(session('access_token')))   // is_null(Session::get('access_token', null))
        {
            $client->setAccessToken(session('access_token'));
        }
        else
        {
            $scope = array();
            $scope[] = 'https://www.googleapis.com/auth/contacts';
            // $client->
            putenv('GOOGLE_APPLICATION_CREDENTIALS='.storage_path('app/google-calendar/service_key.json')); //this can be created with other ENV mode server side
            $client->useApplicationDefaultCredentials();
            $token = $client->getAccessToken();
            session(['access_token' => $token]);
        }
        return $client;
    }*/

    public function getGoogleContacts()
    {
        $client = $this->getToken();
        $service = new PeopleService($client);
        $profile = $service->people_connections->listPeopleConnections('people/me', array('personFields' => 'names,emailAddresses,phoneNumbers'));
    }
    public function getToken()
    {
         $configJson =storage_path('app/google-calendar/service_key.json');
        // $configJson =storage_path('app/google-calendar/people.json');
        // define an application name
        $applicationName = 'gestionale-di-famiglia';

        // create the client
        $client = new \Google_Client();
        $client->setApplicationName($applicationName);
        $client->setAuthConfig($configJson);
        $client->setAccessType('offline'); // necessary for getting the refresh token
        $client->setApprovalPrompt ('force'); // necessary for getting the refresh token
        // scopes determine what google endpoints we can access. keep it simple for now.
       
        $client->setScopes(
            [
                /*
                    \Google\Service\Oauth2::USERINFO_PROFILE,
                    \Google\Service\Oauth2::USERINFO_EMAIL,
                    \Google\Service\Oauth2::OPENID,
                */
                \Google\Service\PeopleService::CONTACTS // allows reading of google drive metadata
            ]
        );
        $client->setIncludeGrantedScopes(true);
        
        return $client;
    }




    public function newContact()
    {
        return view('anagrafica.form');
    }
    
    public function insContact(Request $request)
    {
        anagrafica::inserisci($request);
        return view('anagrafica.list',['anagrafiche'=>anagrafica::getList()]);
    }
    
    public function schedaContact(Request $request)
    {
        $dati = anagrafica::getById($request['id']);
        return view('anagrafica.scheda',['anagrafiche'=>$dati]);
    }
    
    public function listContact()
    {
        return view('anagrafica.list',['anagrafiche'=>anagrafica::getList()]);
    }
    
    public function modifica(Request $request)
    {    
       return view('anagrafica.form',['anagrafiche'=>anagrafica::getById($request['id'])]);
    }
    
    public function getScheda(Request $request)
    {
        $id=$request['id'];
        $anagrafica = anagrafica::getById($id);
        $contatto=contatto::listContactsById($id);
        return view('anagrafica.dettagli',['anagrafiche'=>$anagrafica,'contatti'=>$contatto['contatti'],'tipo'=>$contatto['tipo']]);
        
    }
    
    public function insOtherContact(Request $param){
        $contatto=contatto::listContactsById($param['id']);
        return view('anagrafica.altrocontatto',['id'=>$param['id'],'tipo'=>$contatto['tipo']]);
    }
    
    public function saveOtherContact(Request $param){
        // return view('anagrafica.altrocontatto',[id=>$param['id']]);
        contatto::saveNewContact($param);
        $contatto=contatto::listContactsById($param['cnt_fk_anagraficaId']);
        if($param['another'])
        {
            return view('anagrafica.altrocontatto',['tipo'=>$contatto['tipo'],'id'=>$param['cnt_fk_anagraficaId']]);
        }else {
            return redirect(Route('contatti'));
        }
    }
    
    
    
}
