<?php

defined( 'ABSPATH' ) or exit;

final class PWLPAR_Assets {

    function __construct() {
        add_action( 'init', array( $this, 'register_assets' ) );
        add_action( 'init', array( $this, 'add_inline_style' ) );
    }

    function register_assets() {
        wp_register_style( 'pwlpar-header', pwlpar_relative_url( '/assets/css/header.css' ), array(), PWLPAR_VERSION );
        wp_register_style( 'pwlpar-offer', pwlpar_relative_url( '/assets/css/offer.css' ), array(), PWLPAR_VERSION );
        wp_register_style( 'pwlpar-reward', pwlpar_relative_url( '/assets/css/reward.css' ), array(), PWLPAR_VERSION );
        wp_register_style( 'pwlpar-frontend', pwlpar_relative_url( '/assets/css/frontend.css' ), array(), PWLPAR_VERSION );
        wp_register_style( 'pwlpar-admin', pwlpar_relative_url( '/assets/css/admin.css' ), array( 'woocommerce_admin_styles' ), PWLPAR_VERSION );

        wp_register_script( 'pwlpar-offer', pwlpar_relative_url( '/assets/js/offer.js' ), array( 'jquery' ), PWLPAR_VERSION );
        wp_localize_script( 'pwlpar-offer', 'pwlparo', array(
            'ajaxurl'                           => admin_url( 'admin-ajax.php', 'relative' ),
            'i18n' => array(
                'error'                         => __( 'Error', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                'claimingOffer'                 => __( 'Please wait...', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
            ),
            'nonces' => array(
                'claimOffer'                    => wp_create_nonce( 'pwlpar-claim-offer' ),
            )
        ) );
        wp_register_script( 'pwlpar-reward', pwlpar_relative_url( '/assets/js/reward.js' ), array( 'jquery' ), PWLPAR_VERSION );
        wp_register_script( 'pwlpar-frontend', pwlpar_relative_url( '/assets/js/frontend.js' ), array( 'jquery' ), PWLPAR_VERSION );
        wp_localize_script( 'pwlpar-frontend', 'pwlpar', array(
            'ajaxurl'                           => admin_url( 'admin-ajax.php', 'relative' ),
            'i18n' => array(
                'error'                         => __( 'Error', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                'redeemingPoints'               => __( 'Redeeming points...', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
            )
        ) );

        wp_register_script( 'pwlpar-admin', pwlpar_relative_url( '/assets/js/admin/admin.js' ), array( 'jquery', 'wp-color-picker', 'jquery-ui-datepicker', 'jquery-ui-sortable' ), PWLPAR_VERSION );
        wp_localize_script( 'pwlpar-admin', 'pwlpara', array(
            'editPageUrl'                       => admin_url( 'post.php?action=edit&post=' ),
            'wcDecimals'                        => wc_get_price_decimals(),
            'i18n' => array(
                'error'                         => __( 'Error', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                'saving'                        => __( 'Saving...', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                'loading'                       => __( 'Loading...', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                'promptForPoints'               => __( 'Points', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                'promptForPointsAdjustment'     => __( 'Points (can be positive or negative)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                'promptForNote'                 => __( 'Note (optional)', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                'deleteTransaction'             => __( 'Are you sure you want to delete this Transaction?', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                'deleteReward'                  => __( 'Are you sure you want to delete this Reward? The points used to claim this reward will not be refunded.', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                'categoryTitlePrompt'           => __( 'Category name', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                'deleteCategory'                => __( 'Are you sure you want to delete this category?', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                'notAvailableInPreview'         => __( 'Not available while in Preview mode.', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                'deleteOffer'                   => __( 'Are you sure you want to delete this offer?', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                'free'                          => pwlpar_free_label(),
                'navWarning'                    => __( 'The changes you made will be lost if you navigate away from this page.', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                'previewEmailPrompt'            => __( 'Recipient email address?', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                'skipWelcome'                   => __( 'Exit without saving changes?', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
            ),
            'nonces' => array(
                'submitForm'                    => wp_create_nonce( 'pwlpar-submit-form' ),
                'loadMembers'                   => wp_create_nonce( 'pwlpar-load-members' ),
                'adjustBalance'                 => wp_create_nonce( 'pwlpar-adjust-balance' ),
                'viewTransactions'              => wp_create_nonce( 'pwlpar-view-transactions' ),
                'deleteTransaction'             => wp_create_nonce( 'pwlpar-delete-transaction' ),
                'deleteReward'                  => wp_create_nonce( 'pwlpar-delete-reward' ),
                'loadOffers'                    => wp_create_nonce( 'pwlpar-load-offers' ),
                'sortOffers'                    => wp_create_nonce( 'pwlpar-sort-offers' ),
                'deleteOffer'                   => wp_create_nonce( 'pwlpar-delete-offer' ),
                'sendPreviewEmail'              => wp_create_nonce( 'pwlpar-send-preview-email' ),
            )
        ) );

        wp_register_script( 'pwlpar-admin-welcome', pwlpar_relative_url( '/assets/js/admin/welcome.js' ), array( 'pwlpar-admin' ), PWLPAR_VERSION );
        wp_register_script( 'pwlpar-admin-dashboard', pwlpar_relative_url( '/assets/js/admin/dashboard.js' ), array( 'pwlpar-admin' ), PWLPAR_VERSION );
        wp_register_script( 'pwlpar-admin-members', pwlpar_relative_url( '/assets/js/admin/members.js' ), array( 'pwlpar-admin' ), PWLPAR_VERSION );
        wp_register_script( 'pwlpar-admin-offers', pwlpar_relative_url( '/assets/js/admin/offers.js' ), array( 'pwlpar-admin' ), PWLPAR_VERSION );
        wp_register_script( 'pwlpar-admin-settings', pwlpar_relative_url( '/assets/js/admin/settings.js' ), array( 'pwlpar-admin' ), PWLPAR_VERSION );
        wp_register_script( 'pwlpar-admin-help', pwlpar_relative_url( '/assets/js/admin/help.js' ), array( 'pwlpar-admin' ), PWLPAR_VERSION );
    }

    function add_inline_style() {
        wp_add_inline_style( 'pwlpar-frontend', wp_strip_all_tags( pwlpar_get_inline_style() ) );
    }
}

return new PWLPAR_Assets();
