<?php

namespace Tests\Browser\Components;

use Laravel\Dusk\Browser;
use Laravel\Dusk\Component as BaseComponent;

use Carbon\Carbon;

class DateTimePicker extends BaseComponent
{
    public $field;

    public function __construct($field)
    {
        $this->field = $field;
    }

    /**
     * Get the root selector for the component.
     *
     * @return string
     */
    public function selector()
    {
        return '@date-time-picker-'.$this->field;
    }

    /**
     * Assert that the browser page contains the component.
     *
     * @param  Browser  $browser
     * @return void
     */
    public function assert(Browser $browser)
    {
        $browser->assertVisible($this->selector());
    }

    public function selectDateTime(Browser $browser, $date_time)
    {
        // change the timezone to our testing zone as we enter things into the
        // date time picker in the local timezone but it is sent as UTC to the server
        // if we are asserting strings in the browser be sure to set the timezone
        // before parsing the date to a string for things like $browser->assertSeeIn
        $input = Carbon::parse($date_time)->setTimezone('America/Vancouver');

        $browser->click('.id-'.$input->format('Y-m-d'))
            ->with('.vc-time-select-group', function ($browser) use ($input) {
                $browser->select('.vc-base-select:nth-child(2) select', $input->format('G'))
                    ->pause(250)
                    ->select('.vc-base-select:nth-child(4) select', intval($input->format('i')))
                    ->pause(250)
                    ->select('.vc-base-select:nth-child(5) select', $input->format('a') === 'am' ? 'true' : 'false')
                    ->pause(500);
            });
    }
}
