<?php
namespace BooklyStaffCabinet\Frontend\Modules\Calendar;

use Bookly\Lib as BooklyLib;
use BooklyStaffCabinet\Lib;
use BooklyStaffCabinet\Frontend\Components as FrontendComponents;

/**
 * Class Page
 * @package BooklyStaffCabinet\Frontend\Modules\Calendar
 */
class ShortCode extends BooklyLib\Base\Component
{
    /**
     * Init component.
     */
    public static function init()
    {
        // Register short code.
        add_shortcode( 'bookly-staff-calendar', array( __CLASS__, 'render' ) );

        // Assets.
        if ( get_option( 'bookly_gen_link_assets_method' ) == 'enqueue' ) {
            add_action( 'wp_enqueue_scripts', array( __CLASS__, 'linkStyles' ) );
            add_action( 'wp_enqueue_scripts', array( __CLASS__, 'linkScripts' ) );
        } else {
            add_action( 'wp_print_styles', array( __CLASS__, 'linkStyles' ) );
            add_action( 'wp_print_scripts', array( __CLASS__, 'linkScripts' ) );
        }
    }

    /**
     * Link styles.
     */
    public static function linkStyles()
    {
        if (
            get_option( 'bookly_gen_link_assets_method' ) == 'enqueue' ||
            BooklyLib\Utils\Common::postsHaveShortCode( 'bookly-staff-calendar' )
        ) {
            $bookly_ver = BooklyLib\Plugin::getVersion();
            $bookly_url = plugins_url( '', BooklyLib\Plugin::getMainFile() );
            $cabinet    = plugins_url( '', Lib\Plugin::getMainFile() );

            wp_enqueue_style( 'bookly-bootstrap-theme', $bookly_url . '/backend/resources/bootstrap/css/bootstrap-theme.min.css', array( 'dashicons' ), $bookly_ver );
            wp_enqueue_style( 'bookly-fullcalendar-css', $bookly_url . '/backend/modules/calendar/resources/css/fullcalendar.min.css', array( 'bookly-bootstrap-theme' ), $bookly_ver );
            wp_enqueue_style( 'bookly', $cabinet . '/frontend/resources/css/staff-cabinet.css', array( 'bookly-fullcalendar-css' ), Lib\Plugin::getVersion() );
            wp_enqueue_style( 'bookly-calendar-css', $cabinet . '/frontend/modules/calendar/resources/css/calendar.css', array( 'bookly' ), Lib\Plugin::getVersion() );
        }
    }

    /**
     * Link scripts.
     */
    public static function linkScripts()
    {
        /** @global \WP_Locale $wp_locale */
        global $wp_locale;

        if (
            get_option( 'bookly_gen_link_assets_method' ) == 'enqueue' ||
            BooklyLib\Utils\Common::postsHaveShortCode( 'bookly-staff-calendar' )
        ) {
            $bookly_ver = BooklyLib\Plugin::getVersion();
            $bookly_url = plugins_url( '', BooklyLib\Plugin::getMainFile() );
            $cabinet   = plugins_url( '', Lib\Plugin::getMainFile() );

            wp_enqueue_script( 'bookly-bootstrap', $bookly_url . '/backend/resources/bootstrap/js/bootstrap.min.js', array(), $bookly_ver );
            wp_enqueue_script( 'bookly-moment', $bookly_url . '/backend/resources/js/moment.min.js', array( 'bookly-bootstrap' ), $bookly_ver );
            wp_enqueue_script( 'bookly-calendar-common', $bookly_url . '/backend/modules/calendar/resources/js/calendar-common.js', array( 'jquery', 'bookly-moment' ), $bookly_ver );
            wp_enqueue_script( 'bookly-fullcalendar', $bookly_url . '/backend/modules/calendar/resources/js/fullcalendar.min.js', array( 'bookly-calendar-common', ), $bookly_ver );
            wp_enqueue_script( 'bookly-sc-calendar', $cabinet . '/frontend/modules/calendar/resources/js/calendar.js', array( 'bookly-calendar-common' ), Lib\Plugin::getVersion() );

            $slot_length_minutes = get_option( 'bookly_gen_time_slot_length', '15' );
            $slot                = new \DateInterval( 'PT' . $slot_length_minutes . 'M' );

            wp_localize_script( 'bookly-sc-calendar', 'BooklySCCalendarL10n', array(
                'slotDuration'           => $slot->format( '%H:%I:%S' ),
                'calendar'               => array(
                    'shortMonths' => array_values( $wp_locale->month_abbrev ),
                    'longMonths'  => array_values( $wp_locale->month ),
                    'shortDays'   => array_values( $wp_locale->weekday_abbrev ),
                    'longDays'    => array_values( $wp_locale->weekday ),
                ),
                'dpDateFormat'           => BooklyLib\Utils\DateTime::convertFormat( 'date', BooklyLib\Utils\DateTime::FORMAT_JQUERY_DATEPICKER ),
                'mjsDateFormat'          => BooklyLib\Utils\DateTime::convertFormat( 'date', BooklyLib\Utils\DateTime::FORMAT_MOMENT_JS ),
                'mjsTimeFormat'          => BooklyLib\Utils\DateTime::convertFormat( 'time', BooklyLib\Utils\DateTime::FORMAT_MOMENT_JS ),
                'csrf_token'             => BooklyLib\Utils\Common::getCsrfToken(),
                'today'                  => __( 'Today', 'bookly' ),
                'week'                   => __( 'Week', 'bookly' ),
                'day'                    => __( 'Day', 'bookly' ),
                'month'                  => __( 'Month', 'bookly' ),
                'allDay'                 => __( 'All Day', 'bookly' ),
                'delete'                 => __( 'Delete', 'bookly' ),
                'are_you_sure'           => __( 'Are you sure?', 'bookly' ),
                'startOfWeek'            => (int) get_option( 'start_of_week' ),
                'recurring_appointments' => array(
                    'active' => (int) BooklyLib\Config::recurringAppointmentsActive(),
                    'title'   => __( 'Recurring appointments', 'bookly' ),
                ),
                'waiting_list'           => array(
                    'active' => (int) BooklyLib\Config::waitingListActive(),
                    'title'  => __( 'On waiting list', 'bookly' ),
                ),
                'packages'               => array(
                    'active' => (int) BooklyLib\Config::packagesActive(),
                    'title'  => __( 'Package', 'bookly' ),
                ),
            ) );
        }
    }

    /**
     * Render Calendar shortcode.
     *
     * @param array $attributes
     * @return string
     */
    public static function render( $attributes )
    {
        // Disable caching.
        BooklyLib\Utils\Common::noCache();

        if ( is_user_logged_in() && $staff = BooklyLib\Entities\Staff::query()->select( 'id, visibility' )->where( 'wp_user_id', get_current_user_id() )->fetchRow() ) {
            $custom_fields = array();
            foreach ( (array) BooklyLib\Proxy\CustomFields::getTranslated() as $field ) {
                if ( ! in_array( $field->type, array( 'captcha', 'text-content' ) ) ) {
                    $custom_fields[] = $field;
                }
            }

            \BooklyPro\Backend\Components\License\Components::renderLicenseNotice( false );
            if ( $staff['visibility'] == 'archive' ) {
                return FrontendComponents\Notice\Permission::generateAccountDisabled();
            }
            return \BooklyPro\Lib\Config::graceExpired()
                ? null
                : self::renderTemplate( 'short_code', array( 'staff_id' => $staff['id'], 'custom_fields' => $custom_fields ), false );
        }

        return FrontendComponents\Notice\Permission::generate();
    }
}