<?php

namespace Rtc\PlantShopWC\Admin\Controllers;

use Rtc\PlantShopWC\Admin\Models\ProductModel;
use Rtc\PlantShopWC\Admin\Views\ProductView;
use WP_Post;

class ProductController {


    protected $model;
    protected $view;

    public function __construct() {
        $this->model = new ProductModel();
        $this->view  = new ProductView();
    }

    public function register_hooks(): void {

        add_action( 'add_meta_boxes', array( $this, 'plantshop_wc_add_meta_box' ) );
        add_action( 'save_post_product', array( $this, 'plantshop_wc_save_meta_box_data' ) );
        add_action( 'admin_enqueue_scripts', array( $this, 'plantshop_wc_meta_box_enqueue' ) );
        add_action( 'admin_enqueue_scripts', array( $this, 'plantshop_wc_product_js_hooks' ) );
    }

    public function plantshop_wc_product_js_hooks() {
        wp_enqueue_script( 'plantshop-wc-product-js-hooks', PLANTSHOP_WC_PUBLIC_URL . 'admin/js/plantshop-wc-hooks.js', array(), PLANTSHOP_WC_PLUGIN_VERSION, true );
        wp_add_inline_script( 'plantshop-wc-product-js-hooks', 'console.log("plantshop-wc-product-js-hooks");' );
    }

    public function plantshop_wc_add_meta_box(): void {

        global $post;
        $tabs = $this->model->get_tabs( $post->ID );

        if ( empty( $post ) || empty( $tabs ) ) {
            return;
        }

        $logo = '<img id="plantshop-wc_meta_box_logo" src="' . esc_url( PLANTSHOP_WC_LOGO_URL ) . '" alt="Logo" />';

        add_meta_box(
            'plantshop-wc_meta_box',
            $logo . ' <span>' . __( 'Plant data', 'plantshop-wc' ) . '</span>',
            array( $this, 'plantshop_wc_render_meta_box' ),
            'product',
            'normal',
            'high',
            array( 'tabs' => $tabs )
        );
    }

    public function plantshop_wc_render_meta_box( WP_Post $product, array $metabox ): void {
        $this->view->render( $product, $metabox['args']['tabs'] );
    }

    public function plantshop_wc_save_meta_box_data( int $product_id ): void {
        if ( get_post_type( $product_id ) !== 'product' || ! current_user_can( 'edit_post', $product_id ) ) {
            return;
        }

        $nonce = isset( $_POST['plantshop-wc_tabs_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['plantshop-wc_tabs_nonce'] ) ) : '';

        if ( ! wp_verify_nonce( $nonce, 'plantshop-wc_product_tabs' ) ) {
            return;
        }

        $tabs = $this->model->get_tabs( $product_id );
        if ( empty( $tabs ) ) {
            return;
        }

        $posted = array();

        if ( isset( $_POST['plantshop-wc_tab'] ) && is_array( $_POST['plantshop-wc_tab'] ) ) {
            $posted = map_deep( wp_unslash( $_POST['plantshop-wc_tab'] ), 'sanitize_text_field' );
        }

        foreach ( $tabs as $tab ) {

            $tab->save( $product_id, $posted );

        }
    }

    public function plantshop_wc_meta_box_enqueue( string $hook ): void {

        if ( $hook !== 'post.php' && $hook !== 'post-new.php' ) {
            return;
        }
        if ( get_post_type() !== 'product' ) {
            return;
        }

        wp_enqueue_style(
            'plantshop-wc-admin-tw',
            PLANTSHOP_WC_PUBLIC_URL . 'admin/css/tw.css',
            array(),
            PLANTSHOP_WC_PLUGIN_VERSION
        );

        wp_enqueue_style(
            'tabs-css',
            PLANTSHOP_WC_PUBLIC_URL . 'admin/css/tabs.css',
            array( 'plantshop-wc-admin-tw' ),
            PLANTSHOP_WC_PLUGIN_VERSION
        );

        wp_enqueue_script(
            'plantshop-wc-tabs-js',
            PLANTSHOP_WC_PUBLIC_URL . 'admin/js/tabs.js',
            array( 'jquery' ),
            '1.0.0',
            true
        );

        wp_localize_script(
            'plantshop-wc-tabs-js',
            'plantshop_wc_ajax_tabs',
            array(
                'url'   => admin_url( 'admin-ajax.php' ),
                'nonce' => wp_create_nonce( 'ajax-tabs-nonce' ),
            )
        );

        wp_add_inline_style( 'wp-admin', '.plantshop-wc-tab-content.hidden{display:none;}' );
    }
}
