<?php

/**
 *
 * @package esignRatingWidget
 * @author  Arafat Rahman <arafatrahmank@gmail.com>
 */
if (!class_exists('esignRatingWidgetFormidable')) :

    class esignRatingWidgetFormidable{

        /**
         * Instance of this class.
         * @since    1.0.1
         * @var      object
         */
        protected static $instance = null;
        public $name;
        private $feedbackURL,$rattingURL;

        /**
         * Slug of the plugin screen.
         * @since    1.0.1
         * @var      string
         */
        protected $plugin_screen_hook_suffix = null;

        /**
         * Initialize the plugin by loading admin scripts & styles and adding a
         * settings page and menu.
         * @since     0.1
         */
        public function __construct() {
             /*
             * Call $plugin_slug from public plugin class.
             */

            $this->feedbackURL = 'https://www.approveme.com/plugin-feedback/';
            $this->rattingURL = 'https://wordpress.org/support/plugin/forms-signature-formidable-online-contract-automation/reviews/#new-post';
            
            add_action('esig_admin_notices', array($this, 'esignRatingWidget'));
            add_action('admin_enqueue_scripts', array($this, 'enqueueAdminStyles'));
            add_action('admin_enqueue_scripts', array($this, 'enqueueAdminScripts'));
            add_action('wp_ajax_esig_formidable_ratting_widget_remove', array($this, 'esigFormidableRattingWidgetRemove'));
        
        }

        public function esigFormidableRattingWidgetRemove() {
            
            // Verify nonce for security
            if (!check_ajax_referer('esig_formidableform_fields', 'esig_formidableform_nonce', false)) {
                wp_send_json_error(array('message' => __('Security check failed. Please refresh the page and try again.', 'esig-formidableform')));
                return;
            }

            // Check user capabilities
            if (!current_user_can('manage_options')) {
                wp_send_json_error(array('message' => __('You do not have permission to perform this action.', 'esig-formidableform')));
                return;
            }

            // Check E-Signature plugin is available
            if (!function_exists('WP_E_Sig')) {
                wp_send_json_error(array('message' => __('E-Signature plugin is not available.', 'esig-formidableform')));
                return;
            }

            // Check current user is e-signature sender
            if (!WP_E_Sig()->user->checkEsigAdmin(get_current_user_id())) {
                wp_send_json_error(array('message' => __('You are not authorized to perform this action.', 'esig-formidableform')));
                return;
            }

            // Update option with proper sanitization
            $result = update_option('remove_rating_widget_formidable', 'Yes');
            
            if ($result) {
                wp_send_json_success(array('message' => __('Rating widget hidden successfully.', 'esig-formidableform')));
            } else {
                wp_send_json_error(array('message' => __('Failed to update settings.', 'esig-formidableform')));
            }
        }
        
         public function enqueueAdminStyles() {
            $screen = get_current_screen();
            $current = $screen->id;
            
            if (($current == 'toplevel_page_esign-docs')) {
                wp_enqueue_style('esig-formidable-rating-widget-admin-styles', plugins_url('assets/css/esign-rating-widget.css', __FILE__), array(), '0.1.1');
            }
        }


        public function enqueueAdminScripts() {



            $screen = get_current_screen();
            $current = $screen->id;          

            
            if (($current == 'toplevel_page_esign-docs')) {
              
                 wp_enqueue_script('formidable-rating-widget-admin-script', plugins_url('assets/js/rating-widget-control.js', __FILE__), array('jquery', 'jquery-ui-dialog'), '0.1.1', true);
                 
                 // Localize script with nonce for AJAX security
                 wp_localize_script('formidable-rating-widget-admin-script', 'esigFormidableAjax', array(
                     'ajaxurl' => admin_url('admin-ajax.php'),
                     'esig_formidableform_nonce' => wp_create_nonce('esig_formidableform_fields')
                 ));
            }

        }
        
        
        
        public static function checkSignedDoc($metakey){
            
            $alldocid = WP_E_Sig()->meta->getall_bykey($metakey);
            $arrayValue = json_decode(json_encode($alldocid),true);

            $signature = 0;
            foreach ($arrayValue as $value) {
                

                $getStatus = WP_E_Sig()->document->getStatus($value['document_id']);
                
                if($getStatus == 'signed'){
                    $signature++;
                }
            }

            if($signature >= 5) return true;
            
            return false;
           
           
            
        }

 
        
        
        public function esignRatingWidget(){
            
            
            
             if (!function_exists('WP_E_Sig')) return false;
            
              $screen = get_current_screen();
                           
              if( $screen->id != 'toplevel_page_esign-docs') return false;

              $checkWidget = get_option('remove_rating_widget_formidable');          

              if($checkWidget == "Yes") return false;
              
              $checkRequierment = self::checkSignedDoc('esig_formidable_entry_id');

              if(!wp_validate_boolean($checkRequierment)) return false;

              $api = new WP_E_Api();

              $data = array("form_name" => 'Formidable Form',"feedback_url"=>$this->feedbackURL,"plugin_url"=>$this->rattingURL);
              $displayNotice = dirname(__FILE__) . '/views/esig-ratting-widget-view.php';
              $api->view->renderPartial('', $data, true, '', $displayNotice);
          
        }
        
         /**
         * Return an instance of this class.
         * @since     0.1
         * @return    object    A single instance of this class.
         */
        public static function get_instance() {

            // If the single instance hasn't been set, set it now.
            if (null == self::$instance) {
                self::$instance = new self;
            }

            return self::$instance;
        }
        

        

    }

    

    
endif;

