<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Pipeline_View_Position_Shop' ) ) {

    class Pricely_Pipeline_View_Position_Shop {

        private static $instance;
        private $page_id;

        public static function get_instance(): self {

            if ( !self::$instance ) {

                self::$instance = new self();
            }

            return self::$instance;
        }

        public function __construct() {

            $this->page_id = 'shop';

            $before_title = $this->get_view_position_by_id( 'before_title' );

            if ( $before_title ) {
                
                add_action( $before_title[ 'hook' ], array( $this, 'render_before_title' ), $before_title[ 'priority' ] );
            }

            $after_title = $this->get_view_position_by_id( 'after_title' );

            if ( $after_title ) {

                add_action( $after_title[ 'hook' ], array( $this, 'render_after_title' ), $after_title[ 'priority' ] );
            }

            $after_rating = $this->get_view_position_by_id( 'after_rating' );

            if ( $after_rating ) {

                add_action( $after_rating[ 'hook' ], array( $this, 'render_after_rating' ), $after_rating[ 'priority' ] );
            }

            $after_price = $this->get_view_position_by_id( 'after_price' );

            if ( $after_price ) {

                add_action( $after_price[ 'hook' ], array( $this, 'render_after_price' ), $after_price[ 'priority' ] );
            }

            $after_add_to_cart = $this->get_view_position_by_id( 'after_add_to_cart' );

            if ( $after_add_to_cart ) {

                add_action( $after_add_to_cart[ 'hook' ], array( $this, 'render_after_add_to_cart' ), $after_add_to_cart[ 'priority' ] );
            }
        }

        public function render_before_title() {

            $this->render_position_views( 'before_title' );
        }

        public function render_after_title() {

            $this->render_position_views( 'after_title' );
        }

        public function render_after_rating() {

            $this->render_position_views( 'after_rating' );
        }

        public function render_after_price() {

            $this->render_position_views( 'after_price' );
        }

        public function render_after_add_to_cart() {

            $this->render_position_views( 'after_add_to_cart' );
        }

        private function render_position_views( $position_id ) {
           
            global $product;
           
            $views_data = $this->get_views_by_position( $position_id, $product );

            if ( !$views_data ) {

                return;
            }
 
            foreach ( $views_data as $view_data ) {

                $this->render_view( $view_data );
            }
        }

        private function render_view( $view_data ) {

            return Pricely_Pipeline_View_Positions::get_instance()->render_view( $view_data );
        }

        private function get_views_by_position( $position_id, $product ) {
            
            return Pricely_Pipeline_View_Positions::get_instance()->get_views_by_position( $this->page_id, $position_id, $product );
        }

        private function get_view_position_by_id( $position_id ) {

            return Pricely_Pipeline_View_Positions::get_instance()->get_view_position_by_id( $this->page_id, $position_id );
        }

    }

}
