<?php

namespace GriffinForms\Admin\Html\FormControls\Form;

class AvailabilityWindow extends \GriffinForms\Admin\Html\FormControls\Form\Format
{
    public function html()
    {
        echo '<div class="row">';
        echo '<div class="col-md-12 mb-3" id="griffinforms-toggleavailabilitywindow-wrap">';
        $this->toggleAvailabilityWindow();
        echo '</div>';
        echo '<div class="griffinforms-hidden-option col-md-12 mb-3" id="griffinforms-availabilitywindowstart-wrap">';
        $this->availabilityStart();
        echo '</div>';
        echo '<div class="griffinforms-hidden-option col-md-12 mb-3" id="griffinforms-availabilitywindowend-wrap">';
        $this->availabilityEnd();
        echo '</div>';
        echo '<div class="griffinforms-hidden-option col-md-12 mb-3" id="griffinforms-availabilitywindowpremessage-wrap">';
        $this->availabilityPreMessage();
        echo '</div>';
        echo '<div class="griffinforms-hidden-option col-md-12 mb-3" id="griffinforms-availabilitywindowpostmessage-wrap">';
        $this->availabilityPostMessage();
        echo '</div>';
        echo '</div>';
    }

    protected function toggleAvailabilityWindow()
    {
        $value = isset($this->value['toggle']) ? $this->value['toggle'] : false;
        $data = $this->getControlData(array('label' => $this->lang->getText('toggle_availability_window_label')));
        $field = new \GriffinForms\Admin\Html\FormControls\ToggleCheckbox('toggle_availability_window', $value, $data);
        $field->html();
    }

    protected function availabilityStart()
    {
        $start_date = isset($this->value['start_date']) ? $this->value['start_date'] : '';
        $start_time = isset($this->value['start_time']) ? $this->value['start_time'] : '';
        if ((empty($start_date) || empty($start_time)) && !empty($this->value['start_utc'])) {
            [$start_date, $start_time] = $this->formatUtcToLocal($this->value['start_utc']);
        }

        $date_field = new \GriffinForms\Admin\Html\FormControls\InputDate('availability_window_start_date', $start_date, $this->getControlData());
        $time_field = new \GriffinForms\Admin\Html\FormControls\InputTime('availability_window_start_time', $start_time, $this->getControlData());

        echo '<div class="mb-2">';
        echo esc_html($this->lang->getText('availability_window_start_date_label'));
        echo '</div>';
        echo '<div class="d-flex flex-wrap gap-2">';
        $date_field->html();
        $time_field->html();
        echo '</div>';
    }

    protected function availabilityEnd()
    {
        $end_date = isset($this->value['end_date']) ? $this->value['end_date'] : '';
        $end_time = isset($this->value['end_time']) ? $this->value['end_time'] : '';
        if ((empty($end_date) || empty($end_time)) && !empty($this->value['end_utc'])) {
            [$end_date, $end_time] = $this->formatUtcToLocal($this->value['end_utc']);
        }

        $date_field = new \GriffinForms\Admin\Html\FormControls\InputDate('availability_window_end_date', $end_date, $this->getControlData());
        $time_field = new \GriffinForms\Admin\Html\FormControls\InputTime('availability_window_end_time', $end_time, $this->getControlData());

        echo '<div class="mb-2">';
        echo esc_html($this->lang->getText('availability_window_end_date_label'));
        echo '</div>';
        echo '<div class="d-flex flex-wrap gap-2">';
        $date_field->html();
        $time_field->html();
        echo '</div>';
    }

    protected function availabilityPreMessage()
    {
        $value = isset($this->value['pre_message']) ? $this->value['pre_message'] : '';
        $field = new \GriffinForms\Admin\Html\FormControls\TextArea('availability_window_pre_message', $value, $this->getControlData());
        echo '<div>';
        echo esc_html($this->lang->getText('availability_window_pre_message_label'));
        echo '</div>';
        echo '<div>';
        $field->html();
        echo '</div>';
    }

    protected function availabilityPostMessage()
    {
        $value = isset($this->value['post_message']) ? $this->value['post_message'] : '';
        $field = new \GriffinForms\Admin\Html\FormControls\TextArea('availability_window_post_message', $value, $this->getControlData());
        echo '<div>';
        echo esc_html($this->lang->getText('availability_window_post_message_label'));
        echo '</div>';
        echo '<div>';
        $field->html();
        echo '</div>';
    }

    private function formatUtcToLocal(string $utc_value): array
    {
        $utc_value = trim($utc_value);
        if ($utc_value === '') {
            return ['', ''];
        }

        try {
            $dt = new \DateTimeImmutable($utc_value, new \DateTimeZone('UTC'));
            $local = $dt->setTimezone(wp_timezone());
            return [$local->format('m/d/Y'), $local->format('H:i')];
        } catch (\Exception $e) {
            return ['', ''];
        }
    }
}
