<?php

namespace Rtc\PlantShopWC\Admin\Modules;

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

class Neighbors implements ProductModule {
    private const TAB_FIELD_PREFIX = 'plantshop-wc_tab[';


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

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

    public function vars(): array {
        return array(
            array( 'good_neighbors', __( 'Good Neighbors', 'plantshop-wc' ) ),
            array( 'bad_neighbors', __( 'Bad Neighbors', 'plantshop-wc' ) ),
        );
    }

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

    public function render( WP_Post $product ): void {

        $vars = self::vars();

        ?>
        <div class="plantshop-wc-fields">
            <?php

            foreach ( $vars as [$var, $lang] ) {

                $vals = get_post_meta( $product->ID, '_plantshop-wc_' . $var, true ) ?? array();

                ?>
                <div class="plantshop-wc-field">
                    <p class="label">
                    <?php
                        echo esc_html( $lang )
                    ?>
                    </p>
                        <div id="<?php echo esc_attr( 'plantshop-wc_repeater_' . $var ); ?>" class="plantshop-wc-repeater">
                        <?php
                        if ( ! empty( $vals ) && is_array( $vals ) ) {

                            foreach ( $vals as $i => $val ) {

                                $id   = $val['id'] ?? '';
                                $name = $val['name'] ?? '';
                                $note = $val['note'] ?? '';
                                ?>

                                <div class="plantshop-wc-repeater-row">
                                    <div class="row-search">
                                        <input
                                            type="hidden"
                                            name="<?php echo esc_attr( self::TAB_FIELD_PREFIX . $var . '][' . $i . '][id]' ); ?>"
                                            value="<?php echo esc_attr( $id ); ?>"
                                        />
                                        <input
                                            type="text"
                                            class="neighbor-search"
                                            name="<?php echo esc_attr( self::TAB_FIELD_PREFIX . $var . '][' . $i . '][name]' ); ?>"
                                            value="<?php echo esc_attr( $name ); ?>"
                                            placeholder="<?php esc_attr_e( 'Search neighbor…', 'plantshop-wc' ); ?>"
                                            oninput="plantshop_wc_ajax_neighbor_search(this)"
                                        />
                                        <span class="neighbor-search-results"></span>
                                    </div>
                                    <textarea
                                        name="<?php echo esc_attr( self::TAB_FIELD_PREFIX . $var . '][' . $i . '][note]' ); ?>"
                                        placeholder="<?php esc_attr_e( 'Notice', 'plantshop-wc' ); ?>"
                                    ><?php echo esc_textarea( $note ); ?></textarea>
                                    <button type="button" data-type="<?php echo esc_attr( $var ); ?>" class="button plantshop-wc-remove-row">x
                                    </button>
                                </div>

                                <?php
                            }
                        }
                        ?>

                    </div>
                        <button type="button" data-type="<?php echo esc_attr( $var ); ?>" class="button plantshop-wc-add-row">
                        <?php
                            esc_html_e( 'Add neighbor', 'plantshop-wc' );
                        ?>
                        </button>
                    </div>

                    <script type="text/html" id="<?php echo esc_attr( 'tmpl-plantshop-wc-repeater-row-' . $var ); ?>">
                        <div class="plantshop-wc-repeater-row">
                            <div class="row-search">
                                <input type="hidden" name="<?php echo esc_attr( self::TAB_FIELD_PREFIX . $var . '][{{{ data.index }}}][id]' ); ?>" value=""/>
                                <input
                                    type="text"
                                    class="neighbor-search"
                                    name="<?php echo esc_attr( self::TAB_FIELD_PREFIX . $var . '][{{{ data.index }}}][name]' ); ?>"
                                    value=""
                                    placeholder="<?php esc_attr_e( 'Search neighbor…', 'plantshop-wc' ); ?>"
                                    oninput="plantshop_wc_ajax_neighbor_search(this)"
                                />
                                <span class="neighbor-search-results"></span>
                            </div>
                            <textarea
                                name="<?php echo esc_attr( self::TAB_FIELD_PREFIX . $var . '][{{{ data.index }}}][note]' ); ?>"
                                placeholder="<?php esc_attr_e( 'Notice', 'plantshop-wc' ); ?>"
                            ></textarea>
                            <button type="button" data-type="<?php echo esc_attr( $var ); ?>" class="button plantshop-wc-remove-row">x
                            </button>
                        </div>
                    </script>

                <?php

            }

            ?>
        </div>
        <?php
    }

    public function save( int $product_id, array $posted ): void {
        $vars = self::vars();

        foreach ( $vars as [$var,] ) {
            if ( ! array_key_exists( $var, $posted ) ) {
                continue;
            }

            $vals    = $posted[ $var ];
            $cleaned = array();

            if ( is_array( $vals ) ) {

                foreach ( $vals as $val ) {

                    $id   = sanitize_text_field( $val['id'] ?? '' );
                    $name = sanitize_text_field( $val['name'] ?? '' );
                    $note = sanitize_textarea_field( $val['note'] ?? '' );

                    if ( $name || $note ) {
                        $cleaned[] = array(
                            'id'   => $id,
                            'name' => $name,
                            'note' => $note,
                        );
                    }
                }

                update_post_meta( $product_id, '_plantshop-wc_' . $var, $cleaned );
            }
        }
    }

    public function get( int $product_id ): ?array {

        $vars = self::vars();
        $data = array();

        foreach ( $vars as [$var,] ) {

            $vals = get_post_meta( $product_id, '_plantshop-wc_' . $var, true ) ?? array();
            if ( ! empty( $vals ) && is_array( $vals ) ) {
                $data[ $var ] = $vals;
            } else {
                $data[ $var ] = array();
            }
        }

        return $data;
    }
}
