<?php

namespace WeDevs\AppBridge\Integrations\WooCommerce;

use WeDevs\AppBridge\Core\Contracts\Hookable;
use WP_HTTP_Response;

/**
* Category extended class
*/
class Category implements Hookable {

    /**
     * Load automatically when class initiate
     *
     * @since 1.0.0
     */
    public function register_hooks(): void {
        add_action( 'product_cat_add_form_fields', array( $this, 'add_field' ), 10, 1 );
        add_action( 'product_cat_edit_form_fields', array( $this, 'edit_field' ), 10, 1 );
        add_action( 'edited_product_cat', array( $this, 'save_category_meta' ), 10, 1 );
        add_action( 'create_product_cat', array( $this, 'save_category_meta' ), 10, 1 );

        add_action( 'admin_enqueue_scripts', array( $this, 'category_enqueue' ) );
        add_action( 'admin_footer', array( $this, 'icon_template' ) );

        // For rest api
        add_filter( 'woocommerce_rest_prepare_product_cat', array( $this, 'prepare_product_cat_response' ), 12, 3 );
    }

    /**
     * Enqueue category scripts
     *
     * @param $taxonomy
     *
     * @since 1.0.0
     *
     * @return void
     */
    public function category_enqueue( $taxonomy ) {
        if ( null !== ( $screen = get_current_screen() ) && 'edit-product_cat' !== $screen->id ) { // phpcs:ignore
            return;
        }

        wp_enqueue_style( 'wp-color-picker' );
        wp_enqueue_script( 'wp-color-picker' );
        wp_enqueue_style( 'dokan-iconmoon-css', APP_BRIDGE_ASSETS . '/css/icon.css', null, APP_BRIDGE_VERSION );
        wp_enqueue_script(
            'dokan-app-admin',
            APP_BRIDGE_ASSETS . '/js/dokan-app.js',
            array( 'jquery', 'wp-util', 'underscore', 'backbone', 'wc-backbone-modal' ),
            APP_BRIDGE_VERSION,
            true
        );

        $localize_array = array( 'js_path' => APP_BRIDGE_ASSETS . '/js' );
        wp_localize_script( 'dokan-app-admin', 'dokanApp', $localize_array );
    }

    /**
     * Init scripts in category page
     *
     * @since 1.0.0
     *
     * @return void
     */
    public function init_inline_scripts() {
        if ( null !== ( $screen = get_current_screen() ) && 'edit-product_cat' !== $screen->id ) { // phpcs:ignore
            return;
        }
        ?>
        <script>
            jQuery( document ).ready( function($) {
                $( '.colorpicker' ).wpColorPicker();
            } );
        </script>
        <?php
    }

    /**
     * Add fields for category page
     *
     * @since 1.0.0
     *
     * @return void
     */
    public function add_field() {
        ?>
        <div class="form-field">
            <label for="dokan_cat_icon"><?php esc_html_e( 'Icon', 'app-bridge' ); ?></label>
            <input type="text" name="dokan_cat_icon" id="dokan-cat-icon">
            <p class="description"><?php esc_html_e( 'Set your icon for category', 'app-bridge' ); ?></p>
        </div>
        <div class="form-field">
            <label for="wh_meta_desc"><?php esc_html_e( 'Icon Color', 'app-bridge' ); ?></label>
            <input name="dokan_cat_icon_color" value="#ffffff" class="colorpicker" id="dokan-cat-icon-color" />
            <p class="description"><?php esc_html_e( 'Set your icon color', 'app-bridge' ); ?></p>
        </div>
        <?php
    }

    /**
     * Edit category extra fields
     *
     * @param $term
     *
     * @since 1.0.0
     *
     * @return void
     */
    public function edit_field( $term ) {
        $term_id        = $term->term_id;
        $cat_icon       = get_term_meta( $term_id, 'dokan_cat_icon', true );
        $cat_icon_color = get_term_meta( $term_id, 'dokan_cat_icon_color', true );
        ?>
        <tr class="form-field">
            <th scope="row" valign="top"><label for="dokan-cat-icon"><?php esc_html_e( 'Icon', 'app-bridge' ); ?></label></th>
            <td>
                <input type="text" name="dokan_cat_icon" id="dokan-cat-icon" value="<?php echo esc_attr( $cat_icon ) ? esc_attr( $cat_icon ) : ''; ?>">
                <p class="description"><?php esc_html_e( 'Set your icon for category', 'app-bridge' ); ?></p>
            </td>
        </tr>
        <tr class="form-field">
            <th scope="row" valign="top"><label for="dokan-cat-icon-color"><?php esc_html_e( 'Icon Color', 'app-bridge' ); ?></label></th>
            <td>
                <input name="dokan_cat_icon_color" class="colorpicker" id="dokan-cat-icon-color" value="<?php echo esc_attr( $cat_icon_color ) ? esc_attr( $cat_icon_color ) : ''; ?>"/>
                <p class="description"><?php esc_html_e( 'Set your icon color', 'app-bridge' ); ?></p>
            </td>
        </tr>
        <?php
    }

    /**
     * Save category metas
     *
     * @param integer $term_id
     *
     * @return void
     */
    public function save_category_meta( $term_id ) {
        $cat_icon       = ! empty( $_POST['dokan_cat_icon'] ) ? sanitize_text_field( $_POST['dokan_cat_icon'] ) : ''; // phpcs:ignore
        $cat_icon_color = ! empty( $_POST['dokan_cat_icon_color'] ) ? sanitize_text_field( $_POST['dokan_cat_icon_color'] ) : ''; // phpcs:ignore

        update_term_meta( $term_id, 'dokan_cat_icon', $cat_icon );
        update_term_meta( $term_id, 'dokan_cat_icon_color', $cat_icon_color );
    }

    /**
     * Icon template
     *
     * @since 1.0.0
     *
     * @return void
     */
    public function icon_template() {
        if ( null !== ( $screen = get_current_screen() ) && 'edit-product_cat' !== $screen->id ) { // phpcs:ignore
            return;
        }
        $this->init_inline_scripts();
        ?>
        <script type="text/template" id="tmpl-wc-modal-iconmoon">
            <div class="wc-backbone-modal wc-cat-icon-preview">
                <div class="wc-backbone-modal-content">
                    <section class="wc-backbone-modal-main" role="main">
                        <header class="wc-backbone-modal-header">
                            <h1>Icon list</h1>
                            <button class="modal-close modal-close-link dashicons dashicons-no-alt">
                                <span class="screen-reader-text"><?php esc_html_e( 'Close modal panel', 'app-bridge' ); ?></span>
                            </button>
                        </header>
                        <article style="padding:20px">
                            <ul class="dokan-cat-icon-list">
                                <# _.each( data.icons, function(icon) { #>
                                    <li data-icon-value="icon-{{ icon }}">
                                        <span class="icon-{{ icon }}"></span>
                                    </li>
                                <# }); #>
                            </ul>
                        </article>
                        <footer>
                            <div class="inner">
                                lorem test
                            </div>
                        </footer>
                    </section>
                </div>
            </div>
            <div class="wc-backbone-modal-backdrop modal-close"></div>
            <style>
                ul.dokan-cat-icon-list {
                    list-style:none;
                }

                ul.dokan-cat-icon-list li {
                    display: inline-block;
                    font-size: 25px;
                    padding: 10px;
                    cursor: pointer;
                }

                ul.dokan-cat-icon-list li:hover {
                    color: #0073aa;
                }

                ul.dokan-cat-icon-list li span{
                    display:block;
                }

                ul.dokan-cat-icon-list li span.name{
                    font-size: 10px;
                }
            </style>
        </script>
        <?php
    }

    /**
     * Added extra field in product cat rest api
     *
     * @param $response
     * @param $item
     * @param $request
     *
     * @since 1.0.0
     *
     * @return WP_HTTP_Response
     */
    public function prepare_product_cat_response( $response, $item, $request ) {
        $cat_icon       = get_term_meta( $item->term_id, 'dokan_cat_icon', true );
        $cat_icon_color = get_term_meta( $item->term_id, 'dokan_cat_icon_color', true );

        $data = $response->get_data();

        $data['icon'] = $cat_icon;
        $data['icon_color'] = $cat_icon_color;

        $response->set_data( $data );

        return $response;
    }
}
