<?php
use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface;

class Epaka_Public_Blocks_Integration implements IntegrationInterface{
    /**
     * The name of the integration.
     *
     * @return string
     */
    public function get_name() {
        return 'epaka-pl';
    }

    /**
     * When called invokes any initialization/setup for the integration.
     */
    public function initialize() {
        $this->register_epaka_map_input_block_frontend_scripts();
        $this->register_epaka_map_input_block_editor_scripts();
        $this->register_epaka_map_input_block_editor_styles();
//        $this->register_main_integration();
    }

    /**
     * Registers the main JS file required to add filters and Slot/Fills.
     */
    public function register_main_integration() {
        $script_path = '/blocks/build/index.js';
        $style_path  = '/blocks/build/style-index.css';

        $script_url = plugins_url( $script_path, __FILE__ );
        $style_url  = plugins_url( $style_path, __FILE__ );

        $script_asset_path = dirname(__FILE__) . '/blocks/build/index.asset.php';
        $script_asset      = file_exists( $script_asset_path )
            ? require $script_asset_path
            : array(
                'dependencies' => array(),
                'version'      => $this->get_file_version( $script_path ),
            );

        wp_enqueue_style(
            'epaka-pl-blocks-integration',
            $style_url,
            [],
            $this->get_file_version( $style_path )
        );

        wp_register_script(
            'epaka-pl-blocks-integration',
            $script_url,
            $script_asset['dependencies'],
            $script_asset['version'],
            true
        );

        wp_set_script_translations(
            'epaka-pl-blocks-integration',
            'epaka-pl',
            dirname(__FILE__) . '/languages'
        );
    }

    /**
     * Returns an array of script handles to enqueue in the frontend context.
     *
     * @return string[]
     */
    public function get_script_handles() {
        return array( 'epaka-pl-map-input-block-frontend' );
    }

    /**
     * Returns an array of script handles to enqueue in the editor context.
     *
     * @return string[]
     */
    public function get_editor_script_handles() {
        return array( 'epaka-pl-map-input-block-editor' );
    }

    /**
     * An array of key, value pairs of data made available to the block on the client side.
     *
     * @return array
     */
    public function get_script_data() {
        $data = array(
            'epaka-pl-active' => true,
            'epaka-shipping-courier-mapping' => json_decode(get_option('epakaShippingCourierMapping'),true),
        );

        return $data;
    }

    public function register_epaka_map_input_block_editor_styles(){
        $style_path  = '/blocks/build/epaka-pl-map-input-block.css';

        $style_url  = plugins_url( $style_path, __FILE__ );
        wp_enqueue_style(
            'epaka-pl-map-input-block',
            $style_url,
            [],
            $this->get_file_version( $style_path )
        );
    }

    public function register_epaka_map_input_block_editor_scripts(){
        $script_path       = '/blocks/build/epaka-pl-map-input-block.js';
        $script_url        = plugins_url( $script_path, __FILE__ );
        $script_asset_path = dirname( __FILE__ ) . '/blocks/build/epaka-pl-map-input-block.asset.php';
        $script_asset      = file_exists( $script_asset_path )
            ? require $script_asset_path
            : array(
                'dependencies' => array(),
                'version'      => $this->get_file_version( $script_asset_path ),
            );

        wp_register_script(
            'epaka-pl-map-input-block-editor',
            $script_url,
            $script_asset['dependencies'],
            $script_asset['version'],
            true
        );

        wp_set_script_translations(
            'epaka-pl-map-input-block-editor', // script handle
            'epaka-pl', // text domain
            dirname( __FILE__ ) . '/languages'
        );
    }

    public function register_epaka_map_input_block_frontend_scripts(){
        $script_path       = '/blocks/build/epaka-pl-map-input-block-frontend.js';
        $style_path       = '/blocks/build/epaka-pl-map-input-block-frontend.css';
        $style_url  = plugins_url( $style_path, __FILE__ );
        $script_url        = plugins_url( $script_path, __FILE__ );
        $script_asset_path = dirname( __FILE__ ) . '/blocks/build/epaka-pl-map-input-block-frontend.asset.php';
        $script_asset      = file_exists( $script_asset_path )
            ? require $script_asset_path
            : array(
                'dependencies' => array(),
                'version'      => $this->get_file_version( $script_asset_path ),
            );

        $style_path  = '/blocks/build/epaka-pl-map-input-block.css';

        wp_register_script(
            'epaka-pl-map-input-block-frontend',
            $script_url,
            $script_asset['dependencies'],
            $script_asset['version'],
            true
        );
        wp_set_script_translations(
            'epaka-pl-map-input-block-frontend', // script handle
            'epaka-pl', // text domain
            dirname( __FILE__ ) . '/languages'
        );
        wp_enqueue_style(
            'epaka-pl-map-input-block-frontend',
            $style_url,
            [],
            $this->get_file_version( $style_path )
        );
    }

    /**
     * Get the file modified time as a cache buster if we're in dev mode.
     *
     * @param string $file Local path to the file.
     * @return string The cache buster value to use for the given file.
     */
    protected function get_file_version( $file ) {
        if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $file ) ) {
            return filemtime( $file );
        }
        return EPAKA_VERSION;
    }

}