<?php

namespace Rtc\PlantShopWC\Admin\Modules;

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

class Plot implements ProductModule {


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

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

    public function vars(): array {
        return array(
            array( 'bed_number', __( 'Bed Number', 'plantshop-wc' ) ),
            array( 'plot_description', __( 'Plot Description', '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 ] : '';
                    $field_id_base     = 'plantshop-wc-' . $product->ID . '-' . $var;
                    $is_plot_textarea  = ( 'plot_description' === $var );
                    $textarea_field_id = $field_id_base . '-textarea';
                    $number_field_id   = $field_id_base . '-number';
                    $label_for         = $is_plot_textarea ? $textarea_field_id : $number_field_id;
                ?>
                <div class="plantshop-wc-field">
                    <label for="<?php echo esc_attr( $label_for ); ?>">
                        <?php echo esc_html( $label ); ?>
                    </label>
                    <?php if ( $is_plot_textarea ) { ?>
                        <textarea
                            maxlength="150"
                            id="<?php echo esc_attr( $textarea_field_id ); ?>"
                            class="half medium"
                            name="<?php echo esc_attr( 'plantshop-wc_tab[' . $var . ']' ); ?>"
                        ><?php echo esc_textarea( $value ); ?></textarea>
                    <?php } else { ?>
                        <input
                            type="number"
                            id="<?php echo esc_attr( $number_field_id ); ?>"
                            class="half"
                            name="<?php echo esc_attr( 'plantshop-wc_tab[' . $var . ']' ); ?>"
                            min="1"
                            max="9999"
                            value="<?php echo esc_attr( $value ); ?>"
                        />
                    <?php } ?>
                </div>
            <?php } ?>
        </div>
        <?php
    }

    public function save( int $product_id, array $posted ): void {
        $vars     = self::vars();
        $slug     = self::slug();
        $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();
    }
}
