<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Product_Promotion_Type_Label' ) ) {

    class Pricely_Product_Promotion_Type_Label {

        private static $instance;
        private $ui_type = 'label';
        private $ui_option_key = 'ui_labels';
        private $locale;

        public static function get_instance(): self {

            if ( !self::$instance ) {

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

            return self::$instance;
        }

        public function can_process_data( $promotion_type ) {

            return ('label' == $promotion_type);
        }

        public function process_data( $promotion_data, $promotion_args, $data ) {

            $processed_data = $this->get_label_options( $this->get_translated_args( $promotion_args, $data ), $data );

            if ( !$processed_data ) {

                return $promotion_data;
            }

            $promotion_data[ 'views' ] = $processed_data;

            return $promotion_data;
        }

        private function get_label_options( $promotion_args, $data ) {

            if ( !$this->can_process_view( $data ) ) {

                return false;
            }

            $views = $this->get_single_product_view( $this->get_shop_view( array(), $promotion_args ), $promotion_args );

            if ( !count( $views ) ) {

                return false;
            }

            return $views;
        }

        private function get_shop_view( $label_options, $promotion_args ) {

            $show_in_shop = ('no' != $promotion_args[ 'shop' ][ 'enable' ]);

            if ( !$show_in_shop ) {

                return $label_options;
            }

            $label_options[ 'shop' ][ 'shop_loop_args' ] = $this->get_shop_loop_args( $promotion_args );

            $label_options[ 'shop' ][ 'variation_view_mode' ] = 'combined';

            return $this->process_ui_options( $label_options, $promotion_args, 'shop', 'shop' );
        }

        private function get_single_product_view( $label_options, $promotion_args ) {

            $show_in_single_product = ('no' != $promotion_args[ 'single_product' ][ 'enable' ]);

            if ( !$show_in_single_product ) {

                return $label_options;
            }

            $label_options[ 'single-product' ][ 'variation_view_mode' ] = $promotion_args[ 'variation_view_mode' ];

            return $this->process_ui_options( $label_options, $promotion_args, 'single-product', 'single_product' );
        }

        private function process_ui_options( $label_options, $promotion_args, $page_id, $fields_group ) {

            $promotion_ui_id = $this->get_ui_id( $promotion_args[ $fields_group ][ 'ui_id' ] );
            $ui_options = $this->get_ui_options( $promotion_ui_id );

            $label_options[ $page_id ][ 'view_id' ] = $this->get_view_id( $page_id );

            $label_options[ $page_id ][ 'position' ] = $promotion_args[ $fields_group ][ 'position' ];

            $label_options[ $page_id ][ 'ui_id' ] = $promotion_ui_id;

            $label_options[ $page_id ][ 'ui_css_class' ] = $this->get_css_class( $promotion_ui_id );

            $label_options[ $page_id ][ 'label_args' ] = $this->get_plain_text_args( $promotion_args );

            return $label_options;
        }

        private function get_plain_text_args( $promotion_args ) {

            $plain_text = $promotion_args[ 'text' ];

            if ( empty( $plain_text ) ) {

                return false;
            }

            $text_mode = $promotion_args[ 'mode' ];

            return array(
                'mode' => $text_mode,
                'text' => $plain_text
            );
        }

        private function get_view_id( $page_id ) {

            if ( 'shop' == $page_id ) {

                return 'shop-label';
            }

            return 'single-label';
        }

        private function get_css_class( $promotion_ui_id ) {

            return Pricely_Views_CSS::get_instance()->get_css_class( $promotion_ui_id, $this->ui_type );
        }

        private function get_ui_options( $promotion_ui_id ) {

            $options = $this->get_options( $this->ui_option_key, $this->get_default_ui_options() );

            foreach ( $options as $option ) {

                if ( $promotion_ui_id == $option[ 'ui_id' ] ) {

                    return $option;
                }
            }

            if ( count( $options ) ) {

                return $options[ 0 ];
            }
        }

        private function get_ui_id( $promotion_ui_id ) {

            if ( empty( $promotion_ui_id ) ) {

                return '2234343';
            }

            return $promotion_ui_id;
        }

        private function get_translated_args( $promotion_args, $data ) {

            if ( !$this->can_process_view( $data ) ) {

                return $promotion_args;
            }

            $show_in_single_product = ('no' != $promotion_args[ 'single_product' ][ 'enable' ]);
            $show_in_shop = ('no' != $promotion_args[ 'shop' ][ 'enable' ]);

            if ( !$show_in_single_product && !$show_in_shop ) {

                return $promotion_args;
            }

            $settings_id = $promotion_args[ 'setting_id' ];
            $promotion_id = $promotion_args[ 'id' ];

            $promotion_args[ 'text' ] = $this->get_translated_text( $promotion_args[ 'text' ], $settings_id, $promotion_id, 'text' );

            return $promotion_args;
        }

        private function get_translated_text( $text, $settings_id, $promotion_id, $text_key ) {

            $translation_id = 'product-promotion-' . $this->ui_type . '-' . $settings_id . '-' . $promotion_id . '-' . $text_key;

            return $this->get_translate_string( $text, $translation_id );
        }

        private function get_translate_string( $value, $translation_id ) {

            if ( empty( $value ) ) {

                return $value;
            }

            if ( is_null( $this->locale ) ) {

                $this->locale = Pricely_Locale::get_instance();
            }

            return $this->locale->translate_string( $value, $translation_id );
        }

        private function get_default_ui_options() {

            $defaults = array();

            $defaults[] = array(
                'ui_id' => '2234343',
            );

            return $defaults;
        }

        private function get_shop_loop_args( $promotion_args ) {

            $shop_loop_args = array(
                'loops' => array(),
                'compare' => 'in_list'
            );

            if ( isset( $promotion_args[ 'shop_loop_args' ][ 'loops' ] ) && is_array( $promotion_args[ 'shop_loop_args' ][ 'loops' ] ) ) {

                $shop_loop_args[ 'loops' ] = $promotion_args[ 'shop_loop_args' ][ 'loops' ];
            }

            if ( isset( $promotion_args[ 'shop_loop_args' ][ 'compare' ] ) ) {

                $shop_loop_args[ 'compare' ] = $promotion_args[ 'shop_loop_args' ][ 'compare' ];
            }

            return $shop_loop_args;
        }

        private function can_process_view( $data ) {

            if ( !isset( $data[ 'wc' ][ 'product' ] ) ) {

                return false;
            }

            $product_data = $data[ 'wc' ][ 'product' ];

            if ( 'variable' == $product_data[ 'type' ] && $product_data[ 'variation_id' ] > 0 ) {

                return false;
            }

            return true;
        }

        private function get_options( $option_key, $default ) {

            $pricely = Pricely::get_instance();

            return $pricely->get_options( $option_key, $default );
        }

    }

}

