<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}


if ( !class_exists( 'Pricely_Views' ) ) {

    class Pricely_Views {

        private static $instance;
        private $view_paths;
        private $view_positions;

        public static function get_instance(): self {

            if ( !self::$instance ) {

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

            return self::$instance;
        }

        public function __construct() {

            $this->view_paths = array();
            $this->view_positions = array();
        }

        public function render_view( $view_data ) {

            $view_id = $view_data[ 'view_id' ];

            $view_path = $this->get_view_path( $view_id );

            if ( $view_path ) {

                $view_rendered = true;

                include $view_path;

                return $view_rendered;
            }

            return false;
        }

        public function get_view_positions() {

            if ( count( $this->view_positions ) ) {

                return $this->view_positions;
            }

            $this->view_positions = array(
                'shop' => $this->get_shop_positions(),
                'single-product' => $this->get_single_product_positions(),
            );

            $this->view_positions = apply_filters( 'pricely_register-view-positions', $this->view_positions );

            return $this->view_positions;
        }

        private function get_shop_positions() {

            return array(
                'before_title' => array(
                    'show_in_admin' => true,
                    'title' => esc_html__( 'Before Loop Title', 'pricely-dynamic-pricing-cart-discounts' ),
                    'hook' => 'woocommerce_shop_loop_item_title',
                    'priority' => 9,
                ),
                'after_title' => array(
                    'show_in_admin' => true,
                    'title' => esc_html__( 'After Loop Title', 'pricely-dynamic-pricing-cart-discounts' ),
                    'hook' => 'woocommerce_after_shop_loop_item_title',
                    'priority' => 4,
                ),
                'after_rating' => array(
                    'show_in_admin' => true,
                    'title' => esc_html__( 'After Loop Rating', 'pricely-dynamic-pricing-cart-discounts' ),
                    'hook' => 'woocommerce_after_shop_loop_item_title',
                    'priority' => 6,
                ),
                'after_price' => array(
                    'show_in_admin' => true,
                    'title' => esc_html__( 'After Loop Price', 'pricely-dynamic-pricing-cart-discounts' ),
                    'hook' => 'woocommerce_after_shop_loop_item_title',
                    'priority' => 11,
                ),
                'after_add_to_cart' => array(
                    'show_in_admin' => true,
                    'title' => esc_html__( 'After Loop Add to Cart', 'pricely-dynamic-pricing-cart-discounts' ),
                    'hook' => 'woocommerce_after_shop_loop_item',
                    'priority' => 11,
                ),
                'shortcode' => array(
                    'show_in_admin' => true,
                    'title' => esc_html__( 'Shortcode Position', 'pricely-dynamic-pricing-cart-discounts' ),
                    'hook' => false,
                    'priority' => false,
                ),
            );
        }

        private function get_single_product_positions() {

            return array(
                'before_title' => array(
                    'show_in_admin' => true,
                    'title' => esc_html__( 'Before Summary Title', 'pricely-dynamic-pricing-cart-discounts' ),
                    'hook' => 'woocommerce_single_product_summary',
                    'priority' => 4,
                ),
                'after_title' => array(
                    'show_in_admin' => true,
                    'title' => esc_html__( 'After Summary Title', 'pricely-dynamic-pricing-cart-discounts' ),
                    'hook' => 'woocommerce_single_product_summary',
                    'priority' => 6,
                ),
                'after_rating' => array(
                    'show_in_admin' => true,
                    'title' => esc_html__( 'After Summary Rating &amp; Price', 'pricely-dynamic-pricing-cart-discounts' ),
                    'hook' => 'woocommerce_single_product_summary',
                    'priority' => 11,
                ),
                'after_excerpt' => array(
                    'show_in_admin' => true,
                    'title' => esc_html__( 'After Summary Excerpts', 'pricely-dynamic-pricing-cart-discounts' ),
                    'hook' => 'woocommerce_single_product_summary',
                    'priority' => 21,
                ),
                'after_add_to_cart' => array(
                    'show_in_admin' => true,
                    'title' => esc_html__( 'After Summary Add to Cart', 'pricely-dynamic-pricing-cart-discounts' ),
                    'hook' => 'woocommerce_single_product_summary',
                    'priority' => 31,
                ),
                'after_meta' => array(
                    'show_in_admin' => true,
                    'title' => esc_html__( 'After Summary Meta', 'pricely-dynamic-pricing-cart-discounts' ),
                    'hook' => 'woocommerce_single_product_summary',
                    'priority' => 41,
                ),
                'after_share' => array(
                    'show_in_admin' => true,
                    'title' => esc_html__( 'After Summary Share', 'pricely-dynamic-pricing-cart-discounts' ),
                    'hook' => 'woocommerce_single_product_summary',
                    'priority' => 51,
                ),
                'shortcode' => array(
                    'show_in_admin' => true,
                    'title' => esc_html__( 'Shortcode Position', 'pricely-dynamic-pricing-cart-discounts' ),
                    'hook' => false,
                    'priority' => false,
                ),
            );
        }

        private function get_view_path( $view_id ) {

            $view_paths = $this->get_view_paths();

            if ( !count( $view_paths ) ) {

                return false;
            }

            if ( !isset( $view_paths[ $view_id ] ) ) {

                return false;
            }

            return $view_paths[ $view_id ];
        }

        private function get_view_paths() {

            if ( count( $this->view_paths ) ) {

                return $this->view_paths;
            }

            $this->view_paths = apply_filters( 'pricely_register-view-paths', $this->register_view_paths( array() ) );

            return $this->view_paths;
        }

        private function register_view_paths( $view_paths ) {

            //Cart Item Prices & subtotals Views
            $view_paths[ 'cart-item-prices' ] = dirname( __FILE__ ) . '/cart/cart-item-prices.php';
            $view_paths[ 'cart-item-subtotal' ] = dirname( __FILE__ ) . '/cart/cart-item-subtotal.php';
            $view_paths[ 'checkout-item-prices' ] = dirname( __FILE__ ) . '/cart/checkout-item-prices.php';
            $view_paths[ 'mini-cart-item-prices' ] = dirname( __FILE__ ) . '/cart/mini-cart-item-prices.php';


            //CountDown Inline Views
            $view_paths[ 'shop-countdown-inline' ] = dirname( __FILE__ ) . '/shop/countdown/inline.php';
            $view_paths[ 'single-countdown-inline' ] = dirname( __FILE__ ) . '/single/countdown/inline.php';

            //Volumetric Pricing Table Views
            $view_paths[ 'shop-volumetric-price-table-vertical' ] = dirname( __FILE__ ) . '/shop/tables/volumetric-price-vertical.php';
            $view_paths[ 'single-volumetric-price-table-vertical' ] = dirname( __FILE__ ) . '/single/tables/volumetric-price-vertical.php';

            $view_paths[ 'shop-volumetric-price-table-horizontal' ] = dirname( __FILE__ ) . '/shop/tables/volumetric-price-horizontal.php';
            $view_paths[ 'single-volumetric-price-table-horizontal' ] = dirname( __FILE__ ) . '/single/tables/volumetric-price-horizontal.php';

            //Label Views
            $view_paths[ 'shop-label' ] = dirname( __FILE__ ) . '/shop/label.php';
            $view_paths[ 'single-label' ] = dirname( __FILE__ ) . '/single/label.php';

            return $view_paths;
        }

    }

}
