<?php

if (!defined('ABSPATH'))
exit; 

class DevsAccounting_Pro_Shortcode {
    public function __construct() {
        add_shortcode('devsaccounting_login', [$this, 'devsaccounting_login_shortcode']);
        add_shortcode('devsaccounting_register', [$this, 'devsaccounting_register_shortcode']);
        add_shortcode('devsaccounting_purchase', [$this, 'devsaccounting_purchase_shortcode']); 
        add_shortcode('devsaccounting_profile', [$this, 'devsaccounting_profile_shortcode']);
        add_shortcode('devsaccounting_allItems', [$this, 'devsaccounting_all_items_shortcode']);
    }

    public function enqueue_shortcode_script(){
        add_filter('the_content', [$this, 'checkShortcodesAndEnqueueScript']);
    }

    public function checkShortcodesAndEnqueueScript($content) {
        $shortcodes_to_check = ['devsaccounting_invoice', 'devsaccounting_purchase'];
        
        // if (has_shortcode($content, $shortcode) && !is_page('login') && !is_page('register')) {
         
        //     wp_enqueue_style(
        //         'devs-accounting-front-style', 
        //         DEVSACCOUNTING_URL . 'build/index.css'
        //     );
        // }
    
        
        // if (is_page('login') || is_page('register') || is_user_logged_in()) {
        //     wp_enqueue_style(
        //         'devs-accounting-front-style', 
        //         DEVSACCOUNTING_URL . 'build/index.css'
        //     );
        // }
        $shortcodes_exists = false;

        foreach ($shortcodes_to_check as $shortcode) {
            if (has_shortcode($content, $shortcode)) {
                $shortcodes_exists = true;
                break;
            }
        }
        if ($shortcodes_exists) {
            wp_enqueue_script('devs-accounting-front-js', DEVSACCOUNTING_URL . 'build/frontend.js', ['wp-element'],
                  wp_rand(), 
                  true);
            wp_enqueue_style('devs-accounting-front-style', DEVSACCOUNTING_URL . 'build/index.css');
          }
          wp_localize_script('devs-accounting-front-js', 'devsAccountingApi', [
            'apiUrl' => home_url('/wp-json'),
            'nonce' => wp_create_nonce('wp_rest'),
          ]);
        return $content;
    }
    

    public function devsaccounting_login_shortcode($atts) {
        global $wpdb;
        $atts = shortcode_atts(
            $atts,
            'devsaccounting_login'
        );
        ob_start();
        $settings= get_option('devsaccounting_settings');
        $settings= json_decode($settings,true);
        
        if ( is_user_logged_in() ) {
			echo '<p>You are already logged in. <a href="' . esc_url( wp_logout_url( home_url() ) ) . '">Logout</a></p>';
			return ob_get_clean();
		}

        if (!empty($_POST)) {
            (new DevsAccounting_Auth)->handle_login();
        }
        ?>

        <div class="border-none devs-accounting-wrapper">
            <div class="flex items-center justify-center w-full px-4 py-6">
                <form method="post" action="<?php echo esc_url( site_url( 'wp-login.php', 'login_post' ) ); ?>" class="w-[500px] border px-6 py-4" >
                    <?php wp_nonce_field( 'dac_login_action', 'dac_login_nonce' ); ?>
                    <h1 class="my-3 font-bold uppercase">Login</h1>
                    <p>
                    <label for="username" class="block my-2 text-sm">Username or Email</label>
                    <input type="text" name="log" id="username" class="border rounded-sm w-full px-5 py-1.5 placeholder:text-sm" placeholder="Enter username or email" required>
                    </p>
                    <p>
                    <label for="password"  class="block my-2 text-sm">Password</label>
                    <input type="password" name="pwd" id="password" class="border rounded-sm w-full px-3 py-1.5 placeholder:text-sm" placeholder="Enter your password" required>
                    </p>
                    <!-- <p>
                   <input type="submit" name="devsaccounting_login" value="Login" class="w-full py-3 my-4 text-sm text-center text-white bg-black border rounded-sm">
                    </p> -->
                    <p class="submit">
                        <input type="submit" name="wp-submit" id="wp-submit" class="w-full py-3 my-4 text-sm text-center text-white bg-black border rounded-sm" value="<?php esc_attr_e( 'Log In' ); ?>" />
                        <span class="block text-sm text-start">Don't have an account? Please <a class="font-semibold hover:underline hover:underline-offset-2" href="<?php echo esc_url( home_url('/register') ); ?>" >Register</a> here</span> 
                      <?php
                        $interim_login = isset( $_REQUEST['interim-login'] );
                        $customize_login = isset( $_REQUEST['customize-login'] );
                        $redirect_to = home_url($settings['profile_page']);
                        if ( $interim_login ) {
                            ?>
                            <input type="hidden" name="interim-login" value="1" />
                            <?php
                        } else {
                            ?>
                            <input type="hidden" name="redirect_to" value="<?php echo esc_attr( $redirect_to ); ?>" />
                            <?php
                        }

                        if ( $customize_login ) {
                            ?>
                            <input type="hidden" name="customize-login" value="1" />
                            <?php
                        }

                        ?>
                        <input type="hidden" name="testcookie" value="1" />
                    </p>
                </form>
            </div>
        </div>
      
        <?php
        return ob_get_clean();
    }

    public function devsaccounting_register_shortcode() {
        ob_start();
		if ( is_user_logged_in() ) {
			echo '<p>You are already registered. <a href="' . esc_url( wp_logout_url( home_url() ) ) . '">Logout</a></p>';
			return ob_get_clean();
		}

        if (!empty($_POST)) {
            // print_r($_POST);exit;
            (new DevsAccounting_Auth)->handle_registration();
        }
        ?>
        <div class="border-none devs-accounting-wrapper">
            <div class="flex items-center justify-center w-full px-4 py-6">
            <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" class="w-[500px] border px-6 py-4">
                <input type="hidden" name="action" value="devsaccounting_register">
                <?php wp_nonce_field( 'dac_register_action', 'dac_register_nonce' ); ?>
                <h1 class="my-3 font-bold uppercase">Register</h1>
                <p>
                    <label for="username" class="block my-2 text-sm">Username</label>
                    <input type="text" name="username" id="username" class="border rounded-sm w-full px-3 py-1.5 placeholder:text-sm" placeholder="Enter your username" required>
                </p>
                <p>
                    <label for="email" class="block my-2 text-sm">Email</label>
                    <input type="email" name="email" id="email" class="border rounded-sm w-full px-3 py-1.5 placeholder:text-sm" placeholder="Enter your email" required>
                </p>
                <p>
                    <label for="password" class="block my-2 text-sm">Password</label>
                    <input type="password" name="password" id="password" class="border rounded-sm w-full px-3 py-1.5 placeholder:text-sm" placeholder="Enter your password" required>
                </p>
                <p>
                    <input type="submit" name="devsaccounting_register" value="Register" class="w-full py-3 my-4 text-sm text-center text-white bg-black border rounded-sm">
                    <span class="block text-sm text-start">Already have an account? Please <a class="font-semibold hover:underline hover:underline-offset-2" href="<?php echo esc_url( home_url('/login') ); ?>" >Login</a> here</span> 
                </p>
               
            </form>

            </div>
        </div>
      
        <?php
        return ob_get_clean();
    }  

    public function devsaccounting_purchase_shortcode($atts) {
        $settings = get_option('devsaccounting_settings');
        $settings = json_decode($settings, true);
    
        $atts = shortcode_atts([], $atts, 'devsaccounting_purchase');
        $vendor_id = isset($settings['paddle_vendor_id']) ? $settings['paddle_vendor_id'] : '';
        // if (!$vendor_id) {
        //     return 'Vendor ID is not set.';
        // }
        
        $vendor_id = intval($vendor_id);
    
        ob_start();
        ?>
        <script src="https://cdn.paddle.com/paddle/paddle.js"></script>
        <script type="text/javascript">
            <?php if ($settings['paddle_payment_type'] == 'sandbox') { ?>
            Paddle.Environment.set("sandbox");
            <?php } ?>
           Paddle.Setup({"vendor": <?php echo esc_js($vendor_id); ?>});
        </script>
        <div class="devs-accounting-wrapper">
            <div id="devsaccounting-checkout"></div>
        </div>
        <?php
        return ob_get_clean();
    }
    

    public function devsaccounting_profile_shortcode($atts) {
        $atts = shortcode_atts([], $atts, 'devsaccounting_profile');
    
        ob_start();
        ?>
        <div class="devs-accounting-wrapper">
            <div id="devsaccounting-myprofile"></div>
        </div>
        <?php
        return ob_get_clean();
    }

    public function devsaccounting_all_items_shortcode($atts) {
        $atts = shortcode_atts([], $atts, 'devsaccounting_allItems');
    
        ob_start();
        ?>
        <div class="devs-accounting-wrapper">
            <div id="devsaccounting_allItems"></div>
        </div>
        <?php
        return ob_get_clean();
    }
}

// Instantiate the class
$devsaccounting_pro_shortcode = new DevsAccounting_Pro_Shortcode();
