<?php

namespace Rtc\PlantShopWC\Admin\Modules;

use Rtc\PlantShopWC\Admin\Interfaces\ProductModule;
use WP_Post;

class BotanicalName implements ProductModule {


    public function slug(): string {
        return 'botanical_name';
    }

    public function label(): string {
        return __( 'Botanical name', 'plantshop-wc' );
    }

    public function vars(): array {
        return array(
            array( 'genus', __( 'Genus', 'plantshop-wc' ) ),
            array( 'species', __( 'Species', 'plantshop-wc' ) ),
            array( 'variety', __( 'Variety', 'plantshop-wc' ) ),
            array( 'vernacular', __( 'Vernacular', 'plantshop-wc' ) ),
        );
    }

    public function quick_edit_enabled(): bool {
        return true;
    }

    public function render( WP_Post $product ): void {

        $vars = self::vars();

        $values = self::get( $product->ID );

        ?>
        <div class="plantshop-wc-fields">
            <?php foreach ( $vars as $var_data ) { ?>
                <?php
                    list( $var, $label ) = $var_data;

                    $value = isset( $values[ $var ] ) ? $values[ $var ] : '';
                ?>
                <div class="plantshop-wc-field generate">
                    <label for="<?php echo esc_attr( 'plantshop-wc-' . $var ); ?>">
                        <?php echo esc_html( $label ); ?>
                    </label>
                    <input
                        type="text"
                        id="<?php echo esc_attr( 'plantshop-wc-' . $var ); ?>"
                        class="half"
                        name="<?php echo esc_attr( 'plantshop-wc_tab[' . $var . ']' ); ?>"
                        value="<?php echo esc_attr( $value ); ?>"
                    />
                </div>
            <?php } ?>
        </div>
        <?php
    }

    public function save( int $product_id, array $posted ): void {
        $slug     = self::slug();
        $vars     = self::vars();
        $existing = self::get( $product_id );
        $data     = is_array( $existing ) ? $existing : array();

        foreach ( $vars as $var_data ) {
            $var = $var_data[0];

            if ( ! array_key_exists( $var, $posted ) ) {
                continue;
            }

            $raw_value = $posted[ $var ];
            if ( is_array( $raw_value ) ) {
                $raw_value = '';
            }
            $data[ $var ] = sanitize_text_field( wp_unslash( $raw_value ) );
        }

        update_post_meta( $product_id, '_plantshop-wc_' . $slug, $data );
    }

    public function get( $product_id ): array {

        $slug = self::slug();

        $stored = get_post_meta( $product_id, '_plantshop-wc_' . $slug, true );
        return is_array( $stored ) ? $stored : array();
    }

    public function build( $product, string $original_title, bool $allow_html ) {

        if ( ! $product ) {
            return $original_title;
        }

        $id = is_object( $product ) ? (int) $product->get_id() : (int) $product;
        if ( ! $id ) {
            return $original_title;
        }

        $meta = self::get( $id );

        $f1 = trim( (string) ( $meta['genus'] ?? '' ) );
        $f2 = trim( (string) ( $meta['species'] ?? '' ) );
        $f3 = trim( (string) ( $meta['variety'] ?? '' ) );
        $f4 = trim( (string) ( $meta['vernacular'] ?? '' ) );

        if ( empty( $f1 ) ) {
            return $original_title;
        }

        $title  = $allow_html ? '<em>' . esc_html( "$f1 $f2" ) . '</em>' : "$f1 $f2";
        $title .= ! empty( $meta['variety'] ) ? " '" . esc_html( $f3 ) . "'" : '';
        $title .= ! empty( $f4 ) ? ' (' . esc_html( $f4 ) . ')' : '';

        return $allow_html ? wp_kses( $title, array( 'em' => array() ) ) : $title;
    }
}
