<?php
namespace WPVOFW;
use WPVOFW\Feedback;
use WPVOFW\Submission;
use WPVOFW\Pro\Admin\Settings;

class Frontend{

    public function __construct()
    {
        //display feedback form on thankyou page
        // add_action( 'woocommerce_thankyou', array($this,'display_feedback_on_thankyou'), 20 );

        $this->apply_thankyou_hooks();

        //displaying feedback form on order-details page
        $this->apply_order_details_hooks();
        
        add_action('wp_enqueue_scripts', [$this, 'enqueue_scripts']);

        new Submission();
    }

    // Enqueue scripts
    public function enqueue_scripts(){

        if (is_order_received_page() || is_view_order_page()) {

            global $wp;
            if(is_order_received_page()){
                $order_id = absint( $wp->query_vars['order-received'] );
            }
            else{
                // for view order page
                $order_id = absint( get_query_var('view-order') );
            }

            // Get order meta data
            $order_meta = get_post_meta( $order_id, 'feedback_form_submitted', true);
            if($order_meta ){
                return;
            }
            $current_url = home_url( add_query_arg( array(), $wp->request ) );
            wp_enqueue_style('wpvofw-feedback-form-style', WPVOFW_URL . 'assets/css/frontend.css',[], '0.1');
            wp_enqueue_script('wpvofw-feedback-form-script', WPVOFW_URL . 'assets/js/frontend.js',['jquery'], '0.1', true);
            wp_localize_script('wpvofw-feedback-form-script', 'feedback_form_data', array(
                'ajaxurl' => admin_url('admin-ajax.php'),
                'currentpageurl' => $current_url,
            ));
        }
    }

    function render_feedback_form( $order_id){
        
        $order = wc_get_order($order_id);

        $form_submitted = $order->get_meta('feedback_form_submitted',true);
        
        if($form_submitted){
            return false;
        }
        ob_start();
        ?>
            <div class="wpvofw-feedback-container wpvofw-type-floating" id="wpvofw-feedback-container">
                <div class = "wpvofw-topbar">
                    <h3 class = "wpvofw-heading">
                        <?php echo esc_html_e('Feedback', 'order-feedback-woo');?>
                    </h3>
                    <span class = "wpvofw-close-btn">
                        <svg width="800px" height="800px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                        <path d="M7 10L12 15L17 10" stroke="#000000" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
                        </svg>
                    </span>
                </div>
                <div class = "wpvofw-content">
                    <span class ="wpvofw-info"><?php echo esc_html_e('Please share your feedback about this order.', 'order-feedback-woo');?></span>
                    <form id="wpvofw-order-feedback-form" method="post" class="wpvofw-order-feedback-form">
                        <div class = "wpvofw-fields-container">
                            <label for="wpvofw-title" class="wpvofw-feedback-label"><?php echo esc_html_e('Title', 'order-feedback-woo');?></label>
                            <input type="text" class = "wpvofw-feedback-input" name="title" id="wpvofw-title" placeholder="<?php echo esc_html_e('Enter your title', 'order-feedback-woo');?>" required>
                        </div>

                        <div class = "wpvofw-fields-container">
                            <label for="wpvofw-message" class="wpvofw-feedback-label"><?php echo esc_html_e('Message', 'order-feedback-woo');?></label>
                            <textarea id="wpvofw-message" class = "wpvofw-feedback-input" name="message" placeholder="<?php echo esc_html_e('Enter your message', 'order-feedback-woo');?>" rows="3" required></textarea>
                        </div>

                        <div class = "wpvofw-fields-container">
                            <label for="wpvofw-rating" class="wpvofw-feedback-label"><?php echo esc_html_e('Rate your Experience', 'order-feedback-woo');?></label>
                                <select class = "wpvofw-feedback-input" name="rating" id="wpvofw-rating" required>
                                    <option value="1">1</option>
                                    <option value="2">2</option>
                                    <option value="3">3</option>
                                    <option value="4">4</option>
                                    <option value="5" selected>5</option>
                                </select>
                            </label>
                        </div>
                        <input type = "hidden" name = "order_id" value = "<?php echo esc_attr($order_id); ?>"/>
                        <?php wp_nonce_field( 'wpvofw_thankyou', 'wpvofw_thankyou_nonce' ); ?>

                        <div class = "wpvofw-fields-container">
                            <input type="submit" class="wpvofw-button" id="wpvofw-submit" name="submit_feedback" value="<?php echo esc_html_e('Submit Feedback', 'order-feedback-woo');?>">
                        </div>
                    </form>
                </div>
            </div>
        <?php
        $form_html = ob_get_clean();
        return $form_html;
    }

    function apply_thankyou_hooks(){
            $position = apply_filters('wpvofw/thankyou/form_position', __return_empty_array());
            if(!empty($position) ){
                $actions = $position['action'];
                $form_position = $position['form_position'];

                if (isset($actions[$form_position])) {
                    add_action($actions[ $form_position]['hook'], [$this, 'display_feedback_on_thankyou'], $actions[$form_position]['priority']);
                }

            }
            else{
                add_action( 'woocommerce_thankyou', array($this,'display_feedback_on_thankyou'),10 );
            }
    }
    function apply_order_details_hooks(){

            $position = apply_filters('wpvofw/order_details/form_position', __return_empty_array());
            if(!empty($position) ){
                $actions = $position['action'];
                $form_position = $position['form_position'];

                if (isset($actions[$form_position])) {
                    add_action($actions[ $form_position]['hook'], [$this, 'display_feedback_on_order_details'], $actions[$form_position]['priority']);
                }
            }
            else{
                add_action( 'woocommerce_view_order', array($this,'display_feedback_on_order_details'),11 );
            }
    }
    function display_feedback_on_thankyou($order){
        $show_form = apply_filters('wpvofw/thankyou/render_form', __return_true());

        if(!$show_form){
            return;
        }

        if(is_int($order)){
            $order_id = $order;
        }
        else{
            $order_id = $order->get_id();
        }
        if(is_order_received_page()){
            $form_html = $this->render_feedback_form($order_id);

            $html = apply_filters('wpvofw/feedbackform', $form_html, $order_id);
            echo $html;

        }
    }
    function display_feedback_on_order_details($order){
        $show_form = apply_filters('wpvofw/order_details/render_form', __return_true());

        if(!$show_form){
            return;
        }
        if(is_int($order)){
            $order_id = $order;
        }
        else{
            $order_id = $order->get_id();
        }
        // Get order meta data
        $order_meta = get_post_meta( $order_id, 'feedback_form_submitted', true);
        //checking if page is view order
        if ( is_view_order_page()) {
            if(!$order_meta){
                $form_html = $this->render_feedback_form($order_id);

                $html = apply_filters('wpvofw/feedbackform', $form_html, $order_id);
                echo $html;

            }
        }
    }

    public function validate_form(){
        
        $valid = true;

        if ( ! isset( $_POST['title'] ) || empty( $_POST['title'] ) ) {
            $valid = false;
        }
        if ( ! isset( $_POST['message'] ) || empty( $_POST['message'] ) ) {
            $valid = false;
        }
        if ( ! isset( $_POST['rating'] ) || empty( $_POST['rating'] ) ) {
            $valid = false;
        }

        $valid = apply_filters('wpvofw/validate_form', $valid, $_POST);
        
        return $valid;
    }
}
?>
