<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Pipeline_View_Position_Shortcode' ) ) {

    class Pricely_Pipeline_View_Position_Shortcode {

        private static $instance;
        private $shop_page_id;
        private $single_page_id;
        private $position_id;

        public static function get_instance(): self {

            if ( !self::$instance ) {

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

            return self::$instance;
        }

        public function __construct() {

            $this->shop_page_id = 'shop';
            $this->single_page_id = 'single-product';
            $this->position_id = 'shortcode';

            //implements shortcode position
            //e.g [pricely_view_position product_id="1" is_single="yes" /]

            add_shortcode( 'pricely_view_position', array( $this, 'render_shortcode' ) );
        }

        public function render_shortcode( $in_attrs ) {

            $attrs = $this->process_attrs( $in_attrs );

            $product = $this->get_product( $attrs[ 'product_id' ] );

            if ( !$product ) {

                return '';
            }

            $is_single = $attrs[ 'is_single' ];

            return $this->render_shortcode_views( $is_single, $product );
        }

        private function render_shortcode_views( $is_single, $product ) {

            ob_start();

            $views_data = $this->get_shortcode_position_views( $is_single, $product );

            if ( !$views_data ) {

                return ob_get_clean();
            }

            foreach ( $views_data as $view_data ) {

                $this->render_view( $view_data );
            }

            return ob_get_clean();
        }

        private function render_view( $view_data ) {

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

        private function get_shortcode_position_views( $is_single, $product ) {

            $page_id = $this->get_page_id( $is_single );

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

        private function get_page_id( $is_single ) {

            if ( $is_single ) {

                return $this->single_page_id;
            }

            return $this->shop_page_id;
        }

        private function process_attrs( $in_attrs ) {

            $attrs = array();

            if ( isset( $in_attrs[ 'is_single' ] ) && ('no' === $in_attrs[ 'is_single' ] || !$in_attrs[ 'is_single' ]) ) {

                $attrs[ 'is_single' ] = false;
            }

            if ( isset( $in_attrs[ 'product_id' ] ) && is_numeric( $in_attrs[ 'product_id' ] ) ) {

                $attrs[ 'is_single' ] = ( int ) $in_attrs[ 'product_id' ];
            }

            $default_attrs = array(
                'is_single' => true,
                'product_id' => 0
            );

            return shortcode_atts( $default_attrs, $attrs );
        }

        private function get_product( $product_id ) {

            $product = $this->get_product_by_id( $product_id );

            if ( !$product ) {

                return $this->get_product_from_globals();
            }

            return $product;
        }

        private function get_product_by_id( $product_id ) {

            if ( $product_id <= 0 ) {

                return false;
            }

            $product = Pricely_Product::get_instance()->get_product( $product_id );

            if ( is_null( $product ) ) {

                return false;
            }

            if ( false === $product ) {

                return false;
            }

            return $product;
        }

        private function get_product_from_globals() {

            global $product;

            if ( $product ) {

                return $product;
            }

            return false;
        }

    }

}
