<?php

namespace Elementor_Pay_Addons\Core;

use Elementor_Pay_Addons\Form_Customize\Form;
defined( 'ABSPATH' ) || exit;
class Build_Assets {
    /**
     * Collection of default widgets.
     *
     * @since 1.0.0
     * @access private
     */
    private $widgets;

    public function __construct() {
        $this->widgets = Config::get_active_elements();
        add_action( 'elementor/elements/categories_registered', [$this, 'register_categories'] );
        add_action( 'elementor/widgets/register', [$this, 'register_widgets'] );
        if ( in_array( 'payment-form', $this->widgets ) ) {
            add_action( 'elementor/widgets/register', [$this, 'register_customize_form_widget'] );
        }
        require_once EPA_ADDONS_PATH . '/includes/database/transaction-query.php';
        require_once EPA_ADDONS_PATH . '/includes/elements/editor/api/ajax-api-handler.php';
        require_once EPA_ADDONS_PATH . '/includes/elements/form/api/ajax-api-form-handler.php';
        require_once EPA_ADDONS_PATH . '/includes/elements/form/module.php';
        require_once EPA_ADDONS_PATH . '/includes/elements/form/field-base.php';
        require_once EPA_ADDONS_PATH . '/includes/elements/form/fields/number.php';
        require_once EPA_ADDONS_PATH . '/includes/elements/form/fields/decimal.php';
        require_once EPA_ADDONS_PATH . '/includes/elements/form/fields/date.php';
        require_once EPA_ADDONS_PATH . '/includes/elements/form/fields/time.php';
        require_once EPA_ADDONS_PATH . '/includes/elements/form/actions/base-action.php';
        require_once EPA_ADDONS_PATH . '/includes/elements/form/actions/form-redirect-action-trait.php';
        require_once EPA_ADDONS_PATH . '/includes/elements/form/actions/form-redirect-action.php';
        $this->register_form_fields_free();
        $this->register_form_actions_free();
        new \Elementor_Pay_Addons\Editor\API\Ajax_API_Handler();
        if ( \Elementor_Pay_Addons\Form\API\Ajax_API_Form_Handler::is_form_submitted() ) {
            new \Elementor_Pay_Addons\Form\API\Ajax_API_Form_Handler();
        }
    }

    public function add_widget( $widget_name ) {
        $widget_dir = EPA_ADDONS_PATH . '/includes/elements/';
        $widget_file_path = $widget_dir . $widget_name . '.php';
        if ( !file_exists( $widget_file_path ) ) {
            return;
        }
        include $widget_file_path;
    }

    public function register_widgets( $widgets_manager ) {
        // dependencies
        $this->add_widget( 'checkout-base' );
        // check if the widget is exists
        foreach ( $this->widgets as $widget ) {
            $this->add_widget( $widget );
        }
        foreach ( $this->widgets as $widget_slug ) {
            $class_name = '\\Elementor_Pay_Addons\\EPA_Widget_' . \Elementor_Pay_Addons\Shared\Utils::make_classname( $widget_slug );
            if ( class_exists( $class_name ) ) {
                $widgets_manager->register( new $class_name() );
            }
        }
    }

    public function register_categories( $elements_manager ) {
        $elements_manager->add_category( 'pay-addons', [
            'title' => esc_html__( 'Pay Addons', 'textdomain' ),
            'icon'  => 'fa fa-plug',
        ] );
    }

    public function register_customize_form_widget( $widgets_manager ) {
        require_once EPA_ADDONS_PATH . '/includes/elements/form/form-base.php';
        require_once EPA_ADDONS_PATH . '/includes/elements/form/form-controls.php';
        require_once EPA_ADDONS_PATH . '/includes/elements/form/form-record.php';
        require_once EPA_ADDONS_PATH . '/includes/elements/form/form.php';
        $widgets_manager->register( new \Elementor_Pay_Addons\Form\Form() );
    }

    public function register_form_fields_free() {
        new \Elementor_Pay_Addons\Form\Fields\Number();
        new \Elementor_Pay_Addons\Form\Fields\Decimal_Field();
        new \Elementor_Pay_Addons\Form\Fields\Date();
        new \Elementor_Pay_Addons\Form\Fields\Time();
    }

    public function register_form_actions_free() {
        $form_actions_registrar = \Elementor_Pay_Addons\Form\Form_Module::instance()->actions_registrar;
        $form_actions_registrar->register( new \Elementor_Pay_Addons\Form\Actions\Stripe_Checkout_Redirect_Action(), 'epa-form-redirect-action' );
    }

}
