<?php
class kavehproptions{

    private $prefix;

        public function __construct()
        {

            $this->prefix = 'single_product_options';
            add_action('woocommerce_init', array($this, 'init_metabox'));
            
            // WPML compatibility hooks
            if ($this->is_wpml_active()) {
                add_action('save_post', array($this, 'register_meta_strings'), 10, 2);
                add_filter('get_post_metadata', array($this, 'translate_meta_values'), 10, 4);
            }

        }
        
        /**
         * Check if WPML is active
         */
        private function is_wpml_active()
        {
            return defined('ICL_SITEPRESS_VERSION') && function_exists('icl_object_id');
        }
        
        /**
         * Register meta strings with WPML when post is saved
         */
        public function register_meta_strings($post_id, $post)
        {
            if ($post->post_type !== 'product' || !$this->is_wpml_active()) {
                return;
            }
            
            // Get all meta values for this product
            $meta_values = get_post_meta($post_id, $this->prefix, true);
            
            if (!is_array($meta_values)) {
                return;
            }
            
            // Register translatable text fields
            $translatable_fields = array(
                'kcoupon_single_text',
                'kvideo_qr_single_text1',
                'kvideo_qr_single_text2',
                'kvideo_ersalrygan_single_text1',
                'kvideo_ersalrygan_single_text2'
            );
            
            foreach ($translatable_fields as $field) {
                if (!empty($meta_values[$field])) {
                    do_action('wpml_register_single_string', 
                        'product_meta_' . $post_id, 
                        $field, 
                        $meta_values[$field]
                    );
                }
            }
            
            // Register repeater field strings
            $this->register_repeater_strings($post_id, $meta_values, 'ktickets_single', array('ktickets_single_text1', 'ktickets_single_text2'));
            $this->register_repeater_strings($post_id, $meta_values, 'kticketsthumnail_single', array('kticketsthumnail_single_text1'));
            $this->register_repeater_strings($post_id, $meta_values, 'klis_single', array('klis_single_text1', 'klis_single_text2'));
        }
        
        /**
         * Register repeater field strings
         */
        private function register_repeater_strings($post_id, $meta_values, $repeater_key, $text_fields)
        {
            if (!empty($meta_values[$repeater_key]) && is_array($meta_values[$repeater_key])) {
                foreach ($meta_values[$repeater_key] as $index => $item) {
                    foreach ($text_fields as $field) {
                        if (!empty($item[$field])) {
                            do_action('wpml_register_single_string', 
                                'product_meta_' . $post_id, 
                                $repeater_key . '_' . $index . '_' . $field, 
                                $item[$field]
                            );
                        }
                    }
                }
            }
        }
        
        /**
         * Translate meta values when retrieved
         */
        public function translate_meta_values($value, $object_id, $meta_key, $single)
        {
            if ($meta_key !== $this->prefix || !$this->is_wpml_active()) {
                return $value;
            }
            
            // Let WordPress get the original value first
            remove_filter('get_post_metadata', array($this, 'translate_meta_values'), 10);
            $original_value = get_post_meta($object_id, $meta_key, $single);
            add_filter('get_post_metadata', array($this, 'translate_meta_values'), 10, 4);
            
            if (!is_array($original_value)) {
                return $value;
            }
            
            // Translate the values
            $translated_value = $this->translate_meta_array($original_value, $object_id);
            
            return $translated_value;
        }
        
        /**
         * Translate meta array values
         */
        private function translate_meta_array($meta_array, $post_id)
        {
            $translatable_fields = array(
                'kcoupon_single_text',
                'kvideo_qr_single_text1',
                'kvideo_qr_single_text2',
                'kvideo_ersalrygan_single_text1',
                'kvideo_ersalrygan_single_text2'
            );
            
            // Translate simple fields
            foreach ($translatable_fields as $field) {
                if (!empty($meta_array[$field])) {
                    $translated = apply_filters('wpml_translate_single_string', 
                        $meta_array[$field], 
                        'product_meta_' . $post_id, 
                        $field
                    );
                    if ($translated) {
                        $meta_array[$field] = $translated;
                    }
                }
            }
            
            // Translate repeater fields
            $meta_array = $this->translate_repeater_fields($meta_array, $post_id, 'ktickets_single', array('ktickets_single_text1', 'ktickets_single_text2'));
            $meta_array = $this->translate_repeater_fields($meta_array, $post_id, 'kticketsthumnail_single', array('kticketsthumnail_single_text1'));
            $meta_array = $this->translate_repeater_fields($meta_array, $post_id, 'klis_single', array('klis_single_text1', 'klis_single_text2'));
            
            return $meta_array;
        }
        
        /**
         * Translate repeater field values
         */
        private function translate_repeater_fields($meta_array, $post_id, $repeater_key, $text_fields)
        {
            if (!empty($meta_array[$repeater_key]) && is_array($meta_array[$repeater_key])) {
                foreach ($meta_array[$repeater_key] as $index => $item) {
                    foreach ($text_fields as $field) {
                        if (!empty($item[$field])) {
                            $translated = apply_filters('wpml_translate_single_string', 
                                $item[$field], 
                                'product_meta_' . $post_id, 
                                $repeater_key . '_' . $index . '_' . $field
                            );
                            if ($translated) {
                                $meta_array[$repeater_key][$index][$field] = $translated;
                            }
                        }
                    }
                }
            }
            
            return $meta_array;
        }

        public function init_metabox()
        {
            if (!class_exists('CSF')) {
                return;
            }


            CSF::createMetabox($this->prefix, array(
                'title' => esc_html__('Kaveh Custom Meta', 'kavehTheme'),
                'post_type' => 'product',
            ));

            $this->add_section(esc_html__('General Meta', 'kavehTheme'), array(
                array(
                    'id'           => 'kpr_english_name',
                    'type'         => 'text',
                    'title'        => esc_html__('Product English Name', 'kavehTheme'),
                ),
                array(
                    'id'      => 'ksingle_options_off',
                    'type'    => 'switcher',
                    'title'   => esc_html__('Apply General Theme Settings', 'kavehTheme'),
                    'desc'   => esc_html__('If this option is turned off, specific settings will be activated for this product instead of the general theme settings. If turned on, all these settings will be inherited from the general settings.', 'kavehTheme'),
                    'text_on'    => esc_html__('Active', 'kavehTheme'),
                    'text_off'   => esc_html__('Inactive', 'kavehTheme'),
                    
                    'default' => true
                ),
                array(
                    'id'      => 'kvideo_show',
                    'type'    => 'switcher',
                    'title'   => esc_html__('Product Introduction Video', 'kavehTheme'),
                    'text_on'    => esc_html__('Active', 'kavehTheme'),
                    'text_off'   => esc_html__('Inactive', 'kavehTheme'),
                    'dependency' => array(
                        array( 'ksingle_options_off', '==', 'false' ),
                      ),
                    'default' => false
                ),
                array(
                    'id' => 'kvideo_iframe',
                    'desc' => esc_html__('In this style, you use third-party services like Aparat, etc.', 'kavehTheme'),
                    'type'  => 'code_editor',
                    'sanitize' => false,
                    'title' => esc_html__('Product Introduction Video iFrame', 'kavehTheme'),
                    'dependency' => array(
                        array( 'kvideo_show', '==', 'true' ),
                      ),
                ),
                array(
                    'id'    => 'kvideo_upload',
                    'type'  => 'upload',
                    'title' => esc_html__('Upload Product Video', 'kavehTheme'),
                    'button_title' => esc_html__('Upload Video', 'kavehTheme'),
                    'remove_title' => esc_html__('Remove Video', 'kavehTheme'),
                    'desc' => esc_html__('In this style, you directly upload the video to your host', 'kavehTheme'),
                    'dependency' => array(
                        array( 'kvideo_show', '==', 'true' ),
                      ),
          
                ),
                array(
                    'id'      => 'kcoupon_single',
                    'type'    => 'switcher',
                    'title'   => esc_html__('Show Discount Code', 'kavehTheme'),
                    'text_on'    => esc_html__('Active', 'kavehTheme'),
                    'text_off'   => esc_html__('Inactive', 'kavehTheme'),
                    'dependency' => array(
                        array( 'ksingle_options_off', '==', 'false' ),
                      ),
                    'default' => true
                ),
                array(
                    'id'           => 'kcoupon_single_image',
                    'type'         => 'upload',
                    'title'        => esc_html__('Discount Image', 'kavehTheme'),
                    'library'      => 'image',
                    'placeholder'  => 'http://',
                    'button_title' => esc_html__('Upload Image', 'kavehTheme'),
                    'remove_title' => esc_html__('Remove Image', 'kavehTheme'),
                    'dependency' => array(
                        array( 'kcoupon_single', '==', 1 ),
                        array( 'ksingle_options_off', '==', 'false' ),
                      ),
                    'default' => esc_url( KAVEH_TEMPLATE_URI ) . '/assets/images/single-pr/coupon.png',
                ),
                array(
                    'id'           => 'kcoupon_single_text',
                    'type'         => 'text',
                    'title'        => esc_html__('Discount Code Text', 'kavehTheme'),
                    'default' => esc_html__('10% discount code for first purchase: new', 'kavehTheme'),
                    'dependency' => array(
                        array( 'kcoupon_single', '==', 1 ),
                        array( 'ksingle_options_off', '==', 'false' ),
                      ),
                ),
                array(
                    'id'           => 'kcoupon_single_link',
                    'type'         => 'text',
                    'title'        => esc_html__('Link', 'kavehTheme'),
                    'default' => '#',
                    'dependency' => array(
                        array( 'kcoupon_single', '==', 1 ),
                        array( 'ksingle_options_off', '==', 'false' ),
                      ),
                ),
                array(
                    'id'      => 'kvideo_qr_single',
                    'type'    => 'switcher',
                    'title'   => esc_html__('Show Product QR Code', 'kavehTheme'),
                    'text_on'    => esc_html__('Active', 'kavehTheme'),
                    'text_off'   => esc_html__('Inactive', 'kavehTheme'),
                    'dependency' => array(
                        array( 'ksingle_options_off', '==', 'false' ),
                      ),
                    'default' => true
                ),
                array(
                    'id'           => 'kvideo_qr_single_text1',
                    'type'         => 'text',
                    'title'        => esc_html__('First QR Code Scan Text', 'kavehTheme'),
                    'default' => esc_html__('View on Mobile', 'kavehTheme'),
                    'dependency' => array(
                        array( 'kvideo_qr_single', '==', 1 ),
                        array( 'ksingle_options_off', '==', 'false' ),

                    ),
                ),
                array(
                    'id'           => 'kvideo_qr_single_text2',
                    'type'         => 'text',
                    'title'        => esc_html__('Second QR Code Scan Text', 'kavehTheme'),
                    'default' => esc_html__('Scan Now', 'kavehTheme'),
                    'dependency' => array(
                        array( 'kvideo_qr_single', '==', 1 ),
                        array( 'ksingle_options_off', '==', 'false' ),

                    ),
                ),
                array(
                    'id'      => 'kvideo_ersalrygan_single',
                    'type'    => 'switcher',
                    'title'   => esc_html__('Free Shipping Box', 'kavehTheme'),
                    'text_on'    => esc_html__('Active', 'kavehTheme'),
                    'text_off'   => esc_html__('Inactive', 'kavehTheme'),
                    'dependency' => array(
                        array( 'ksingle_options_off', '==', 'false' ),
                      ),
                    'default' => true
                ),
                array(
                    'id'           => 'kvideo_ersalrygan_single_text1',
                    'type'         => 'text',
                    'title'        => esc_html__('First Box Text', 'kavehTheme'),
                    'default' => esc_html__('Free shipping for premium members', 'kavehTheme'),
                    'dependency' => array(
                        array( 'kvideo_ersalrygan_single', '==', 1 ),
                        array( 'ksingle_options_off', '==', 'false' ),

                    ),
                ),
                array(
                    'id'           => 'kvideo_ersalrygan_single_text2',
                    'type'         => 'text',
                    'title'        => esc_html__('Second Box Text', 'kavehTheme'),
                    'default' => esc_html__('Standard shipping cost applies for regular users', 'kavehTheme'),
                    'dependency' => array(
                        array( 'kvideo_ersalrygan_single', '==', 1 ),
                        array( 'ksingle_options_off', '==', 'false' ),

                    ),
                ),
                array(
                    'id'           => 'kvideo_ersalrygan_single_image',
                    'type'         => 'upload',
                    'title'        => esc_html__('Left Side Box Image', 'kavehTheme'),
                    'library'      => 'image',
                    'placeholder'  => 'http://',
                    'button_title' => esc_html__('Upload Image', 'kavehTheme'),
                    'remove_title' => esc_html__('Remove Image', 'kavehTheme'),
                    'dependency' => array(
                        array( 'kvideo_ersalrygan_single', '==', '1' ),
                        array( 'ksingle_options_off', '==', 'false' ),
                    ),
                    'default' => esc_url( KAVEH_TEMPLATE_URI ) . '/assets/images/single-pr/date-send.svg',
                ),
                array(
                    'id'        => 'ktickets_single',
                    'type'      => 'repeater',
                    'title'     => esc_html__('Custom Boxes Under Description', 'kavehTheme'),
                    'dependency' => array(
                        array( 'ksingle_options_off', '==', 'false' ),
                      ),
                    'fields'    => array(
                        array(
                            'id'           => 'ktickets_single_image',
                            'type'         => 'upload',
                            'title'        => esc_html__('Box Image', 'kavehTheme'),
                            'library'      => 'image',
                            'placeholder'  => 'http://',
                            'button_title' => esc_html__('Upload Image', 'kavehTheme'),
                            'remove_title' => esc_html__('Remove Image', 'kavehTheme'),
                            
                        ),
                        array(
                            'id'           => 'ktickets_single_text1',
                            'type'         => 'text',
                            'title'        => esc_html__('First Box Text', 'kavehTheme'),
                        ),
                        array(
                            'id'           => 'ktickets_single_text2',
                            'type'         => 'text',
                            'title'        => esc_html__('Second Box Text', 'kavehTheme'),
                        ),
                        array(
                            'id'           => 'ktickets_single_link',
                            'type'         => 'text',
                            'title'        => esc_html__('Box Link', 'kavehTheme'),
                        ),
                        array(
                            'id'    => 'ktickets_single_color1',
                            'type'  => 'color',
                            'title' => esc_html__('Box Background Color', 'kavehTheme'),
                        ),
                        array(
                            'id'    => 'ktickets_single_color2',
                            'type'  => 'color',
                            'title' => esc_html__('Image Box Color', 'kavehTheme'),
                        ),
                        array(
                            'id'    => 'ktickets_single_color3',
                            'type'  => 'color',
                            'title' => esc_html__('Text Color', 'kavehTheme'),
                        ),
          
                ),
                    'default'   => array(
                        array(
                            'ktickets_single_image' => esc_url( KAVEH_TEMPLATE_URI ) . '/assets/images/single-pr/snappaylogo.png',
                            'ktickets_single_text1' => esc_html__('Installment payment with SnapPay', 'kavehTheme'),
                            'ktickets_single_text2' => esc_html__('No need for check or promissory note guarantee', 'kavehTheme'),
                            'ktickets_single_link'  => '#',
                            'ktickets_single_color1' => '#008efa',
                            'ktickets_single_color2' => '#096db9',
                            'ktickets_single_color3' => '#fff',
                        ),
                        array(
                            'ktickets_single_image' => esc_url( KAVEH_TEMPLATE_URI ) . '/assets/images/single-pr/tara.svg',
                            'ktickets_single_text1' => esc_html__('Credit payment with Tara', 'kavehTheme'),
                            'ktickets_single_text2' => esc_html__('Buy now, pay at the end of the month', 'kavehTheme'),
                            'ktickets_single_link'  => '#',
                            'ktickets_single_color1' => '#581f81',
                            'ktickets_single_color2' => '#f9abe0',
                            'ktickets_single_color3' => '#fff',
                        ),
                    )
                ),
                array(
                'id'        => 'kticketsthumnail_single',
                'type'      => 'repeater',
                'title'     => esc_html__('Custom Boxes Under Images', 'kavehTheme'),
                'dependency' => array(
                    array( 'ksingle_options_off', '==', 'false' ),
                  ),
                'fields'    => array(
                    array(
                        'id'           => 'kticketsthumnail_single_image',
                        'type'         => 'upload',
                        'title'        => esc_html__('Box Image', 'kavehTheme'),
                        'library'      => 'image',
                        'placeholder'  => 'http://',
                        'button_title' => esc_html__('Upload Image', 'kavehTheme'),
                        'remove_title' => esc_html__('Remove Image', 'kavehTheme'),
                        
                    ),
                    array(
                        'id'           => 'kticketsthumnail_single_text1',
                        'type'         => 'text',
                        'title'        => esc_html__('Box Text', 'kavehTheme'),
                    ),
                    array(
                        'id'           => 'kticketsthumnail_single_link',
                        'type'         => 'text',
                        'title'        => esc_html__('Box Link', 'kavehTheme'),
                    ),
                    array(
                        'id'    => 'kticketsthumnail_single_color1',
                        'type'  => 'color',
                        'title' => esc_html__('Box Background Color', 'kavehTheme'),
                    ),
                    array(
                        'id'    => 'kticketsthumnail_single_color2',
                        'type'  => 'color',
                        'title' => esc_html__('Image Box Color', 'kavehTheme'),
                    ),
                    array(
                        'id'    => 'kticketsthumnail_single_color3',
                        'type'  => 'color',
                        'title' => esc_html__('Text Color', 'kavehTheme'),
                    ),
          
                ),
                'default'   => array(
                    array(
                    'kticketsthumnail_single_image' => esc_url( KAVEH_TEMPLATE_URI ) . '/assets/images/single-pr/telegram.png',
                    'kticketsthumnail_single_text1' => esc_html__('Connect via Telegram', 'kavehTheme'),
                    'kticketsthumnail_single_link' => '#',
                    'kticketsthumnail_single_color1' => '#40b1e3',
                    'kticketsthumnail_single_color2' => '#0090c7',
                    'kticketsthumnail_single_color3' => '#fff',
                    ),
                    array(
                    'kticketsthumnail_single_image' => esc_url( KAVEH_TEMPLATE_URI ) . '/assets/images/single-pr/whatsapp.png',
                    'kticketsthumnail_single_text1' => esc_html__('Connect via WhatsApp', 'kavehTheme'),
                    'kticketsthumnail_single_link' => '#',
                    'kticketsthumnail_single_color1' => '#41bf55',
                    'kticketsthumnail_single_color2' => '#32a346',
                    'kticketsthumnail_single_color3' => '#fff',
                    ),
                    array(
                    'kticketsthumnail_single_image' => esc_url( KAVEH_TEMPLATE_URI ) . '/assets/images/single-pr/rubika.png',
                    'kticketsthumnail_single_text1' => esc_html__('Connect via Rubika', 'kavehTheme'),
                    'kticketsthumnail_single_link' => '#',
                    'kticketsthumnail_single_color1' => '#fe6600',
                    'kticketsthumnail_single_color2' => '#fff',
                    'kticketsthumnail_single_color3' => '#fff',
                    ),
                    array(
                    'kticketsthumnail_single_image' => esc_url( KAVEH_TEMPLATE_URI ) . '/assets/images/single-pr/bale.png',
                    'kticketsthumnail_single_text1' => esc_html__('Connect via Bale', 'kavehTheme'),
                    'kticketsthumnail_single_link' => '#',
                    'kticketsthumnail_single_color1' => '#01b894',
                    'kticketsthumnail_single_color2' => '#04f2c4',
                    'kticketsthumnail_single_color3' => '#fff',
                    ),
            
                )
                ),
                array(
                'id'        => 'klis_single',
                'type'      => 'repeater',
                'dependency' => array(
                    array( 'ksingle_options_off', '==', 'false' ),
                  ),
                'title'     => esc_html__('Product List Features', 'kavehTheme'),
                'fields'    => array(
                    array(
                        'id'           => 'klis_single_icon',
                        'type'         => 'text',
                        'title'        => esc_html__('Icon', 'kavehTheme'),
                    ),
                    array(
                        'id'           => 'klis_single_text1',
                        'type'         => 'text',
                        'title'        => esc_html__('First Text Part', 'kavehTheme'),
                    ),
                    array(
                        'id'           => 'klis_single_text2',
                        'type'         => 'text',
                        'title'        => esc_html__('Second Text Part', 'kavehTheme'),
                    ),
                ),
                'default'   => array(
                    array(
                        'klis_single_icon' => 'far fa-dot-circle',
                        'klis_single_text1' => esc_html__('Ship from', 'kavehTheme'),
                        'klis_single_text2' => esc_html__('2 business days', 'kavehTheme'),
                    ),
                    array(
                        'klis_single_icon' => 'far fa-dot-circle',
                        'klis_single_text1' => esc_html__('Express shipping', 'kavehTheme'),
                        'klis_single_text2' => esc_html__('In Tehran city', 'kavehTheme'),
                    ),
                    array(
                        'klis_single_icon' => 'far fa-dot-circle',
                        'klis_single_text1' => esc_html__('Warranty', 'kavehTheme'),
                        'klis_single_text2' => esc_html__('Authenticity and physical condition', 'kavehTheme'),
                    ),
                )
                ),

            ));

            $this->add_section(esc_html__('Comments Meta', 'kavehTheme'), array(
                array(
                    'id' => 'opt-textarea',
                    'type' => 'textarea',
                    'title' => esc_html__('Simple Textarea', 'kavehTheme'),
                ),
            ));
        }

        public function add_section($title, $fields)
        {

            if (!class_exists('CSF')) {
                return;
            }
            CSF::createSection($this->prefix, array(
                'title' => $title,
                'fields' => $fields,
            ));
        }

}
