<?php

namespace App\Http\Controllers;

use App\Instagram;
use App\TokenStore\TokenCache;
use Illuminate\Http\Request;
use Microsoft\Graph\Exception\GraphException;
use Microsoft\Graph\Graph;
use Microsoft\Graph\Model;
use Illuminate\Support\Facades\Log;


class InstagramController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $viewData = $this->loadViewData();

        // Get the access token from the cache
        $tokenCache = new TokenCache();
        $accessToken = $tokenCache->getAccessToken();

        // Create a Graph client
        $graph = new Graph();
        $graph->setAccessToken($accessToken);

        $queryParams = array(
            '$select' => 'id,name,size',
        );

        // Append query parameters to the '/me/events' url
//        $getEventsUrl = '/me/drive/items/70D9602035ABE6A9!897/children?'.http_build_query($queryParams);
        $getEventsUrl = '/me/drive/items/70D9602035ABE6A9!9943/children?'.http_build_query($queryParams);
//        $getEventsUrl = '/me/drive';

        try {
            $events = $graph->createRequest('GET', $getEventsUrl)
                ->setReturnType(Model\DriveItem::class)
                ->execute();
        } catch (GraphException $e) {
            var_dump($e);
        }
//        print_r($events);
        foreach ($events as $event) {
//            var_dump($event);exit;
            $subItemUrl =  '/me/drive/items/' . $event->getId(). '/children?'.http_build_query($queryParams);
            $images = $graph->createRequest('GET', $subItemUrl)
//                ->setReturnType(Model\DriveItem::class)
                ->execute();
//            dd($images->getBody());
            $body = $images->getBody();
            $values = $body['value'];
            foreach ($values as $value) {
                Log::debug("current user".$event->getName() ." current image " . $value['name']);
                Instagram::where('image_name', $value['name'])->update(['is_uploaded' => 1]);
            }
            while (array_key_exists('@odata.nextLink', $body)) {
                $nextSubItemUrl = $body['@odata.nextLink'];
                $response = $graph->createRequest('GET', $nextSubItemUrl)
                    ->execute();
                $body = $response->getBody();
                $values = $body['value'];
                foreach ($values as $value) {
                    Log::debug("current user".$event->getName() ." current image " . $value['name']);
                    Instagram::where('image_name', $value['name'])->update(['is_uploaded' => 1]);
                }
            }
//            var_dump($event);
        }

//        $viewData['events'] = $events;
//        return view('calendar', $viewData);
        //
//        return 1;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Instagram  $instagram
     * @return \Illuminate\Http\Response
     */
    public function show(Instagram $instagram)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Instagram  $instagram
     * @return \Illuminate\Http\Response
     */
    public function edit(Instagram $instagram)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Instagram  $instagram
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Instagram $instagram)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Instagram  $instagram
     * @return \Illuminate\Http\Response
     */
    public function destroy(Instagram $instagram)
    {
        //
    }
}
