<?php
namespace Formipay;
use Formipay\Traits\SingletonTrait;

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

class Init {

    use SingletonTrait;

	/**
	 * Initializes the plugin by setting filters and administration functions.
	 */
    
    protected function __construct() {
        
        add_action( 'admin_menu',                               [$this, 'menu'] );
        add_action( 'admin_footer',                             [$this, 'admin_global_custom_js'] );
        add_action( 'admin_enqueue_scripts',                    [$this, 'enqueue'] );
        add_action( 'plugin_loaded',                            [$this, 'load_nuxy'] );
        add_action( 'plugin_loaded',                            [$this, 'init'] );
        add_action( 'plugin_loaded',                            [$this, 'default_config'] );
        
    }

    public function init() {

        // Integration Class
        // \Formipay\Integration\Paypal::get_instance();
        
        // Payment classes
        \Formipay\Payment\BankTransfer::get_instance();
        \Formipay\Payment\CashOnDelivery::get_instance();
    
        // Shipping classes
        \Formipay\Shipping\FlatRate::get_instance();
    
        // Notification classes
        \Formipay\Notification\Notification::get_instance();
        \Formipay\Notification\Email::get_instance();
    
        // Core and other classes
        \Formipay\Field::get_instance();
        \Formipay\Form::get_instance();
        \Formipay\Product::get_instance();
        \Formipay\Access::get_instance();
        \Formipay\Coupon::get_instance();
        \Formipay\Settings::get_instance();
        \Formipay\Order::get_instance();
        \Formipay\Customer::get_instance();
        \Formipay\Render::get_instance();
        \Formipay\Thankyou::get_instance();

        \Formipay\License::get_instance();
        \Formipay\LicenseAPI::get_instance();

        new \Formipay\Token;
    
        do_action('formipay_init');
    }

    public function menu() {

        add_menu_page( 
            'Formipay', 
            'Formipay', 
            'manage_options', 
            FORMIPAY_MENU_SLUG, 
            '',
            FORMIPAY_URL . 'admin/assets/img/White.png', 
            5 
        );

    }

    public function load_nuxy() {
        require_once( FORMIPAY_PATH . 'vendor/wpcfto/WPCFTO.php');
    }

    public function enqueue() {
        global $current_screen;

        $formipayCPTs = [
            'formipay-form' => __( 'Edit Form', 'formipay' ), 
            'formipay-product' => __( 'Edit Product', 'formipay' ), 
            'formipay-coupon' => __( 'Edit Coupon', 'formipay' ), 
            'formipay-access' => __( 'Edit Access', 'formipay' )
        ];
        
        wp_enqueue_style( 'formipay-admin-global', FORMIPAY_URL . 'admin/assets/css/admin-global.css', [], FORMIPAY_VERSION, 'all');
        wp_enqueue_style( 'sweetalert2', FORMIPAY_URL . 'vendor/SweetAlert2/sweetalert2.min.css', [], '11.14.4', 'all');
        wp_enqueue_script( 'sweetalert2', FORMIPAY_URL . 'vendor/SweetAlert2/sweetalert2.min.js', ['jquery'], '11.14.4', true);

        if(strpos($current_screen->id, 'formipay') !== false){

            wp_enqueue_style( 'formipay-admin-pages', FORMIPAY_URL . 'admin/assets/css/admin-pages.css', [], FORMIPAY_VERSION, 'all' );
            wp_enqueue_style( 'choices', FORMIPAY_URL . 'vendor/ChoicesJS/choices.min.css', [], FORMIPAY_VERSION, 'all' );
            wp_enqueue_style( 'gridjs', FORMIPAY_URL . 'vendor/GridJS/gridjs.mermaid.min.css', [], '6.2.0', 'all' );

            wp_enqueue_script( 'choices', FORMIPAY_URL . 'vendor/ChoicesJS/choices.min.js', [], FORMIPAY_VERSION, true );
            wp_enqueue_script( 'formipay-admin-pages', FORMIPAY_URL . 'admin/assets/js/admin-pages.js', ['jquery'], FORMIPAY_VERSION, true );
            wp_enqueue_script( 'gridjs', FORMIPAY_URL . 'vendor/GridJS/gridjs.production.min.js', ['jquery'], '6.2.0', true );

        }

        if(in_array($current_screen->post_type, array_keys($formipayCPTs)) && $current_screen->base == 'post'){
            wp_enqueue_style( 'formipay-classic-editor', FORMIPAY_URL . 'admin/assets/css/classic-editor.css', [], FORMIPAY_VERSION, 'all' );
            wp_enqueue_script( 'formipay-classic-editor', FORMIPAY_URL . 'admin/assets/js/classic-editor.js', ['jquery'], FORMIPAY_VERSION, true);
            wp_enqueue_script( 'formipay-screen-menu', FORMIPAY_URL . 'admin/assets/js/admin-all-post-type.js', ['jquery'], FORMIPAY_VERSION, true);
            wp_localize_script( 'formipay-screen-menu', 'formipay_admin', [
                'site_url' => site_url(),
                'page_title' => $formipayCPTs[$current_screen->post_type]
            ] );
        }

    }

    public function admin_global_custom_js() {
        global $pagenow, $current_screen;
        ?>
            <script class="formipay-adminjs-global">
                jQuery(function($){
                    <?php 
                    // Formipay Form CPT
                    if( $current_screen->post_type == 'formipay-form' ) {
                        ?>
                        $('#toplevel_page_formipay').removeClass('wp-not-current-submenu').addClass('wp-has-current-submenu wp-menu-open');
                        $('a[href="admin.php?page=formipay"]').closest('li').addClass('current');
                        <?php
                    }
                    // Formipay Access CPT
                    if( $current_screen->post_type == 'formipay-product' ) {
                        ?>
                        $('#toplevel_page_formipay').removeClass('wp-not-current-submenu').addClass('wp-has-current-submenu wp-menu-open');
                        $('a[href="admin.php?page=formipay-product"]').closest('li').addClass('current');
                        <?php
                    }
                    // Formipay Access CPT
                    if( $current_screen->post_type == 'formipay-access' ) {
                        ?>
                        $('#toplevel_page_formipay').removeClass('wp-not-current-submenu').addClass('wp-has-current-submenu wp-menu-open');
                        $('a[href="admin.php?page=formipay-access-items"]').closest('li').addClass('current');
                        <?php
                    }
                    // Formipay Coupon CPT
                    if( $current_screen->post_type == 'formipay-coupon' ) {
                        ?>
                        $('#toplevel_page_formipay').removeClass('wp-not-current-submenu').addClass('wp-has-current-submenu wp-menu-open');
                        $('a[href="admin.php?page=formipay-coupon"]').closest('li').addClass('current');
                        <?php
                    }
                    ?>
                });
            </script>
        <?php

    }

    public function default_config() {

        $array = array (
            'thankyou_link' => 'thanks',
            'thankyou_style' => 'receipt',
            'thankyou_page_container_bg_color' => '#808080',
            'thankyou_page_wrapper_bg_color' => '#ffffff',
            'thankyou_page_wrapper_max_width' => '600',
            'thankyou_confirmation_dropzone_color' => '#cccccc',
            'thankyou_confirmation_dropzone_text_color' => '#808080',
            'thankyou_confirmation_dropzone_instruction' => 'Drag & drop a file here or click to select one',
            'thankyou_confirmation_submit_button' => 'Upload',
            'cod_instruction_thankyou' => '<p>Your package will be shipped as soon as possible. Please make sure your&nbsp;<span style="background-color: transparent; color: var(--bs-body-color); text-align: var(--bs-body-text-align);">cash of </span><span style="background-color: transparent; color: var(--bs-body-color); text-align: var(--bs-body-text-align);">{{grand_total}} will be prepared&nbsp;</span><span style="background-color: transparent; color: var(--bs-body-color); text-align: var(--bs-body-text-align);">and you can pay it to the courier who delivers your&nbsp;</span><span style="background-color: transparent; color: var(--bs-body-color); text-align: var(--bs-body-text-align);">{{product_name}}.</span></p>',
            'cod_instruction_whatsapp' => 'I will prepare the cash of {{grand_total}} for paying the order to the courier who delivers my {{product_name}}.',
            'cod_confirmation_page_group' => '',
            'cod_confirmation_page_content' => '<h3 style="text-align: left; margin-bottom: unset; padding: 0px; border: none; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-variant-alternates: inherit; font-variant-position: inherit; font-stretch: inherit; line-height: var(--theme-line-height); font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; --theme-font-weight: 700; --theme-font-size: 30px; --theme-line-height: 1.5;">Confirm Your Order</h3><h6 style="text-align: left; margin-bottom: calc(var(--has-theme-content-spacing, 1)*(.3em + 10px)); padding: 0px; border: none; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-variant-alternates: inherit; font-variant-position: inherit; font-stretch: inherit; line-height: var(--theme-line-height); font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; --theme-font-weight: 700; --theme-font-size: 16px; --theme-line-height: 1.5;">Cash on Delivery</h6><p style="text-align: center; margin-bottom: calc(var(--has-theme-content-spacing, 1)*(.3em + 10px)); padding: 0px; border: none; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-variant-alternates: inherit; font-variant-position: inherit; font-stretch: inherit; line-height: var(--theme-line-height); font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; --theme-font-weight: 700; --theme-font-size: 16px; --theme-line-height: 1.5;"><p style="text-align: left;"><span style="background-color: transparent; color: var(--bs-body-color);">Here is your order details:</span></p><p style="text-align: left;"><span style="background-color: transparent; color: var(--bs-body-color);">{{order_details}}</span></p></p><p style="text-align: center; margin-bottom: calc(var(--has-theme-content-spacing, 1)*(.3em + 10px)); padding: 0px; border: none; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-variant-alternates: inherit; font-variant-position: inherit; font-stretch: inherit; line-height: var(--theme-line-height); font-optical-sizing: inherit; font-kerning: inherit; font-feature-settings: inherit; font-variation-settings: inherit; --theme-font-weight: 700; --theme-font-size: 16px; --theme-line-height: 1.5;"><p style="text-align: left;"><span style="background-color: transparent; color: var(--bs-body-color);">Please confirm if the package has been arrived</span></p><p style="text-align: left;"><span style="background-color: transparent; color: var(--bs-body-color);">{{confirmation_form}}</span></p></p>',
            'cod_confirmation_page_width' => '600',
            'cod_confirmation_table_alignment' => 'left',
            'cod_confirmation_form_agreement' => 'I have receipt the package and pay as billed to the courier.',
            'cod_confirmation_form_button' => 'Confirm',
            'notification_email_footer' => '<p style="text-align: center;">You got this email because of ordering our products in Formipay.<br>Ignore this email if it was not you.</p>',
            'notification_email_admin_on_hold_group_header' => '',
            'notification_email_admin_on_hold_toggle' => true,
            'notification_email_admin_on_hold_title' => 'New Order On-Hold: Action Required',
            'notification_email_admin_on_hold_content' => '<p>Hello Admin,</p><p><br></p><p>A new order has been placed and is currently on hold, awaiting payment from the buyer.&nbsp;</p><p><br></p><p><strong>Buyer Name:</strong>&nbsp;{{buyer_name}}&nbsp;&nbsp;</p><p><strong>Order ID:</strong> {{order_id}}</p><p><strong>Order Status:</strong> On Hold&nbsp;&nbsp;</p><p><br></p><p>{{order_details}}</p><p><br></p><p>Please review the order and follow up with the buyer to encourage them to complete their payment.</p><p><br></p><p>Best regards,</p><p>Your E-commerce Team</p>',
            'notification_email_buyer_on_hold_group_header' => '',
            'notification_email_buyer_on_hold_toggle' => true,
            'notification_email_buyer_on_hold_title' => 'Your Order is On Hold: Action Needed',
            'notification_email_buyer_on_hold_content' => '<p>Hello {{buyer_name}},</p><p>Thank you for your recent order! Your order is currently on hold because we have not yet received your payment.</p><p><strong>Order ID:</strong>{{order_id}}</p><p><strong>Order Status:</strong>On Hold</p><p><strong>Payment Timeout:</strong>{{payment_timeout}}</p><p><br></p><p>{{order_details}}</p><p><br></p><p>Please complete your payment within the next {{payment_timeout}} to avoid cancellation. If you have any questions, feel free to reach out.</p><p>Thank you for shopping with us!</p><p><br></p><p>Best,</p><p>Your E-commerce Team</p>',
            'notification_email_admin_payment_confirm_group_header' => '',
            'notification_email_admin_payment_confirm_toggle' => true,
            'notification_email_admin_payment_confirm_title' => 'Payment Confirmed for Order: Immediate Attention Required',
            'notification_email_admin_payment_confirm_content' => '<p>Hello Admin,</p><p><br></p><p>The buyer has confirmed payment for the following order:Buyer Name: {{buyer_name}}&nbsp;&nbsp;</p><p><br></p><p><strong>Buyer Name:</strong>**Buyer Name:* {{buyer_name}}&nbsp;&nbsp;</p><p><strong>Order ID:</strong>**Order ID: {{order_id}}&nbsp;&nbsp;</p><p><strong>Order Status:</strong>Order Status: Payment Confirmed&nbsp;&nbsp;</p><p><br></p><p>{{order_details}}&nbsp;&nbsp;</p><p><br></p><p>Please take the necessary steps to process this order and prepare for shipment.</p><p><br></p><p>Best regards,&nbsp;&nbsp;</p><p>Your E-commerce Team</p>',
            'notification_email_buyer_payment_confirm_group_header' => '',
            'notification_email_buyer_payment_confirm_toggle' => true,
            'notification_email_buyer_payment_confirm_title' => 'Payment Confirmed for Your Order',
            'notification_email_buyer_payment_confirm_content' => '<p>Hello {{buyer_name}},</p><p><br></p><p>We\'re excited to let you know that your payment has been confirmed!</p><p><strong><br></strong></p><p><strong>Order ID:</strong> {{order_id}}&nbsp;&nbsp;</p><p><strong>Order Status:</strong> Payment Confirmed&nbsp;&nbsp;</p><p><br></p><p>{{order_details}}<br></p><p><br></p><p>Our team is now preparing your order for shipment. You will receive another update once your items are on their way!</p><p><br></p><p>Thank you for choosing us!</p><p><br></p><p>Best,&nbsp;&nbsp;</p><p>Your E-commerce Team</p><p><br></p>',
            'notification_email_admin_completed_toggle' => true,
            'notification_email_admin_completed_title' => 'Order Completed: Final Update',
            'notification_email_admin_completed_content' => '<p>Hello Admin,</p><p><br></p><p>The following order has been successfully completed:</p><p><strong><br></strong></p><p><strong>Buyer Name:</strong> {{buyer_name}}&nbsp;&nbsp;</p><p><strong>Order ID:</strong> {{order_id}}&nbsp;&nbsp;</p><p><strong>Order Status:</strong> Completed&nbsp;&nbsp;</p><p><br></p><p>{{order_details}}&nbsp;&nbsp;</p><p><br></p><p>No further action is required. Thank you for your continued support!</p><p><br></p><p>Best regards,&nbsp;&nbsp;</p><p>Your E-commerce Team</p><p><br></p>',
            'notification_email_buyer_completed_toggle' => true,
            'notification_email_buyer_completed_title' => 'Your Order is Complete!',
            'notification_email_buyer_completed_content' => '<p>Hello {{buyer_name}},</p><p><br></p><p>Thank you for your order! We are pleased to inform you that your order has been completed.</p><p><br></p><p><strong>Order ID:</strong> {{order_id}}&nbsp;&nbsp;</p><p><strong>Order Status:</strong> Completed&nbsp;&nbsp;</p><p><br></p><p>{{order_details}}&nbsp;&nbsp;</p><p><br></p><p>We hope you enjoy your purchase! If you have any feedback or questions, please let us know.</p><p><br></p><p>Best,&nbsp;&nbsp;</p><p>Your E-commerce Team</p><p><br></p>',
        );

        if(empty(get_option('formipay_settings'))){
            update_option('formipay_settings', $array);
        }

    }
    
}