<?php

namespace Rtc\PlantShopWC\Frontend\Controllers;

use Rtc\PlantShopWC\Admin\Models\GeneralSettingsModel;
use Rtc\PlantShopWC\Admin\Modules\BotanicalName;
use Rtc\PlantShopWC\Admin\Modules\Neighbors;
use Rtc\PlantShopWC\Admin\Modules\PlantInstructions;
use Rtc\PlantShopWC\Frontend\Models\SingleProductModel;
use Rtc\PlantShopWC\Frontend\Views\SingleProductView;

class SingleProductController {


    protected $model;
    protected $view;

    protected $botanicalNameChecked;

    public function __construct() {

        $this->model = new SingleProductModel();
        $this->view  = new SingleProductView();
    }

    public function register_hooks(): void {

        add_action( 'woocommerce_after_single_product_summary', array( $this, 'plantshop_wc_plant_instructions' ), 15 );
        add_action( 'woocommerce_after_single_product_summary', array( $this, 'plantshop_wc_neighbors' ), 10 );

        add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_product_styles' ) );

        // Change the Product Title to Botanical Name if enabled
        $opts                       = get_option( GeneralSettingsModel::get_option_key(), array() );
        $this->botanicalNameChecked = isset( $opts['botanical_name_replacement'] ) ? (string) $opts['botanical_name_replacement'] : '';
        if ( ! empty( $this->botanicalNameChecked ) && $this->botanicalNameChecked ) {
            add_filter( 'the_title', array( $this, 'plantshop_wc_title' ), 20, 2 );
        }
    }

    public function enqueue_product_styles(): void {

        wp_enqueue_style( 'plantshop-wc-cart', PLANTSHOP_WC_PUBLIC_URL . 'frontend/css/cart.css', array(), PLANTSHOP_WC_PLUGIN_VERSION );

        wp_enqueue_style( 'plantshop-wc-frontend-single-product', PLANTSHOP_WC_PUBLIC_URL . 'frontend/css/single-product.css', array(), PLANTSHOP_WC_PLUGIN_VERSION );
    }

    public function plantshop_wc_plant_instructions(): void {

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

        $product_id = $product->get_id();

        if ( ! GeneralSettingsModel::is_product_included( $product_id ) ) {
            return;
        }

        $data = ( new PlantInstructions() )->get( $product_id );
        if ( ! $data ) {
            return;
        }

        $this->view->render_instructions( $data );
    }


    public function plantshop_wc_neighbors(): void {

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

        $product_id = $product->get_id();

        if ( ! GeneralSettingsModel::is_product_included( $product_id ) ) {
            return;
        }

        $raw = ( new Neighbors() )->get( $product_id );
        if ( empty( $raw ) || ! is_array( $raw ) ) {
            return;
        }

        $data = $this->model->get_neighbor_data( $raw, $product_id );
        if ( empty( $data ) ) {
            return;
        }

        $this->view->render_neighbors( $data );
    }

    public function plantshop_wc_title( $title, $post_id ) {

        if ( is_admin() || get_post_type( $post_id ) !== 'product' ) {
            return $title;
        }

        $product = function_exists( 'wc_get_product' ) ? wc_get_product( $post_id ) : null;
        if ( ! $product || ! GeneralSettingsModel::is_product_included( $product ) ) {
            return $title;
        }

        return ( new BotanicalName() )->build( $product, $title, true );
    }
}
