<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;

use App\Utilities\CalendarEvent;

use App\Models\GoogleCalendar;

use Carbon\Carbon;

use Google_Client;
use Google_Service_Calendar;
use Google_Service_Calendar_Event;

class CalendarEventTest extends TestCase
{
    use WithFaker;

    public function test_a_google_calendar_event_can_be_converted_into_a_calendar_event()
    {
        $google_calendar = GoogleCalendar::factory()->create();

        $client = new Google_Client();
        $client->setScopes(Google_Service_Calendar::CALENDAR_READONLY);
        $client->setAuthConfig(base_path('sa/calendar.json'));
        $client->setSubject('sa_developer@brentwood.ca');

        $service = new Google_Service_Calendar($client);

        // https://developers.google.com/calendar/v3/reference/events/list

        $optParams = [
          //'maxResults' => 10, // the default is 250
          'orderBy' => 'startTime',
          'singleEvents' => true,
          'timeMin' => date('c'), // ISO date for now()
        ];

        $results = $service->events->listEvents($google_calendar->google_id, $optParams);
        $events = collect($results->getItems());

        $this->assertInstanceOf(Google_Service_Calendar_Event::class, $events->random());

        $color = $this->faker->hexColor();
        $calendar_events = CalendarEvent::convertGoogleCalendarEvents($events, $color);

        $calendar_event = $calendar_events->random();
        $this->assertInstanceOf(CalendarEvent::class, $calendar_event);

        $this->assertNotEquals($calendar_event->start_date->toIso8601ZuluString(), $calendar_event->end_date->toIso8601ZuluString());
        $this->assertInstanceOf(Carbon::class, $calendar_event->start_date);
        $this->assertInstanceOf(Carbon::class, $calendar_event->end_date);
        $this->assertEquals($color, $calendar_event->color);
    }

    public function a_google_calendar_description_can_be_cleaned()
    {
        $html = '<h4><span style="font-weight: normal;">Calling all young alumni, young at heart, and starving students in Victoria! Join Director of University Counselling, Rick Rodrigues, and Advancement Associate, Alumni Relations Lauren Yanick \'16 for a night out in the Sam McGee Room (upstairs) at the Bard &amp; Banker Pub. Drinks and appies are on us! Remember to bring 2 pieces of ID. Sorry, NO MINORS.</span></h4><h4>St Patricks Day Pub Night:</h4><u></u><h4><span style="font-weight: normal;">Thursday, March 16 | 6.00 pm – 9.00 pm<br>Reservation: "Brentwood College School"<br>Location: Bard &amp; Banker Pub (Sam McGee Room) | 1022 Government St, Victoria, BC</span></h4><u></u><p></p><h1><p></p><span style="font-weight: normal;"><u></u><u></u></span><p></p></h1><p></p><h4><b>REGISTRATION <a href="https://www.brentwood.bc.ca/index.php?id=750">HERE</a></b></h4>';

        $cleaned = CalendarEvent::cleanDescription($html);

        $this->assertEquals('<p>Calling all young alumni, young at heart, and starving students in Victoria! Join Director of University Counselling, Rick Rodrigues, and Advancement Associate, Alumni Relations Lauren Yanick \'16 for a night out in the Sam McGee Room (upstairs) at the Bard &amp; Banker Pub. Drinks and appies are on us! Remember to bring 2 pieces of ID. Sorry, NO MINORS.St Patricks Day Pub Night:Thursday, March 16 | 6.00 pm &ndash; 9.00 pm<br>Reservation: "Brentwood College School"<br>Location: Bard &amp; Banker Pub (Sam McGee Room) | 1022 Government St, Victoria, BC</p><p></p><p></p><p></p><p></p>REGISTRATION <a href="https://www.brentwood.bc.ca/index.php?id=750">HERE</a>', $cleaned);
    }
}
