<?php

namespace WeDevs\Dokan\Vendor;

use WC_Countries;
use WeDevs\Dokan\Admin\SetupWizard as DokanSetupWizard;

/**
 * Seller setup wizard class
 */
class SetupWizard extends DokanSetupWizard {
    /** @var string Currenct Step */
    protected $step = '';

    /** @var array Steps for the setup wizard */
    protected $steps = [];

    /** @var string custom logo url of the theme */
    protected $custom_logo = '';

    /**
     * Hook in tabs.
     */
    public function __construct() {
        add_filter( 'woocommerce_registration_redirect', [ $this, 'filter_woocommerce_registration_redirect' ], 10, 1 );
        add_action( 'init', [ $this, 'setup_wizard' ], 9999 );
        add_action( 'dokan_setup_wizard_enqueue_scripts', [ $this, 'frontend_enqueue_scripts' ] );
    }

    // define the woocommerce_registration_redirect callback
    public function filter_woocommerce_registration_redirect( $var ) {
        $url  = $var;
        $user = wp_get_current_user();

        if ( in_array( 'seller', $user->roles, true ) ) {
            $url = dokan_get_navigation_url();

            if ( 'off' === dokan_get_option( 'disable_welcome_wizard', 'dokan_selling', 'off' ) ) {
                $url = apply_filters( 'dokan_seller_setup_wizard_url', site_url( '?page=dokan-seller-setup' ) );
            }
        }

        return $url;
    }

    /**
     * Show the setup wizard.
     */
    public function setup_wizard() {
        if ( empty( $_GET['page'] ) || 'dokan-seller-setup' !== $_GET['page'] ) { // phpcs:ignore
            return;
        }

        if ( ! is_user_logged_in() ) {
            return;
        }

        $this->custom_logo     = null;
        $setup_wizard_logo_url = dokan_get_option( 'setup_wizard_logo_url', 'dokan_general', '' );

        if ( ! empty( $setup_wizard_logo_url ) ) {
            $this->custom_logo = $setup_wizard_logo_url;
        }

        $this->store_id   = dokan_get_current_user_id();
        $this->store_info = dokan_get_store_info( $this->store_id );

        $steps = [
            'introduction' => [
                'name'    => __( 'Introduction', 'dokan-lite' ),
                'view'    => [ $this, 'dokan_setup_introduction' ],
                'handler' => '',
            ],
            'store'        => [
                'name'    => __( 'Store', 'dokan-lite' ),
                'view'    => [ $this, 'dokan_setup_store' ],
                'handler' => [ $this, 'dokan_setup_store_save' ],
            ],
            'payment'      => [
                'name'    => __( 'Payment', 'dokan-lite' ),
                'view'    => [ $this, 'dokan_setup_payment' ],
                'handler' => [ $this, 'dokan_setup_payment_save' ],
            ],
            'next_steps'   => [
                'name'    => __( 'Ready!', 'dokan-lite' ),
                'view'    => [ $this, 'dokan_setup_ready' ],
                'handler' => '',
            ],
        ];

        $this->steps = apply_filters( 'dokan_seller_wizard_steps', $steps );
        $this->step  = current( array_keys( $this->steps ) );

        // get step from url
        if ( isset( $_GET['_admin_sw_nonce'], $_GET['step'] ) && wp_verify_nonce( sanitize_key( wp_unslash( $_GET['_admin_sw_nonce'] ) ), 'dokan_admin_setup_wizard_nonce' ) ) {
            $this->step = sanitize_key( wp_unslash( $_GET['step'] ) );
        }

        if ( ! empty( $_POST['save_step'] ) && isset( $this->steps[ $this->step ]['handler'] ) ) { // WPCS: CSRF ok.
            call_user_func( $this->steps[ $this->step ]['handler'] );
        }

        $this->enqueue_scripts();
        ob_start();
        $this->set_setup_wizard_template();
        exit;
    }

    /**
     * Enqueue vendor setup wizard scripts
     *
     * @since 3.7.0
     *
     * @return void
     */
    public function frontend_enqueue_scripts() {
        wp_enqueue_script( 'jquery' );
        wp_enqueue_script( 'jquery-tiptip' );
        wp_enqueue_script( 'jquery-blockui' );
        wp_enqueue_script( 'wc-enhanced-select' );
    }

    /**
     * Setup Wizard Header.
     */
    public function setup_wizard_header() {
        ?>
        <!DOCTYPE html>
        <html <?php language_attributes(); ?>>
        <head>
            <meta name="viewport" content="width=device-width"/>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
            <title><?php esc_attr_e( 'Vendor &rsaquo; Setup Wizard', 'dokan-lite' ); ?></title>
            <?php wp_print_scripts(); ?>
            <?php wp_print_styles(); ?>
            <?php do_action( 'dokan_setup_wizard_styles' ); ?>
        </head>
        <body class="wc-setup wp-core-ui dokan-vendor-setup-wizard">
        <?php if ( ! empty( $this->custom_logo ) ) { ?>
            <h1 id="wc-logo">
                <a href="<?php echo esc_url( home_url() ); ?>">
                    <img src="<?php echo esc_url( $this->custom_logo ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>"/>
                </a>
            </h1>
            <?php
        } else {
            echo '<h1 id="wc-logo">' . esc_attr( get_bloginfo( 'name' ) ) . '</h1>';
        }
    }

    /**
     * Setup Wizard Footer.
     */
    public function setup_wizard_footer() {
        ?>
        <?php if ( 'next_steps' === $this->step ) : ?>
            <a class="wc-return-to-dashboard" href="<?php echo esc_url( site_url() ); ?>"><?php esc_attr_e( 'Return to the Marketplace', 'dokan-lite' ); ?></a>
        <?php endif; ?>
        </body>
        </html>
        <?php
    }

    /**
     * Introduction step.
     */
    public function dokan_setup_introduction() {
        $dashboard_url        = dokan_get_navigation_url();
        $default_message      = wp_kses_post( __( '<p>Thank you for choosing The Marketplace to power your online store! This quick setup wizard will help you configure the basic settings. <strong>It’s completely optional and shouldn’t take longer than two minutes.</strong></p>', 'dokan-lite' ) );
        $setup_wizard_message = dokan_get_option( 'setup_wizard_message', 'dokan_general', $default_message );
        ?>
        <h1><?php esc_attr_e( 'Welcome to the Marketplace!', 'dokan-lite' ); ?></h1>
        <?php if ( ! empty( $setup_wizard_message ) ) : ?>
            <div><?php printf( '%s', $setup_wizard_message ); ?></div>
        <?php endif; ?>
        <p><?php esc_attr_e( 'No time right now? If you don’t want to go through the wizard, you can skip and return to the Store!', 'dokan-lite' ); ?></p>
        <p class="wc-setup-actions step">
            <a href="<?php echo esc_url( $this->get_next_step_link() ); ?>" class="button-primary button button-large button-next lets-go-btn dokan-btn-theme"><?php esc_attr_e( 'Let\'s Go!', 'dokan-lite' ); ?></a>
            <a href="<?php echo esc_url( $dashboard_url ); ?>" class="button button-large not-right-now-btn dokan-btn-theme"><?php esc_attr_e( 'Not right now', 'dokan-lite' ); ?></a>
        </p>
        <?php
        do_action( 'dokan_seller_wizard_introduction', $this );
    }

    /**
     * Store step.
     */
    public function dokan_setup_store() {
        $store_info = $this->store_info;

        $store_ppp       = isset( $store_info['store_ppp'] ) ? absint( $store_info['store_ppp'] ) : (int) dokan_get_option( 'store_products_per_page', 'dokan_general', 12 );
        $show_email      = isset( $store_info['show_email'] ) ? esc_attr( $store_info['show_email'] ) : 'no';
        $address_street1 = isset( $store_info['address']['street_1'] ) ? $store_info['address']['street_1'] : '';
        $address_street2 = isset( $store_info['address']['street_2'] ) ? $store_info['address']['street_2'] : '';
        $address_city    = isset( $store_info['address']['city'] ) ? $store_info['address']['city'] : '';
        $address_zip     = isset( $store_info['address']['zip'] ) ? $store_info['address']['zip'] : '';
        $address_country = isset( $store_info['address']['country'] ) ? $store_info['address']['country'] : '';
        $address_state   = isset( $store_info['address']['state'] ) ? $store_info['address']['state'] : '';

        $country_obj = new WC_Countries();
        $countries   = $country_obj->countries;
        $states      = $country_obj->states;
        ?>
        <h1><?php esc_attr_e( 'Store Setup', 'dokan-lite' ); ?></h1>
        <form method="post" class="dokan-seller-setup-form">
            <table class="form-table">
                <tr>
                    <th scope="row"><label for="store_ppp"><?php esc_attr_e( 'Store Products Per Page', 'dokan-lite' ); ?></label></th>
                    <td>
                        <input type="text" id="store_ppp" name="store_ppp" value="<?php echo esc_attr( $store_ppp ); ?>"/>
                    </td>
                </tr>
                <tr>
                    <th scope="row"><label for="address[street_1]"><?php esc_html_e( 'Street', 'dokan-lite' ); ?></label></th>
                    <td>
                        <input type="text" id="address[street_1]" name="address[street_1]" value="<?php echo esc_attr( $address_street1 ); ?>"/>
                    </td>
                </tr>
                <tr>
                    <th scope="row"><label for="address[street_2]"><?php esc_html_e( 'Street 2', 'dokan-lite' ); ?></label></th>
                    <td>
                        <input type="text" id="address[street_2]" name="address[street_2]" value="<?php echo esc_attr( $address_street2 ); ?>"/>
                    </td>
                </tr>
                <tr>
                    <th scope="row"><label for="address[city]"><?php esc_html_e( 'City', 'dokan-lite' ); ?></label></th>
                    <td>
                        <input type="text" id="address[city]" name="address[city]" value="<?php echo esc_attr( $address_city ); ?>"/>
                    </td>
                </tr>
                <th scope="row"><label for="address[zip]"><?php esc_html_e( 'Post/Zip Code', 'dokan-lite' ); ?></label></th>
                <td>
                    <input type="text" id="address[zip]" name="address[zip]" value="<?php echo esc_attr( $address_zip ); ?>"/>
                </td>
                <tr>
                    <th scope="row"><label for="address[country]"><?php esc_html_e( 'Country', 'dokan-lite' ); ?></label></th>
                    <td>
                        <select name="address[country]" class="wc-enhanced-select country_to_state" id="address[country]" style="width: 100%;">
                            <?php dokan_country_dropdown( $countries, $address_country, false ); ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <th scope="row"><label for="calc_shipping_state"><?php esc_html_e( 'State', 'dokan-lite' ); ?></label></th>
                    <td>
                        <input type="text" id="calc_shipping_state" name="address[state]" value="<?php echo esc_attr( $address_state ); ?>" / placeholder="<?php esc_attr_e( 'State Name', 'dokan-lite' ); ?>">
                    </td>
                </tr>

                <?php do_action( 'dokan_seller_wizard_store_setup_after_address_field', $this ); ?>

                <tr>
                    <th scope="row"><label for="show_email"><?php esc_html_e( 'Email', 'dokan-lite' ); ?></label></th>
                    <td class="checkbox">
                        <input type="checkbox" name="show_email" id="show_email" class="switch-input" value="1" <?php echo ( $show_email === 'yes' ) ? 'checked="true"' : ''; ?>>
                        <label for="show_email">
                            <?php esc_html_e( 'Show email address in store', 'dokan-lite' ); ?>
                        </label>
                    </td>
                </tr>

                <?php do_action( 'dokan_seller_wizard_store_setup_field', $this ); ?>

            </table>
            <p class="wc-setup-actions step">
                <input type="submit" class="button-primary button button-large button-next store-step-continue dokan-btn-theme" value="<?php esc_attr_e( 'Continue', 'dokan-lite' ); ?>" name="save_step"/>
                <a href="<?php echo esc_url( $this->get_next_step_link() ); ?>" class="button button-large button-next store-step-skip-btn dokan-btn-theme"><?php esc_html_e( 'Skip this step', 'dokan-lite' ); ?></a>
                <?php wp_nonce_field( 'dokan-seller-setup' ); ?>
            </p>
        </form>
        <script>
            (function ($) {
                var states = <?php echo wp_json_encode( $states ); ?>;

                $('body').on('change', 'select.country_to_state, input.country_to_state', function () {
                    // Grab wrapping element to target only stateboxes in same 'group'
                    var $wrapper = $(this).closest('form.dokan-seller-setup-form');

                    var country = $(this).val(),
                        $statebox = $wrapper.find('#calc_shipping_state'),
                        $parent = $statebox.closest('tr'),
                        input_name = $statebox.attr('name'),
                        input_id = $statebox.attr('id'),
                        value = $statebox.val(),
                        placeholder = $statebox.attr('placeholder') || $statebox.attr('data-placeholder') || '',
                        state_option_text = '<?php echo esc_attr__( 'Select an option&hellip;', 'dokan-lite' ); ?>';

                    if (states[country]) {
                        if ($.isEmptyObject(states[country])) {
                            $statebox.closest('tr').hide().find('.select2-container').remove();
                            $statebox.replaceWith('<input type="hidden" class="hidden" name="' + input_name + '" id="' + input_id + '" value="" placeholder="' + placeholder + '" />');

                            $(document.body).trigger('country_to_state_changed', [country, $wrapper]);

                        } else {

                            var options = '',
                                state = states[country];

                            for (var index in state) {
                                if (state.hasOwnProperty(index)) {
                                    options = options + '<option value="' + index + '">' + state[index] + '</option>';
                                }
                            }

                            $statebox.closest('tr').show();

                            if ($statebox.is('input')) {
                                // Change for select
                                $statebox.replaceWith('<select name="' + input_name + '" id="' + input_id + '" class="wc-enhanced-select state_select" data-placeholder="' + placeholder + '"></select>');
                                $statebox = $wrapper.find('#calc_shipping_state');
                            }

                            $statebox.html('<option value="">' + state_option_text + '</option>' + options);
                            $statebox.val(value).trigger('change');

                            $(document.body).trigger('country_to_state_changed', [country, $wrapper]);

                        }
                    } else {
                        if ($statebox.is('select')) {

                            $parent.show().find('.select2-container').remove();
                            $statebox.replaceWith('<input type="text" class="input-text" name="' + input_name + '" id="' + input_id + '" placeholder="' + placeholder + '" />');

                            $(document.body).trigger('country_to_state_changed', [country, $wrapper]);

                        } else if ($statebox.is('input[type="hidden"]')) {

                            $parent.show().find('.select2-container').remove();
                            $statebox.replaceWith('<input type="text" class="input-text" name="' + input_name + '" id="' + input_id + '" placeholder="' + placeholder + '" />');

                            $(document.body).trigger('country_to_state_changed', [country, $wrapper]);

                        }
                    }

                    $(document.body).trigger('country_to_state_changing', [country, $wrapper]);
                    $('.wc-enhanced-select').select2();
                });

                $(':input.country_to_state').trigger('change');

            })(jQuery);

        </script>
        <style>
            .select2-container--open .select2-dropdown {
                left: 20px;
            }
        </style
        <?php

        do_action( 'dokan_seller_wizard_after_store_setup_form', $this );
    }

    /**
     * Save store options.
     */
    public function dokan_setup_store_save() {
        if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'dokan-seller-setup' ) ) {
            return;
        }

        $dokan_settings = $this->store_info;

        $dokan_settings['store_ppp']  = isset( $_POST['store_ppp'] ) ? absint( $_POST['store_ppp'] ) : '';
        $dokan_settings['address']    = isset( $_POST['address'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['address'] ) ) : [];
        $dokan_settings['show_email'] = isset( $_POST['show_email'] ) ? 'yes' : 'no';

        // Check address and add manually values on Profile Completion also increase progress value
        if ( $dokan_settings['address'] ) {
            $dokan_settings['profile_completion']['address'] = 10;
            $profile_settings                                = get_user_meta( $this->store_id, 'dokan_profile_settings', true );

            if ( ! empty( $profile_settings['profile_completion']['progress'] ) ) {
                $dokan_settings['profile_completion']['progress'] = $profile_settings['profile_completion']['progress'] + 10;
            }
        }

        update_user_meta( $this->store_id, 'dokan_profile_settings', $dokan_settings );

        do_action( 'dokan_seller_wizard_store_field_save', $this );

        wp_safe_redirect( esc_url_raw( $this->get_next_step_link() ) );
        exit;
    }

    /**
     * Payment step.
     */
    public function dokan_setup_payment() {
        $methods    = dokan_withdraw_get_active_methods();
        $store_info = $this->store_info;
        ?>
        <h1><?php esc_html_e( 'Payment Setup', 'dokan-lite' ); ?></h1>
        <form method="post" novalidate>
            <table class="form-table">
                <?php
                foreach ( $methods as $method_key ) {
                    $method = dokan_withdraw_get_method( $method_key );
                    if ( isset( $method['callback'] ) && is_callable( $method['callback'] ) ) {
                        ?>
                        <tr>
                            <th scope="row"><label><?php echo esc_html( dokan_withdraw_get_method_title( $method_key ) ); ?></label></th>
                            <td>
                                <?php call_user_func( $method['callback'], $store_info ); ?>
                            </td>
                        </tr>
                        <?php
                    }
                }

                do_action( 'dokan_seller_wizard_payment_setup_field', $this );
                ?>
            </table>
            <p class="wc-setup-actions step">
                <input type="submit" class="button-primary button button-large button-next payment-continue-btn dokan-btn-theme" value="<?php esc_attr_e( 'Continue', 'dokan-lite' ); ?>" name="save_step"/>
                <a href="<?php echo esc_url( $this->get_next_step_link() ); ?>" class="button button-large button-next payment-step-skip-btn dokan-btn-theme"><?php esc_html_e( 'Skip this step', 'dokan-lite' ); ?></a>
                <?php wp_nonce_field( 'dokan-seller-setup' ); ?>
            </p>
        </form>
        <?php

        do_action( 'dokan_seller_wizard_after_payment_setup_form', $this );
    }

    /**
     * Save payment options.
     */
    public function dokan_setup_payment_save() {
        if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'dokan-seller-setup' ) ) {
            return;
        }

        $dokan_settings = $this->store_info;

        if ( isset( $_POST['settings']['bank'] ) ) {
            $bank = array_map( 'sanitize_text_field', (array) wp_unslash( $_POST['settings']['bank'] ) );

            $dokan_settings['payment']['bank'] = [
                'ac_name'        => $bank['ac_name'],
                'ac_type'        => $bank['ac_type'],
                'ac_number'      => $bank['ac_number'],
                'bank_name'      => $bank['bank_name'],
                'bank_addr'      => $bank['bank_addr'],
                'routing_number' => $bank['routing_number'],
                'iban'           => $bank['iban'],
                'swift'          => $bank['swift'],
            ];
        }

        if ( isset( $_POST['settings']['paypal']['email'] ) ) {
            $dokan_settings['payment']['paypal']            = [
                'email' => sanitize_email( wp_unslash( $_POST['settings']['paypal']['email'] ) ),
            ];
            $dokan_settings['profile_completion']['paypal'] = 15;
            $dokan_settings['profile_completion']['skrill'] = 0;
        }

        if ( isset( $_POST['settings']['skrill']['email'] ) ) {
            $dokan_settings['payment']['skrill']            = [
                'email' => sanitize_email( wp_unslash( $_POST['settings']['skrill']['email'] ) ),
            ];
            $dokan_settings['profile_completion']['skrill'] = 15;
            $dokan_settings['profile_completion']['paypal'] = 0;
        }

        // Check any payment methods setups and add manually value on Profile Completion also increase progress value
        if ( isset( $_POST['settings']['paypal'] ) || isset( $_POST['settings']['skrill'] ) ) {
            $profile_settings = get_user_meta( $this->store_id, 'dokan_profile_settings', true );
            if ( ! empty( $profile_settings['profile_completion']['progress'] ) ) {
                $dokan_settings['profile_completion']['progress'] = $profile_settings['profile_completion']['progress'] + 15;
            }
        }

        update_user_meta( $this->store_id, 'dokan_profile_settings', $dokan_settings );

        do_action( 'dokan_seller_wizard_payment_field_save', $this );

        wp_safe_redirect( apply_filters( 'dokan_ww_payment_redirect', esc_url_raw( $this->get_next_step_link() ) ) );
        exit;
    }

    /**
     * Final step.
     */
    public function dokan_setup_ready() {
        $dashboard_url = dokan_get_navigation_url();
        ?>
        <div class="dokan-setup-done">
            <img src="<?php echo esc_url( plugins_url( 'assets/images/dokan-checked.png', DOKAN_FILE ) ); ?>" alt="dokan setup">
            <h1><?php esc_html_e( 'Your Store is Ready!', 'dokan-lite' ); ?></h1>
        </div>

        <div class="dokan-setup-done-content">
            <p class="wc-setup-actions step">
                <a class="button button-primary dokan-btn-theme" href="<?php echo esc_url( $dashboard_url ); ?>"><?php esc_html_e( 'Go to your Store Dashboard!', 'dokan-lite' ); ?></a>
            </p>
        </div>
        <?php
    }
}
