<?php

namespace Rtc\PlantShopWC\Frontend\Views;

class CartView {


    public function render_neighbors( array $neighbors ): string {

        $good = $neighbors['good_neighbors'] ?? array();
        $bad  = $neighbors['bad_neighbors'] ?? array();

        if ( empty( $good ) && empty( $bad ) ) {
            return '';
        }

        $good_icon = PLANTSHOP_WC_ASSET_URL . 'good_neighbor.png';
        $bad_icon  = PLANTSHOP_WC_ASSET_URL . 'bad_neighbor.png';

        ob_start(); ?>

        <section class="plantshop-neighbors">

            <?php
            if ( ! empty( $good ) ) {
                ?>
                <div class="plantshop-neighbors__group plantshop-neighbors__group--good">
                    <h3 class="plantshop-neighbors__heading">
                    <?php
                        echo esc_html__( 'Good Neighbors', 'plantshop-wc' );
                    ?>
                    </h3>
                    <table class="plantshop-neighbors__list" style="table-layout:fixed;">
                        <thead class="screen-reader-text">
                            <tr>
                                <th scope="col"><?php esc_html_e( 'From', 'plantshop-wc' ); ?></th>
                                <th scope="col"><?php esc_html_e( 'Relation', 'plantshop-wc' ); ?></th>
                                <th scope="col"><?php esc_html_e( 'To', 'plantshop-wc' ); ?></th>
                            </tr>
                        </thead>
                        <?php
                        foreach ( $good as $edge ) {
                            $from = (string) ( $edge['from'] ?? '' );
                            $to   = (string) ( $edge['to'] ?? '' );
                            $note = trim( (string) ( $edge['note'] ?? '' ) );
                            if ( $from === '' || $to === '' ) {
                                continue;
                            }
                            ?>
                            <tbody class="neighbor-block">
                            <tr class="neighbor-row">
                                <td style="width: 45%;" class="plantshop-neighbor__from"><strong>
                                <?php
                                        echo wp_kses( $from, array( 'em' => array() ) );
                                ?>
                                </strong></td>
                                    <td style="width: 10%;" class="plantshop-neighbor__icon"><img alt="" src="
                                <?php
                                    echo esc_url( $good_icon );
                                ?>
                                "/></td>
                                <td style="width: 45%;" class="plantshop-neighbor__to"><strong>
                                <?php
                                        echo wp_kses( $to, array( 'em' => array() ) );
                                ?>
                                </strong></td>
                            </tr>

                            <?php
                            if ( $note !== '' ) {
                                ?>
                                <tr class="neighbor-note">
                                    <td class="plantshop-neighbor__note" colspan="3">
                                    <?php
                                        echo esc_html( $note );
                                    ?>
                                    </td>
                                </tr>
                                <?php
                            }
                            ?>
                            </tbody>
                            <?php
                        }
                        ?>
                    </table>
                </div>
                <?php
            }
            ?>

            <?php
            if ( ! empty( $bad ) ) {
                ?>
                <div class="plantshop-neighbors__group plantshop-neighbors__group--bad">
                    <h3 class="plantshop-neighbors__heading">
                    <?php
                        echo esc_html__( 'Bad Neighbors', 'plantshop-wc' );
                    ?>
                    </h3>
                    <table class="plantshop-neighbors__list" style="table-layout:fixed;">
                        <thead class="screen-reader-text">
                            <tr>
                                <th scope="col"><?php esc_html_e( 'From', 'plantshop-wc' ); ?></th>
                                <th scope="col"><?php esc_html_e( 'Relation', 'plantshop-wc' ); ?></th>
                                <th scope="col"><?php esc_html_e( 'To', 'plantshop-wc' ); ?></th>
                            </tr>
                        </thead>

                        <?php
                        foreach ( $bad as $edge ) {
                            $from = (string) ( $edge['from'] ?? '' );
                            $to   = (string) ( $edge['to'] ?? '' );
                            $note = trim( (string) ( $edge['note'] ?? '' ) );
                            if ( $from === '' || $to === '' ) {
                                continue;
                            }
                            ?>
                            <tbody class="neighbor-block">
                            <tr class="neighbor-row">
                                <td style="width: 45%;" class="plantshop-neighbor__from"><strong>
                                <?php
                                        echo wp_kses( $from, array( 'em' => array() ) );
                                ?>
                                </strong></td>
                                    <td style="width: 10%;" class="plantshop-neighbor__icon"><img alt="" src="
                                <?php
                                    echo esc_url( $bad_icon );
                                ?>
                                "/></td>
                                <td style="width: 45%;" class="plantshop-neighbor__to"><strong>
                                <?php
                                        echo wp_kses( $to, array( 'em' => array() ) );
                                ?>
                                </strong></td>
                            </tr>

                            <?php
                            if ( $note !== '' ) {
                                ?>
                                <tr class="neighbor-note">
                                    <td class="plantshop-neighbor__note" colspan="3">
                                    <?php
                                        echo esc_html( $note );
                                    ?>
                                    </td>
                                </tr>
                                <?php
                            }
                            ?>
                            </tbody>
                            <?php
                        }
                        ?>

                    </table>
                </div>
                <?php
            }
            ?>

        </section>

        <?php
        $html = ob_get_clean();
        return (string) $html;
    }
}
