<?php

namespace cBuilder\Classes;

use cBuilder\Classes\CCBEmbedCalculator;

class CCBAjaxAction {

	/**
	 * @param string   $tag The name of the action to which the $function_to_add is hooked.
	 * @param callable $function_to_add The name of the function you wish to be called.
	 * @param boolean  $nonpriv Optional. Boolean argument for adding wp_ajax_nopriv_action. Default false.
	 * @param int      $priority Optional. Used to specify the order in which the functions
	 *                                       associated with a particular action are executed. Default 10.
	 *                                       Lower numbers correspond with earlier execution,
	 *                                       and functions with the same priority are executed
	 *                                       in the order in which they were added to the action.
	 * @param int      $accepted_args Optional. The number of arguments the function accepts. Default 1.
	 * @return true Will always return true.
	 */

	public static function addAction( $tag, $function_to_add, $nonpriv = false, $priority = 10, $accepted_args = 1 ) {
		add_action( 'wp_ajax_' . $tag, $function_to_add, $priority = 10, $accepted_args = 1 );
		if ( $nonpriv ) {
			add_action( 'wp_ajax_nopriv_' . $tag, $function_to_add );
		}
		return true;
	}

	public static function init() {
		/**
		 * NEW ACTIONS
		 * ------------------------------------------------------------- **/
		self::addAction( 'ccb_fetch_calculators', array( CCBCalculatorsHandler::class, 'ccb_fetch_calculators' ) );
		self::addAction( 'ccb_fetch_templates', array( CCBCalculatorsHandler::class, 'ccb_fetch_templates' ) );
		self::addAction( 'calc_create_id', array( CCBCalculatorsHandler::class, 'create_calc_id' ) );
		self::addAction( 'calc_edit_calc', array( CCBCalculatorsHandler::class, 'edit_calc' ) );
		self::addAction( 'calc_use_template', array( CCBCalculatorsHandler::class, 'use_template' ) );
		self::addAction( 'calc_run_updates', array( CCBCalculatorsHandler::class, 'run_calc_updates' ) );
		self::addAction( 'ccb_delete_calc', array( CCBCalculatorsHandler::class, 'delete_calc' ) );
		self::addAction( 'ccb_duplicate_calc', array( CCBCalculatorsHandler::class, 'duplicate_calc' ) );

		self::addAction( 'calc_toggle_favorite', array( CCBCalculatorTemplates::class, 'calc_toggle_favorite' ) );

		/** import/export  */
		self::addAction( 'cost-calculator-custom-import-total', array( CCBExportImport::class, 'custom_import_calculators_total' ) );
		self::addAction( 'cost-calculator-demo-calculators-total', array( CCBExportImport::class, 'demo_calculators_total' ) );
		self::addAction( 'cost-calculator-import-run', array( CCBExportImport::class, 'import_run' ) );
		self::addAction( 'cost-calculator-custom-export-run', array( CCBExportImport::class, 'export_calculators' ) );

		/** Admin actions */
		self::addAction( 'ccb_get_calculator_admin_data', array( CCBCalculatorsHandler::class, 'get_calculator_admin_data' ) );
		self::addAction( 'calc_save_settings', array( CCBCalculatorsHandler::class, 'save_settings' ) );

		/**
		 * OLD ACTIONS
		 * ------------------------------------------------------------- **/
		self::addAction( 'calc_get_products', array( CCBCalculators::class, 'getProducts' ) );
		self::addAction( 'calc_get_woo_categories', array( CCBCalculators::class, 'getWooCategories' ) );
		self::addAction( 'calc_get_wp_pages', array( CCBCalculators::class, 'get_wp_pages' ) );
		self::addAction( 'calc_save_custom', array( CCBCalculators::class, 'save_custom' ) );
		self::addAction( 'calc_skip_quick_tour', array( CCBCalculators::class, 'calc_skip_quick_tour' ) );
		self::addAction( 'calc_skip_hint', array( CCBCalculators::class, 'calc_skip_hint' ) );
		self::addAction( 'calc_get_existing', array( CCBCalculators::class, 'get_existing' ) );
		self::addAction( 'calc_save_ai_api_key', array( CCBCalculators::class, 'save_api_key' ) );
		self::addAction( 'ccb_get_preset', array( CCBCalculators::class, 'ccb_get_preset' ) );
		self::addAction( 'ccb_select_preset', array( CCBCalculators::class, 'ccb_select_preset' ) );
		self::addAction( 'ccb_update_preset', array( CCBCalculators::class, 'ccb_update_preset' ) );
		self::addAction( 'ccb_update_preset_title', array( CCBCalculators::class, 'ccb_update_preset_title' ) );
		self::addAction( 'ccb_add_preset', array( CCBCalculators::class, 'ccb_add_preset' ) );
		self::addAction( 'ccb_extend_preset', array( CCBCalculators::class, 'ccb_extend_preset' ) );
		self::addAction( 'ccb_duplicate_preset', array( CCBCalculators::class, 'ccb_duplicate_preset' ) );
		self::addAction( 'ccb_delete_preset', array( CCBCalculators::class, 'ccb_delete_preset' ) );
		self::addAction( 'ccb_reset_type', array( CCBCalculators::class, 'ccb_reset_type' ) );
		self::addAction( 'ccb_preset_hide_notice', array( CCBCalculators::class, 'ccb_preset_hide_notice' ) );
		self::addAction( 'ccb_extend_pdf_template', array( CCBCalculators::class, 'ccb_extend_pdf_template' ) );
		self::addAction( 'ccb_delete_pdf_template', array( CCBCalculators::class, 'ccb_delete_pdf_template' ) );
		self::addAction( 'ccb_edit_pdf_template', array( CCBCalculators::class, 'ccb_edit_pdf_template' ) );
		self::addAction( 'ccb_restore_pdf_template', array( CCBCalculators::class, 'ccb_restore_pdf_template' ) );
		self::addAction( 'calc_save_general_settings', array( CCBCalculators::class, 'save_general_settings' ) );
		self::addAction( 'calc_get_general_settings', array( CCBCalculators::class, 'calc_get_general_settings' ) );
		self::addAction( 'calc_config_settings', array( CCBCalculators::class, 'calc_config_settings' ) );
		self::addAction( 'calc_delete_payment', array( CCBCalculators::class, 'calc_delete_payment' ) );
		self::addAction( 'ccb_update_banner', array( CCBCalculators::class, 'ccb_update_banner' ) );
		self::addAction( 'ccb_calc_views', array( CCBCalculators::class, 'ccb_calc_views' ), true );
		self::addAction( 'ccb_calc_interactions', array( CCBCalculators::class, 'ccb_calc_interactions' ), true );

		self::addAction( 'calc_save_as_template', array( CCBCalculatorTemplates::class, 'calc_save_as_template' ) );
		self::addAction( 'calc_get_templates_list', array( CCBCalculatorTemplates::class, 'calc_get_all_templates' ) );
		self::addAction( 'calc_delete_templates', array( CCBCalculatorTemplates::class, 'calc_delete_template' ) );

		self::addAction( 'calc_rollback', array( CCBCalculators::class, 'ccb_rollback_handler' ) );

		self::addAction( 'calc_delete_category', array( CCBCategory::class, 'calc_delete_category' ) );
		self::addAction( 'calc_add_category', array( CCBCategory::class, 'calc_add_category' ) );
		self::addAction( 'calc_get_category', array( CCBCategory::class, 'calc_get_categories' ) );
		self::addAction( 'calc_update_category', array( CCBCategory::class, 'calc_update_categories' ) );

		self::addAction( 'calc_get_code', array( CCBCalculatorTemplates::class, 'calc_get_code' ) );
		self::addAction( 'calc_send_code', array( CCBCalculatorTemplates::class, 'calc_send_code' ) );

		/** Embed Calculator */
		self::addAction( 'embed-create-page', array( CCBEmbedCalculator::class, 'create_page' ) );
		self::addAction( 'embed-get-pages', array( CCBEmbedCalculator::class, 'get_all_pages' ) );
		self::addAction( 'embed-insert-pages', array( CCBEmbedCalculator::class, 'insert_pages' ) );
		self::addAction( 'calc_discount_list', array( CCBDiscountController::class, 'discount_list' ) );

		if ( defined( 'CCB_PRO_VERSION' ) ) {
			self::addAction( 'calc_create_discount', array( CCBDiscountController::class, 'create' ) );
			self::addAction( 'calc_update_discount', array( CCBDiscountController::class, 'update' ) );
			self::addAction( 'calc_delete_discount', array( CCBDiscountController::class, 'delete' ) );
			self::addAction( 'calc_duplicate_discount', array( CCBDiscountController::class, 'duplicate' ) );
			self::addAction( 'calc_preview_discount_list', array( CCBDiscountController::class, 'discount_preview_list' ) );

			self::addAction( 'create_cc_order', array( CCBOrderController::class, 'create' ), true );
			self::addAction( 'complete_payment', array( CCBOrderController::class, 'complete' ), true );
			self::addAction( 'ccb_woocommerce_payment', array( CCBOrderController::class, 'renderWooCommercePayment' ), true );
		}

		/** Cost Calculator Settings */
		self::addAction( 'save_invoice_logo', array( CCBAdminActions::class, 'upload_invoice_logo' ) );
		self::addAction( 'ccb_save_email_logo', array( CCBAdminActions::class, 'upload_email_logo' ) );

		self::addAction( 'save_pickup_icon', array( CCBAdminActions::class, 'upload_pickup_icon' ) );
		self::addAction( 'save_marker_icon', array( CCBAdminActions::class, 'upload_marker_icon' ) );
		self::addAction( 'save_section_icon', array( CCBAdminActions::class, 'upload_section_icon' ) );
		self::addAction( 'save_total_icon', array( CCBAdminActions::class, 'upload_total_icon' ) );

		/** Form Manager actions */
		self::addAction( 'calc_forms_list', array( CCBForms::class, 'get_all_forms' ) );
		self::addAction( 'calc_active_form_fields', array( CCBForms::class, 'get_active_form_fields' ) );
		self::addAction( 'calc_apply_form_id', array( CCBForms::class, 'apply_form_id' ) );
		self::addAction( 'calc_create_default_form', array( CCBForms::class, 'create_default_form' ) );
		self::addAction( 'calc_update_form', array( CCBForms::class, 'update_form' ) );
		self::addAction( 'calc_duplicate_form', array( CCBForms::class, 'duplicate_form' ) );
		self::addAction( 'calc_delete_form', array( CCBForms::class, 'delete_form' ) );

		/** Cost Calculator ContactForm */
		self::addAction( 'get_template_preview_cf7', array( CCBContactFormSeven::class, 'get_template_preview_cf7' ) );

		self::addAction( 'ccb_orders_list', array( CCBOrderController::class, 'orders_list' ) );
		self::addAction( 'ccb_update_status_list', array( CCBOrderController::class, 'update_status_list' ) );
		self::addAction( 'ccb_update_order_status', array( CCBOrderController::class, 'update_order_status' ) );
		self::addAction( 'ccb_save_table_settings', array( CCBOrderController::class, 'save_table_settings' ) );
		self::addAction( 'ccb_save_orders_settings', array( CCBOrderController::class, 'save_orders_settings' ) );
		self::addAction( 'ccb_restore_orders_settings', array( CCBOrderController::class, 'restore_orders_settings' ) );
		self::addAction( 'ccb_restore_table_settings', array( CCBOrderController::class, 'restore_table_settings' ) );
		self::addAction( 'ccb_delete_order', array( CCBOrderController::class, 'delete_order' ) );
		self::addAction( 'ccb_create_order_note', array( CCBOrderController::class, 'create_order_note' ) );
		self::addAction( 'ccb_update_order_note', array( CCBOrderController::class, 'update_order_note' ) );
		self::addAction( 'ccb_delete_order_note', array( CCBOrderController::class, 'delete_order_note' ) );
		self::addAction( 'ccb_delete_orders', array( CCBOrderController::class, 'delete_orders' ) );
		self::addAction( 'ccb_update_orders_status', array( CCBOrderController::class, 'update_orders_status' ) );
		self::addAction( 'ccb_send_to_email', array( CCBOrderController::class, 'send_to_email' ) );
		self::addAction( 'ccb_get_pdf_settings', array( CCBOrderController::class, 'fetch_pdf_settings' ) );
	}
}
