<?php

namespace cBuilder\Classes;

use cBuilder\Classes\Appearance\CCBAppearanceHelper;
use cBuilder\Classes\Appearance\Presets\CCBPresetGenerator;
use cBuilder\Classes\Database\Discounts;
use cBuilder\Classes\Database\FormFields;
use cBuilder\Classes\Database\Forms;
use cBuilder\Classes\pdfManager\CCBPdfManager;
use cBuilder\Classes\pdfManager\CCBPdfManagerHelper;
use cBuilder\Classes\pdfManager\CCBPdfManagerTemplates;
use cBuilder\Helpers\CCBCleanHelper;
use cBuilder\Helpers\CCBFieldsHelper;
use cBuilder\Helpers\CCBOrderFormFieldsHelper;
use cBuilder\Classes\Database\AnalyticsViews;
use cBuilder\Classes\Database\AnalyticsInteractions;

class CCBCalculators {

	const DESC_POSITION_BEFORE = 'before';
	const DESC_POSITION_AFTER  = 'after';
	const CALCULATOR_POST_TYPE = 'cost-calc';

	public static function getWPCalculatorsData() {
		$args = array(
			'posts_per_page' => - 1,
			'post_type'      => self::CALCULATOR_POST_TYPE,
			'post_status'    => array( 'publish' ),
		);

		if ( class_exists( 'Polylang' ) ) {
			$args['lang'] = '';
		}

		$calculator_posts = new \WP_Query( $args );

		return $calculator_posts->posts;
	}

	public static function getWPCalculatorsWithIdsData( $ids ) {
		$args = array(
			'post__in'       => $ids,
			'posts_per_page' => - 1,
			'post_type'      => self::CALCULATOR_POST_TYPE,
			'post_status'    => array( 'publish' ),
		);

		if ( class_exists( 'Polylang' ) ) {
			$args['lang'] = '';
		}

		$calculator_posts = new \WP_Query( $args );

		return $calculator_posts->posts;
	}

	/**
	 * Get Default Data
	 */
	public static function get_default_calculator_data() {
		return array(
			'pages'              => array(),
			'calculator_builder' => array(
				'id'      => '',
				'title'   => '',
				'formula' => array(),
				'fields'  => array(),
				'builder' => array(),
			),
			'settings'           => array(),
			'total_summary'      => array(),
			'general_settings'   => array(),
			'products'           => array(),
			'categories'         => array(),
			'conditions'         => array(),
			'appearance'         => array(),
			'desc_options'       => array(
				self::DESC_POSITION_BEFORE => __( 'Show before field', 'cost-calculator-builder' ),
				self::DESC_POSITION_AFTER  => __( 'Show after field', 'cost-calculator-builder' ),
			),
			'order_form_manager' => array(
				'order_form_fields'        => array(),
				'active_form_id'           => null,
				'order_forms'              => array(),
				'calc_active_form_fields'  => array(),
				'order_active_form_fields' => array(),
			),
		);
	}

	public static function getProducts() {
		check_ajax_referer( 'calc_load_more_products', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$product_ids = array();

		$page     = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1;
		$search   = ! empty( $_GET['search'] ) ? $_GET['search'] : '';
		$per_page = ! empty( $_GET['per_page'] ) ? $_GET['per_page'] : 5;

		$called_by = ! empty( $_GET['called_by'] ) ? $_GET['called_by'] : false;

		if ( ! empty( $_GET['product_ids'] ) ) {
			$product_ids = explode( ',', $_GET['product_ids'] );
		}

		$data = ccb_woo_products( $page, $per_page, $search, $product_ids, $called_by );

		$products     = $data['items'];
		$total_count  = $data['total'];
		$loaded_count = $page * $per_page;

		wp_send_json(
			array(
				'products' => $products,
				'has_more' => $loaded_count < $total_count,
				'page'     => $page,
				'success'  => true,
			)
		);
	}

	public static function getWooCategories() {
		check_ajax_referer( 'calc_load_woo_categories', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$category_ids = array();

		$page     = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1;
		$search   = ! empty( $_GET['search'] ) ? sanitize_text_field( $_GET['search'] ) : '';
		$per_page = ! empty( $_GET['per_page'] ) ? (int) $_GET['per_page'] : 5;

		if ( ! empty( $_GET['category_ids'] ) ) {
			$category_ids = explode( ',', sanitize_text_field( $_GET['category_ids'] ) );
		}

		$data = ccb_woo_categories_paginated( $page, $per_page, $search, $category_ids );

		wp_send_json(
			array(
				'categories' => $data['items'],
				'has_more'   => $data['has_more'],
				'page'       => $page,
				'success'    => true,
			)
		);
	}

	public static function get_wp_pages() {
		check_ajax_referer( 'calc_load_pages', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$page_ids = array();

		$page     = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1;
		$search   = ! empty( $_GET['search'] ) ? $_GET['search'] : '';
		$per_page = ! empty( $_GET['per_page'] ) ? $_GET['per_page'] : 5;

		if ( ! empty( $_GET['page_ids'] ) ) {
			$page_ids = explode( ',', $_GET['page_ids'] );
		}

		$data = ccb_all_available_pages( $page, $per_page, $search, $page_ids );

		$pages        = $data['items'];
		$total_count  = $data['total'];
		$loaded_count = $page * $per_page;

		wp_send_json(
			array(
				'pages'    => $pages,
				'has_more' => $loaded_count < $total_count,
				'page'     => $page,
				'success'  => true,
			)
		);
	}

	public static function edit_calc() {
		check_ajax_referer( 'ccb_edit_calc', 'nonce' );
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$params = array(
			'discount'    => $_GET['discount'] ?? '',
			'page_params' => self::get_filter_data( $_GET ),
			'product_ids' => ! empty( $_GET['product_ids'] ) ? $_GET['product_ids'] : array(),
		);

		$params['calc_id'] = ! empty( $_GET['calc_id'] ) ? sanitize_text_field( $_GET['calc_id'] ) : '';
		$result            = self::edit_calc_body( $params );

		$result['success'] = true;
		$result['message'] = '';

		wp_send_json( $result );
	}

	public static function edit_calc_body( $params ) {
		$result = self::get_default_calculator_data();
		if ( ! empty( $params['calc_id'] ) ) {
			$calc_id = (int) sanitize_text_field( $params['calc_id'] );

			$result['order_form_fields']        = CCBOrderFormFieldsHelper::order_form_fields();
			$result['activeFormId']             = CCBOrderFormFieldsHelper::get_active_form_id( $calc_id );
			$result['order_forms']              = Forms::get_all_forms();
			$result['order_active_form_fields'] = FormFields::get_active_fields( $result['activeFormId'] );

			$result['saved']  = get_post_meta( $calc_id, 'calc_saved', true );
			$general_settings = CCBSettingsData::get_calc_global_settings();
			$positions_keys   = array( 'top_left', 'center_left', 'bottom_left', 'top_center', 'bottom_center', 'top_right', 'center_right', 'bottom_right' );
			$positions        = array();

			foreach ( $positions_keys as $key ) {
				$positions[ $key ] = array(
					'count'  => 0,
					'titles' => array(),
					'ids'    => array(),
				);
			}

			$calculators = CCBUpdatesCallbacks::get_calculators();

			foreach ( array_reverse( $calculators ) as $calculator ) {
				$calc_settings = CCBSettingsData::get_calc_single_settings( $calculator->ID );
				if ( isset( $calc_settings['sticky_calc'] ) && $calc_settings['sticky_calc']['enable'] && 'btn' === $calc_settings['sticky_calc']['display_type'] ) {
					$position_type = $calc_settings['sticky_calc']['btn_position'];

					if ( isset( $positions[ $position_type ]['count'] ) && $positions[ $position_type ]['count'] < 2 ) {
						$title = get_post_meta( $calculator->ID, 'stm-name', true );

						$positions[ $position_type ]['count']++;
						$positions[ $position_type ]['titles'][] = $title;
						$positions[ $position_type ]['ids'][]    = $calculator->ID;
					}
				}

				if ( isset( $calc_settings['sticky_calc'] ) && ! $calc_settings['sticky_calc']['enable'] && 'banner' === $calc_settings['sticky_calc']['display_type'] ) {
					$general_settings['sticky']['used_banner'] = null;
				}
			}

			if ( ! isset( $general_settings['sticky'] ) ) {
				$general_settings['sticky'] = array(
					'positions' => $positions,
				);
			} else {
				$general_settings['sticky']['positions'] = $positions;
			}

			$result['general_settings'] = $general_settings;

			$discount_params = array();
			if ( isset( $params['discount'] ) ) {
				$discount_params = self::get_filter_data( $params['discount'] );
			}

			$discount_params['calc_id'] = $calc_id;

			$preset_key = get_post_meta( $calc_id, 'ccb_calc_preset_idx', true );
			$preset_key = empty( $preset_key ) ? 'default' : $preset_key;

			$appearance = CCBAppearanceHelper::get_appearance_data( $preset_key );

			if ( ! empty( $appearance ) ) {
				$result['preset_idx'] = $preset_key;
				$result['appearance'] = $appearance['data'];
				$result['presets']    = $appearance['list'];

				$preset_keys = array();
				foreach ( $appearance['list'] as $item ) {
					$preset_keys[] = $item['key'];
				}

				if ( ! in_array( $preset_key, $preset_keys, true ) ) {
					$result['preset_idx'] = 'default';
					CCBPresetGenerator::update_preset_key( $calc_id );
					$appearance_inner = CCBAppearanceHelper::get_appearance_data( $preset_key );

					if ( ! empty( $appearance_inner ) ) {
						$result['appearance'] = $appearance_inner['data'];
					}
				}
			}

			$settings = CCBSettingsData::get_calc_single_settings( $calc_id );

			if ( ccb_pro_active() ) {
				/* pro-features */
				$result['payments']   = \cBuilder\Classes\CCBProSettings::get_payments();
				$result['pages']      = self::fetch_page_ids( $settings );
				$result['forms']      = ccb_contact_forms();
				$result['products']   = self::fetch_product_ids( $settings );
				$result['categories'] = ccb_woo_categories();
			}

			$result['sp_list'] = array();
			$sp_list           = get_post_meta( $calc_id, 'ccb_savepoint_list', true );

			if ( empty( $sp_list ) ) {
				$sp_list = array();
			}

			foreach ( $sp_list as $key => $sp ) {
				$result['sp_list'][] = array_merge(
					$sp['basic'],
					array(
						'key'     => $key,
						'created' => ccb_format_history_created( $sp['basic']['timestamp'] ),
					)
				);
			}

			if ( isset( $settings[0]['general'] ) ) {
				$settings = $settings[0];
			}

			if ( ! empty( $settings ) ) {
				$result['settings'] = $settings;
			}

			if ( ! is_array( $result['settings'] ) || empty( $result['settings']['general'] ) ) {
				$result['settings'] = CCBSettingsData::settings_data();
			}

			if ( ! empty( $result['settings']['formFields']['body'] ) ) {
				$result['settings']['formFields']['body'] = str_replace( '<br>', PHP_EOL, $result['settings']['formFields']['body'] );
			}

			if ( defined( 'CALC_DEV_MODE' ) ) {
				$result['cats']            = CCBCategory::calc_categories_list();
				$result['icons']           = CCBCalculatorTemplates::calc_template_icons();
				$result['cat']             = get_post_meta( $calc_id, 'category', true );
				$result['icon']            = get_post_meta( $calc_id, 'icon', true );
				$result['pluginType']      = get_post_meta( $calc_id, 'plugin_type', true );
				$result['calcLink']        = get_post_meta( $calc_id, 'calc_link', true );
				$result['info']            = get_post_meta( $calc_id, 'info', true );
				$result['calcDescription'] = get_post_meta( $calc_id, 'description', true );
			}

			$general_settings   = CCBSettingsData::get_calc_global_settings();
			$ccb_sync           = ccb_sync_settings_from_general_settings( $result['settings'], $general_settings );
			$result['settings'] = $ccb_sync['settings'];
		}

		return $result;
	}

	public static function fetch_product_ids( $settings ) {
		$products = ccb_woo_products( 1, 5, '', $settings['woo_products']['product_ids'] );
		return $products['items'] ?? array();
	}

	public static function fetch_page_ids( $settings ) {
		$ids   = array_column( $settings['sticky_calc']['pages'], 'id' ) ?? array();
		$pages = ccb_all_available_pages( 1, 5, '', $ids );
		return $pages['items'] ?? array();
	}

	/**
	 * Duplicate target calc
	 *
	 * @param $calc_id
	 * @return array
	 */
	public static function duplicate_target_calc( $calc_id, $copy_title = true ) {
		$calc_id = (int) sanitize_text_field( $calc_id );

		$my_post = array(
			'post_type'   => 'cost-calc',
			'post_status' => true === $copy_title ? 'publish' : 'draft',
			'post_parent' => $calc_id,
		);

		// get id
		$id = wp_insert_post( $my_post );

		$calc_data          = CCBSettingsData::settings_data();
		$duplicate_settings = CCBSettingsData::get_calc_single_settings( $calc_id );
		$settings           = ccb_array_merge_recursive_left_source( $calc_data, $duplicate_settings );

		return array(
			'id'         => $id,
			'title'      => self::get_calculator_name_for_duplicate( $calc_id, $copy_title ),
			'formula'    => get_post_meta( $calc_id, 'stm-formula', true ),
			'settings'   => $settings,
			'builder'    => get_post_meta( $calc_id, 'stm-fields', true ),
			'conditions' => get_post_meta( $calc_id, 'stm-conditions', true ),
			'preset_idx' => get_post_meta( $calc_id, 'ccb_calc_preset_idx', true ),
		);
	}

	/**
	 *  Generate calc id(create cost-calc post) and send
	 */
	public static function create_calc_id() {
		check_ajax_referer( 'ccb_create_id', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		// create cost-calc post and get id
		$id = wp_insert_post(
			array(
				'post_type'   => 'cost-calc',
				'post_status' => 'draft',
			)
		);

		$result = array(
			'id'                => $id,
			'url'               => admin_url( 'admin.php' ) . '?page=cost_calculator_builder&action=edit&id=' . $id,
			'success'           => true,
			'forms'             => ccb_contact_forms(),
			'products'          => ccb_woo_products()['items'] ?? array(),
			'categories'        => ccb_woo_categories(),
			'fields'            => CCBFieldsHelper::fields(),
			'order_form_fields' => CCBOrderFormFieldsHelper::order_form_fields(),
			'desc_options'      => array(
				self::DESC_POSITION_BEFORE => __( 'Show before field', 'cost-calculator-builder' ),
				self::DESC_POSITION_AFTER  => __( 'Show after field', 'cost-calculator-builder' ),
			),
		);

		$preset_key = get_post_meta( $id, 'ccb_calc_preset_idx', true );
		$preset_key = empty( $preset_key ) ? 'default' : $preset_key;
		$appearance = CCBAppearanceHelper::get_appearance_data( $preset_key );

		if ( ! empty( $appearance ) ) {
			$result['preset_idx'] = $preset_key;
			$result['appearance'] = $appearance['data'];
			$result['presets']    = $appearance['list'];
		}

		$calculators = self::get_calculator_list();
		if ( $calculators['calculators_count'] <= 1 && 'skip' === get_option( 'ccb_quick_tour_type' ) ) {
			update_option( 'ccb_quick_tour_type', 'quick_tour_start' );
		}

		wp_send_json( $result );
	}

	/**
	 * Append ( copy int ) to new calendar title
	 * based on duplicated calculator title
	 *
	 * @param mixed
	 * @return mixed|string
	 */
	public static function get_calculator_name_for_duplicate( $calc_id, $copy_title = true ) {
		$args = array(
			'post_type'      => 'cost-calc',
			'posts_per_page' => -1,
			'post_status'    => 'publish',
			'post_parent'    => $calc_id,
		);

		if ( class_exists( 'Polylang' ) ) {
			$args['lang'] = '';
		}

		$exist_duplicates          = get_children( $args, ARRAY_A );
		$count_duplicate_meta      = count( $exist_duplicates );
		$duplicated_from_calc_name = get_post_meta( $calc_id, 'stm-name', true );

		if ( $copy_title ) {
			return $duplicated_from_calc_name . ' (copy ' . $count_duplicate_meta . ')';
		}

		return $duplicated_from_calc_name;
	}

	/**
	 * Save Custom Styles
	 */
	public static function save_custom() {

		check_ajax_referer( 'ccb_save_custom', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$result = array(
			'success' => false,
			'message' => 'Something went wrong',
		);

		if ( isset( $_POST['action'] ) && 'calc_save_custom' === $_POST['action'] && ! empty( $_POST['content'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
			$data       = apply_filters( 'stm_ccb_sanitize_array', $_POST );
			$content    = str_replace( '\"', '"', $data['content'] );
			$content    = str_replace( "\'", "'", $content );
			$content    = str_replace( '\\\\', "\\", $content ); //phpcs:ignore
			$content    = json_decode( $content, true );
			$appearance = array();

			if ( isset( $content['appearance'] ) ) {
				$appearance = $content['appearance'];
			}

			$preset_key = ! isset( $data['selectedIdx'] ) ? 'default' : sanitize_text_field( $data['selectedIdx'] );
			if ( 'saved' === $preset_key ) {
				$presets = CCBPresetGenerator::get_static_preset_from_db( true );
				$max_idx = 0;
				foreach ( array_keys( $presets ) as $k ) {
					if ( preg_match( '/^saved_(\d+)$/', $k, $m ) ) {
						$max_idx = max( $max_idx, (int) $m[1] );
					}
				}
				$new_preset             = CCBPresetGenerator::generate_new_preset( $max_idx + 1, $appearance );
				$preset_key             = $new_preset['key'];
				$presets[ $preset_key ] = $new_preset;

				CCBPresetGenerator::update_presets( $presets );
			}

			if ( ! CCBPresetGenerator::preset_exist( $preset_key ) ) {
				$preset_key = 'default';
			}

			CCBPresetGenerator::save_custom( $preset_key, $appearance );
			CCBPresetGenerator::update_preset_key( $data['id'], sanitize_text_field( $preset_key ) );

			$presets_data = CCBAppearanceHelper::get_appearance_data( $preset_key );
			$appearance   = self::get_save_custom_appearance_response( $preset_key, $presets_data );

			$result['success'] = true;
			$result['message'] = 'Changes Saved successfully';
			$result['data']    = array(
				'appearance' => $appearance,
				'presetIdx'  => $appearance['selected_preset_key'],
				'presets'    => $appearance['presets'],
			);
		}

		wp_send_json( $result );
	}

	private static function get_save_custom_appearance_response( $preset_key, $presets_data ) {
		$active_preset = isset( $presets_data['data'] ) && is_array( $presets_data['data'] ) ? $presets_data['data'] : array();
		if ( ! isset( $active_preset['preset_key'] ) ) {
			$active_preset['preset_key'] = $preset_key;
		}

		return array(
			'presets'             => self::normalize_save_custom_presets( $presets_data['list'] ?? array() ),
			'active_preset'       => $active_preset,
			'selected_preset_key' => $preset_key,
		);
	}

	private static function normalize_save_custom_presets( $presets ) {
		if ( ! is_array( $presets ) ) {
			return array();
		}

		$result = array();
		foreach ( $presets as $preset ) {
			$preset_id = isset( $preset['key'] ) ? $preset['key'] : '';
			if ( empty( $preset_id ) ) {
				continue;
			}

			$preset_appearance = CCBAppearanceHelper::get_appearance_data( $preset_id );
			$appearance_data   = isset( $preset_appearance['data'] ) && is_array( $preset_appearance['data'] ) ? $preset_appearance['data'] : array();

			$result[] = array(
				'id'     => $preset_id,
				'name'   => isset( $preset['title'] ) ? $preset['title'] : '',
				'colors' => self::extract_save_custom_preset_colors( $appearance_data ),
			);
		}

		return $result;
	}

	private static function extract_save_custom_preset_colors( $appearance_data ) {
		$color_keys = array( 'container', 'primary_color', 'accent_color', 'secondary_color', 'error_color' );
		$result     = array();

		foreach ( $color_keys as $key ) {
			$value = '';

			if ( isset( $appearance_data['desktop']['colors']['data'][ $key ]['value'] ) ) {
				$value = $appearance_data['desktop']['colors']['data'][ $key ]['value'];
			} elseif ( isset( $appearance_data['desktop']['colors'][ $key ] ) ) {
				$value = $appearance_data['desktop']['colors'][ $key ];
			}

			if ( is_array( $value ) && isset( $value['color'] ) ) {
				$value = $value['color'];
			}

			$result[] = $value;
		}

		return $result;
	}

	public static function calc_skip_hint() {
		check_ajax_referer( 'calc_skip_hint', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$skipped_done = get_option( 'ccb_quick_tour_type', 'quick_tour_start' );
		if ( 'done' === $skipped_done ) {
			$hints = ! empty( $_POST['hints'] ) ? apply_filters( 'ccb_sanitize_hints', $_POST['hints'] ) : array();
			update_option( 'calc_hint_skipped', $hints );
		}

		$result = array(
			'success' => true,
			'message' => 'Hint skipped',
		);
		wp_send_json( $result );
	}

	public static function calc_skip_quick_tour() {
		check_ajax_referer( 'calc_skip_quick_tour', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		update_option( 'ccb_quick_tour_type', 'done' );

		$result = array(
			'success' => true,
			'message' => 'Quick Tour skipped',
		);
		wp_send_json( $result );
	}

	/**
	 * Get All existing calculator
	 */
	public static function get_existing() {
		check_ajax_referer( 'ccb_get_existing', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$result = array(
			'forms'       => array(),
			'calculators' => array(),
		);

		$params      = self::get_filter_data( $_GET );
		$calculators = self::get_calculator_list( $params );

		$default_hints               = array( 'calculators', 'conditions', 'settings', 'discounts' );
		$result['calc_hint_skipped'] = ccb_pro_active() && ! empty( get_option( 'calc_allow_hint' ) ) ? get_option( 'calc_hint_skipped', array() ) : $default_hints;
		$result['quick_tour_step']   = get_option( 'ccb_quick_tour_type', 'skip' );
		$result['quick_tour_data']   = null;

		if ( 'done' !== $result['quick_tour_step'] ) {
			$quick_tour_data = CALC_PATH . '/demo-sample/quick_tour_data.txt'; //phpcs:ignore
			if ( file_exists( $quick_tour_data ) ) { //phpcs:ignore
				$contents = file_get_contents( $quick_tour_data ); //phpcs:ignore
				$contents = json_decode( $contents, true );

				if ( isset( $contents['calculators'] ) && count( $contents['calculators'] ) > 0 ) {
					$quick_tour_calc           = $contents['calculators'][0];
					$result['quick_tour_data'] = array(
						'title'      => $quick_tour_calc['ccb_name'],
						'fields'     => $quick_tour_calc['ccb_fields'],
						'conditions' => $quick_tour_calc['ccb_conditions'],
					);
				}
			}
		}

		if ( is_array( $calculators ) ) {
			$result['success']     = true;
			$result['calculators'] = $calculators;

			/* pro-features */
			$result['forms'] = ccb_contact_forms();
		}

		wp_send_json( $result );
	}

	public static function save_api_key() {
		check_ajax_referer( 'ccb_save_ai_api_key', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$result = array(
			'success' => false,
			'message' => 'Something went wrong',
		);

		if ( ! empty( $_POST['key'] ) ) {
			$key             = sanitize_text_field( $_POST['key'] );
			$global_settings = CCBSettingsData::get_calc_global_settings();

			if ( empty( $global_settings['ai']['gpt_api_key'] ) ) {
				$global_settings['ai']['gpt_api_key'] = $key;
				CCBSettingsData::update_calc_global_settings( $global_settings );

				$result['success'] = true;
				$result['message'] = __( 'Key updated successfully', 'cost-calculator-builder' );
			}
		}

		wp_send_json( $result );
	}

	/**
	 * Get general settings
	 */
	public static function calc_get_general_settings() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$result = array(
			'success' => false,
			'data'    => null,
			'message' => 'Something went wrong',
		);

		$data = CCBSettingsData::get_calc_global_settings();

		if ( isset( $data['recaptcha'] ) && empty( $data['recaptcha']['type'] ) ) {
			$data['recaptcha']['type'] = 'v2';
		}

		if ( ! empty( $data ) ) {
			$result['data']    = $data;
			$result['success'] = true;
			$result['message'] = __( 'General settings data', 'cost-calculator-builder' );
		}

		wp_send_json( $result );
	}

	/**
	 * Save general settings
	 */
	public static function save_general_settings() {
		check_ajax_referer( 'ccb_save_general_settings', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$result = array(
			'success' => false,
			'message' => 'Something went wrong',
		);

		$request_body = file_get_contents( 'php://input' );
		$request_data = json_decode( $request_body, true );
		$data         = apply_filters( 'stm_ccb_sanitize_array', $request_data );

		if ( ! empty( $data ) && isset( $data['settings'] ) ) {
			if ( isset( $data['settings']['backup_settings'] ) && true !== filter_var( $data['settings']['backup_settings']['auto_backup'], FILTER_VALIDATE_BOOLEAN ) ) {
				self::clear_all_history();
			}

			CCBSettingsData::update_calc_global_settings( $data['settings'] );
			$result['success'] = true;
			$result['message'] = __( 'Settings updated successfully', 'cost-calculator-builder' );
		}
		if ( ! empty( $data ) && isset( $data['pdf_manager'] ) ) {
			$options          = $data['pdf_manager']['options'] ?? array();
			$general_settings = CCBSettingsData::get_calc_global_settings();

			foreach ( $options as $key => $value ) {
				if ( isset( $general_settings['invoice'][ $key ] ) ) {
					$general_settings['invoice'][ $key ] = $value;
				}
			}

			CCBSettingsData::update_calc_global_settings( $general_settings );

			$key       = $data['pdf_manager']['key'];
			$templates = CCBPdfManagerTemplates::ccb_get_templates_list( true );

			if ( ( ! empty( $templates[ $key ] ) && $templates[ $key ]['is_custom'] ) || ! isset( $templates[ $key ] ) ) {
				$template = CCBPdfManagerTemplates::ccb_extract_template_data( $data['pdf_manager']['data'] );
				CCBPdfManager::ccb_add_or_update_pdf_tempalte( $key, $template );
				CCBPdfManager::update_template_key( $key );

				$result['success']          = true;
				$result['message']          = 'Templated updated successfully';
				$result['data']['template'] = CCBPdfManagerTemplates::ccb_get_template_skeleton( $template );
			} elseif ( ! empty( $templates[ $key ] ) && ! $templates[ $key ]['is_custom'] ) {
				$new_template = CCBPdfManagerTemplates::ccb_extract_template_data( $data['pdf_manager']['data'] );
				CCBPdfManager::ccb_add_or_update_pdf_tempalte( $new_template['key'], $new_template );
				CCBPdfManager::update_template_key( $new_template['key'] );

				$result['success']          = true;
				$result['message']          = 'Templated created successfully';
				$result['data']['template'] = CCBPdfManagerTemplates::ccb_get_template_skeleton( $new_template );
			} else {
				$result['success'] = false;
				$result['message'] = 'Cannot save template';
			}

			$result['data']['templates'] = CCBPdfManagerTemplates::ccb_get_templates_list();
		}

		wp_send_json( $result );
	}

	/**
	 * Return ready array for response
	 *
	 * @return array
	 */
	public static function get_calculator_list( $params = array() ) {
		$result          = array();
		$existing        = ccb_calc_get_all_posts( 'cost-calc', $params );
		$params['limit'] = -1;
		$existing_count  = ccb_calc_get_all_posts( 'cost-calc', $params );

		if ( is_array( $existing ) ) {
			foreach ( $existing as $key => $value ) {
				$temp = array();

				$temp['id']           = $key;
				$temp['project_name'] = ! empty( $value ) ? $value : 'name is empty';

				$result[] = $temp;
			}
		}

		return array(
			'calculators_count' => count( $existing_count ),
			'existing'          => $result,
		);
	}

	/**
	 * @param $data
	 * @return array
	 */
	private static function get_filter_data( $data ) {
		$sort_by   = ! empty( $data['sortBy'] ) ? sanitize_text_field( $data['sortBy'] ) : 'id';
		$direction = ! empty( $data['direction'] ) ? sanitize_text_field( $data['direction'] ) : 'desc';
		$page      = ! empty( $data['page'] ) ? (int) sanitize_text_field( $data['page'] ) : 1;
		$limit     = ! empty( $data['limit'] ) ? sanitize_text_field( $data['limit'] ) : 5;
		$search    = ! empty( $data['search'] ) ? sanitize_text_field( $data['search'] ) : '';
		$offset    = 1 === $page ? 0 : ( $page - 1 ) * $limit;
		$direction = 'desc' === strtolower( $direction ) ? $direction : 'asc';

		return array(
			'page'      => $page,
			'limit'     => $limit,
			'offset'    => $offset,
			'sort_by'   => $sort_by,
			'direction' => $direction,
			'search'    => $search,
		);
	}

	public static function ccb_delete_preset() {
		check_ajax_referer( 'ccb_delete_preset', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$result = array(
			'success' => false,
			'message' => 'Something went wrong',
		);

		if ( isset( $_GET['calc_id'] ) && isset( $_GET['selectedIdx'] ) ) {
			$preset_key      = sanitize_text_field( $_GET['selectedIdx'] );
			$presets_from_db = CCBPresetGenerator::get_static_preset_from_db();

			if ( isset( $presets_from_db[ $preset_key ] ) ) {
				unset( $presets_from_db[ $preset_key ] );
				CCBPresetGenerator::update_presets( apply_filters( 'ccb_appearance_data_update', $presets_from_db ) );
				CCBPresetGenerator::update_preset_key( $_GET['calc_id'], 'default' );

				$preset = CCBAppearanceHelper::get_appearance_data( $preset_key );
				$result = array(
					'success' => true,
					'message' => 'Theme deleted',
					'list'    => ! isset( $preset['list'] ) ? array() : $preset['list'],
					'data'    => ! isset( $preset['data'] ) ? array() : $preset['data'],
				);
			}
		}

		wp_send_json( $result );
	}

	public static function ccb_preset_hide_notice() {
		check_ajax_referer( 'ccb_preset_hide_notice', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		update_option( 'ccb_appearance_hide_notice', true );
		wp_send_json(
			array(
				'status'  => 200,
				'message' => 'Notice hide successfully',
			)
		);
	}

	public static function ccb_reset_type() {
		check_ajax_referer( 'ccb_reset_type', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$result = array(
			'data'    => null,
			'success' => false,
			'message' => 'Something went wrong',
		);

		if ( isset( $_GET['selectedIdx'] ) && isset( $_GET['type'] ) && isset( $_GET['device'] ) ) {
			$key        = sanitize_text_field( $_GET['selectedIdx'] );
			$type       = sanitize_text_field( $_GET['type'] );
			$device     = sanitize_text_field( $_GET['device'] );
			$reset_data = CCBPresetGenerator::reset_type( $key, $type, $device );

			if ( $reset_data ) {
				$result['data']    = array( 'reset_data' => $reset_data );
				$result['success'] = true;
				$result['message'] = 'Changes Saved successfully';
			}
		}

		wp_send_json( $result );
	}

	public static function ccb_add_preset() {
		check_ajax_referer( 'ccb_add_preset', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$result = array(
			'success' => false,
			'message' => 'Something went wrong',
		);

		if ( isset( $_GET['calc_id'] ) && isset( $_GET['selectedIdx'] ) ) {
			$preset_idx      = sanitize_text_field( $_GET['idx'] );
			$presets_from_db = CCBPresetGenerator::get_static_preset_from_db();
			$new_preset      = CCBPresetGenerator::generate_new_preset( $preset_idx );
			$new_key         = $new_preset['key'];

			$presets_from_db[ $new_key ] = $new_preset;

			CCBPresetGenerator::update_presets( apply_filters( 'ccb_appearance_data_update', $presets_from_db ) );
			CCBPresetGenerator::update_preset_key( $_GET['calc_id'], sanitize_text_field( $new_key ) );

			$presets = CCBAppearanceHelper::get_appearance_data( $new_key );
			$result  = array(
				'success' => true,
				'message' => 'Theme created',
				'list'    => $presets['list'] ?? array(),
				'data'    => $presets['data'] ?? array(),
			);
		}

		wp_send_json( $result );
	}

	public static function ccb_duplicate_preset() {
		check_ajax_referer( 'ccb_duplicate_preset', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$result = array(
			'success' => false,
			'message' => 'Something went wrong',
		);

		if ( isset( $_GET['selectedIdx'] ) ) {
			$preset_key      = sanitize_text_field( $_GET['selectedIdx'] );
			$presets_from_db = CCBPresetGenerator::get_static_preset_from_db( true );

			$source_data = null;
			if ( isset( $presets_from_db[ $preset_key ]['data'] ) ) {
				$source_data = $presets_from_db[ $preset_key ]['data'];
			} else {
				$default_presets = CCBPresetGenerator::default_presets();
				if ( isset( $default_presets[ $preset_key ]['data'] ) ) {
					$source_data = $default_presets[ $preset_key ]['data'];
				}
			}

			if ( ! empty( $source_data ) ) {
				$max_idx = 0;
				foreach ( array_keys( $presets_from_db ) as $k ) {
					if ( preg_match( '/^saved_(\d+)$/', $k, $m ) ) {
						$max_idx = max( $max_idx, (int) $m[1] );
					}
				}
				$new_preset                  = CCBPresetGenerator::extend_preset( $max_idx + 1, $source_data );
				$new_key                     = $new_preset['key'];
				$presets_from_db[ $new_key ] = $new_preset;

				CCBPresetGenerator::update_presets( apply_filters( 'ccb_appearance_data_update', $presets_from_db ) );

				$presets_data = CCBAppearanceHelper::get_appearance_data( $new_key );
				$result       = array(
					'success'        => true,
					'message'        => 'Preset duplicated successfully',
					'list'           => self::normalize_save_custom_presets( $presets_data['list'] ?? array() ),
					'data'           => $presets_data['data'] ?? array(),
					'new_preset_key' => $new_key,
				);
			}
		}

		wp_send_json( $result );
	}

	public static function ccb_extend_preset() {
		check_ajax_referer( 'ccb_extend_preset', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$result = array(
			'success' => false,
			'message' => 'Something went wrong',
		);

		if ( isset( $_GET['selectedIdx'] ) ) {
			$preset_key = sanitize_text_field( $_GET['selectedIdx'] );
			$presets    = CCBAppearanceHelper::get_appearance_data( $preset_key );

			if ( isset( $presets['data'] ) ) {
				$result = array(
					'success' => true,
					'message' => 'Preset extended successfully',
					'data'    => $presets['data'],
				);
			}
		}

		wp_send_json( $result );
	}

	public static function ccb_get_preset() {
		check_ajax_referer( 'ccb_get_preset', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$result = array(
			'success' => false,
			'message' => 'Something went wrong',
		);

		if ( isset( $_GET['selectedIdx'] ) ) {
			$preset_key = sanitize_text_field( $_GET['selectedIdx'] );
			$presets    = CCBAppearanceHelper::get_appearance_data( $preset_key );

			if ( isset( $presets['data'] ) ) {
				$result = array(
					'success' => true,
					'message' => 'Theme loaded',
					'data'    => $presets['data'],
				);
			}
		}

		wp_send_json( $result );
	}

	public static function ccb_select_preset() {
		check_ajax_referer( 'ccb_select_preset', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$result = array(
			'success' => false,
			'message' => 'Something went wrong',
		);

		if ( isset( $_GET['calc_id'] ) && isset( $_GET['selectedIdx'] ) ) {
			$calc_id    = intval( $_GET['calc_id'] );
			$preset_key = sanitize_text_field( $_GET['selectedIdx'] );

			if ( ! CCBPresetGenerator::preset_exist( $preset_key ) ) {
				$preset_key = 'default';
			}

			CCBPresetGenerator::update_preset_key( $calc_id, $preset_key );

			$result = array(
				'success' => true,
				'message' => 'Theme selected',
				'data'    => array(
					'selected_preset_key' => $preset_key,
				),
			);
		}

		wp_send_json( $result );
	}

	public static function ccb_update_preset() {
		check_ajax_referer( 'ccb_update_preset', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$result = array(
			'success' => false,
			'message' => 'Something went wrong',
		);

		if ( isset( $_GET['calc_id'] ) && isset( $_GET['selectedIdx'] ) ) {
			$preset_key = sanitize_text_field( $_GET['selectedIdx'] );
			$presets    = CCBAppearanceHelper::get_appearance_data( $preset_key );

			if ( isset( $presets['data'] ) ) {
				$result = array(
					'success' => true,
					'message' => 'Theme changed',
					'data'    => $presets['data'],
					'list'    => $presets['list'],
				);

				CCBPresetGenerator::update_preset_key( $_GET['calc_id'], sanitize_text_field( $preset_key ) );
			}
		}

		wp_send_json( $result );
	}

	public static function ccb_update_preset_title() {
		check_ajax_referer( 'ccb_update_preset_title', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$result = array(
			'success' => false,
			'message' => 'Something went wrong',
		);

		if ( isset( $_GET['calc_id'] ) && isset( $_GET['selectedIdx'] ) && isset( $_GET['title'] ) ) {
			$preset_key = sanitize_text_field( $_GET['selectedIdx'] );
			$title      = apply_filters( 'ccb_sanitize_text_field', $_GET['title'] );

			CCBPresetGenerator::update_preset_title( $preset_key, $title );
			$presets = CCBAppearanceHelper::get_appearance_data( $preset_key );

			if ( isset( $presets['data'] ) ) {
				$result = array(
					'success' => true,
					'message' => 'Theme title changed',
				);
			}
		}

		wp_send_json( $result );
	}

	public static function calc_use_template() {
		check_ajax_referer( 'ccb_use_template', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$request_body = file_get_contents( 'php://input' );
		$request_data = json_decode( $request_body, true );
		$data         = apply_filters( 'stm_ccb_sanitize_array', $request_data );
		$template_id  = ! isset( $data['template_id'] ) ? null : $data['template_id'];

		$result = array(
			'url'         => '',
			'calculators' => array(),
			'success'     => false,
			'message'     => __( 'Could not duplicate calculator, please try again!', 'cost-calculator-builder' ),
		);

		if ( ! is_null( $template_id ) ) {
			$calc_id  = get_post_meta( $template_id, 'calc_id', true );
			$category = get_post_meta( $template_id, 'category', true );
			$data     = self::duplicate_target_calc( $calc_id, false );

			if ( 'custom_templates' !== $category ) {
				$data['preset_idx'] = 'default';
			}

			if ( ccb_update_calc_values( $data ) ) {
				$result['success'] = true;
				$result['message'] = __( 'Calculator created successfully', 'cost-calculator-builder' );
				$result['url']     = admin_url( 'admin.php' ) . '?page=cost_calculator_builder&action=edit&id=' . $data['id'];
			}
		}

		wp_send_json( $result );
	}

	public static function calc_config_settings() {
		check_ajax_referer( 'ccb_save_config', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}
		$result = array(
			'success' => false,
			'message' => __( 'Could not save calculator config, please try again!', 'cost-calculator-builder' ),
		);

		$request_body = file_get_contents( 'php://input' );
		$request_data = json_decode( $request_body, true );
		$data         = apply_filters( 'stm_ccb_sanitize_array', $request_data );

		if ( isset( $data['calc_id'] ) ) {
			update_post_meta( $data['calc_id'], 'category', $data['category'] );
			update_post_meta( $data['calc_id'], 'icon', $data['icon'] );
			update_post_meta( $data['calc_id'], 'plugin_type', $data['type'] );
			update_post_meta( $data['calc_id'], 'calc_link', $data['link'] );
			update_post_meta( $data['calc_id'], 'info', $data['info'] );
			update_post_meta( $data['calc_id'], 'description', $data['description'] );

			$result['success'] = true;
			$result['message'] = __( 'Calculator configs saved successfully', 'cost-calculator-builder' );
		}

		wp_send_json( $result );
	}

	public static function create_sample_calculator() {
		if ( empty( get_option( 'calc_sample_calculator', '' ) ) ) {
			update_option( 'calc_sample_calculator', true );
			$simple_calculator_path = CALC_PATH . '/demo-sample/sample_calculator.txt';

			if ( file_exists( $simple_calculator_path ) ) {
				$contents = file_get_contents( $simple_calculator_path ); //phpcs:ignore
				$contents = json_decode( $contents, true );
				$contents = json_decode( wp_json_encode( $contents ), true );

				if ( isset( $contents['calculators'] ) ) {
					foreach ( $contents['calculators'] as $calculator ) {
						if ( isset( $calculator['ccb_fields'] ) ) {
							$res = null;
							CCBExportImport::addCalculatorData( $calculator, $res );
						}
					}
					update_option( 'ccb_quick_tour_type', 'quick_tour_start' );
				}
			}
		}
	}

	public static function ccb_rollback_handler() {
		check_ajax_referer( 'ccb_rollback_nonce', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$result = array(
			'success' => false,
			'message' => __( 'Could not rollback settings, please try again!', 'cost-calculator-builder' ),
		);

		if ( ! isset( $_GET['calc_id'] ) || ! isset( $_GET['key'] ) ) {
			wp_send_json( $result );
		}

		$calc_id = $_GET['calc_id'];
		$key     = $_GET['key'];
		$sp_list = get_post_meta( $calc_id, 'ccb_savepoint_list', true );

		if ( empty( $sp_list ) || ! isset( $sp_list[ $key ] ) ) {
			wp_send_json( $result );
		}

		$savepoint_data = $sp_list[ $key ]['data'];
		$calc_data      = CCBSettingsData::settings_data();
		$sp_settings    = apply_filters( 'stm_ccb_sanitize_array', $savepoint_data['settings'] );
		$settings       = ccb_array_merge_recursive_left_source( $calc_data, $sp_settings );

		$update_data = array(
			'id'         => $calc_id,
			'title'      => $savepoint_data['title'],
			'formula'    => $savepoint_data['formula'],
			'settings'   => $settings,
			'builder'    => $savepoint_data['builder'],
			'conditions' => $savepoint_data['conditions'],
			'preset_idx' => $savepoint_data['preset_idx'],
		);

		if ( ccb_update_calc_values( $update_data ) ) {
			$result['success'] = true;
			$result['message'] = __( 'Changes successfully rolled back', 'cost-calculator-builder' );
		}

		wp_send_json( $result );
	}

	public static function savepoint( $data = array() ) {
		if ( empty( $data ) ) {
			return false;
		}

		$calc_id = null;
		if ( isset( $data['calc_id'] ) ) {
			$calc_id = $data['calc_id'];
		}

		if ( isset( $data['id'] ) ) {
			$calc_id = $data['id'];
		}

		$sp_list     = get_post_meta( $calc_id, 'ccb_savepoint_list', true );
		$current_key = ccb_generate_random_string();

		if ( ! $sp_list ) {
			$sp_list = array();
		}

		if ( count( $sp_list ) > 2 ) {
			array_shift( $sp_list );
		}

		$calc_data   = CCBSettingsData::settings_data();
		$sp_settings = apply_filters( 'stm_ccb_sanitize_array', $data['settings'] );
		$settings    = ccb_array_merge_recursive_left_source( $calc_data, $sp_settings );

		$savepoint = array(
			'title'      => ! empty( $data['title'] ) ? sanitize_text_field( $data['title'] ) : __( 'empty name', 'cost-calculator-builder' ),
			'formula'    => ! empty( $data['formula'] ) ? apply_filters( 'stm_ccb_sanitize_array', $data['formula'] ) : array(),
			'settings'   => $settings,
			'builder'    => ! empty( $data['builder'] ) ? apply_filters( 'stm_ccb_sanitize_array', $data['builder'] ) : array(),
			'conditions' => ! empty( $data['conditions'] ) ? apply_filters( 'stm_ccb_sanitize_array', $data['conditions'] ) : array(),
			'preset_idx' => get_post_meta( $calc_id, 'ccb_calc_preset_idx', true ),
		);

		$timezone_format = _x( 'H:i - d.m.Y', 'timezone date format' );
		$timestamp       = date_i18n( $timezone_format );

		$sp_list[ $current_key ] = array(
			'data'  => $savepoint,
			'basic' => array(
				'timestamp' => $timestamp,
			),
		);

		update_post_meta( $calc_id, 'ccb_savepoint_list', $sp_list );

		return true;
	}

	private static function clear_all_history() {
		$calculators = CCBUpdatesCallbacks::get_calculators();

		foreach ( $calculators as $calculator ) {
			delete_post_meta( $calculator->ID, 'ccb_savepoint_list' );
		}
	}

	public static function calc_delete_payment() {
		check_ajax_referer( 'ccb_delete_payment', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$result = array(
			'success' => false,
			'message' => 'Something went wrong',
			'data'    => null,
		);

		$request_body = file_get_contents( 'php://input' );
		$request_data = json_decode( $request_body, true );
		$data         = apply_filters( 'stm_ccb_sanitize_array', $request_data );

		if ( isset( $data['type'] ) ) {
			$general_settings = CCBSettingsData::general_settings_data();
			if ( isset( $general_settings['payment_gateway']['cards']['card_payments'][ $data['type'] ] ) ) {
				$result['success'] = true;
				$result['data']    = $general_settings['payment_gateway']['cards']['card_payments'][ $data['type'] ];
				$result['message'] = 'Payment deleted';
			}
		}

		wp_send_json( $result );
	}

	public static function ccb_update_banner() {
		check_ajax_referer( 'ccb_update_banner', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$result = array(
			'success' => false,
			'message' => 'Could not update banner',
		);

		if ( isset( $_GET['calc_id'] ) && ! empty( $_GET['type'] ) ) {
			$type             = sanitize_text_field( $_GET['type'] );
			$calc_id          = intval( $_GET['calc_id'] );
			$general_settings = CCBSettingsData::get_calc_global_settings();
			$banner_id        = '';

			if ( isset( $general_settings['sticky']['used_banner'] ) ) {
				$banner_id = intval( $general_settings['sticky']['used_banner'] );
			}

			$value = 'banner' === $type ? $calc_id : null;
			if ( ! isset( $general_settings['sticky'] ) ) {
				$general_settings['sticky'] = array(
					'used_banner' => $value,
				);
			} else {
				$banner_id = $general_settings['sticky']['used_banner'];
				if ( ( 'btn' === $type && intval( $banner_id ) === $calc_id ) || intval( $banner_id !== $calc_id ) || empty( $banner_id ) ) {
					$general_settings['sticky']['used_banner'] = $value;
				}
			}

			update_option( 'ccb_general_settings', apply_filters( 'calc_update_options', $general_settings ) );

			$calculators = CCBUpdatesCallbacks::get_calculators();
			if ( $calc_id !== $banner_id && 'banner' === $type ) {
				foreach ( $calculators as $calculator ) {
					$calc_settings = CCBSettingsData::get_calc_single_settings( $calculator->ID );
					if ( $calc_id !== $calculator->ID && isset( $calc_settings['sticky_calc']['display_type'] ) && 'banner' === $calc_settings['sticky_calc']['display_type'] ) {
						$calc_settings['sticky_calc']['display_type'] = 'btn';
					} elseif ( $calc_id === $calculator->ID ) {
						$calc_settings['sticky_calc']['display_type'] = $type;
					}

					CCBSettingsData::update_calc_single_settings( $calculator->ID, $calc_settings );
				}
			} elseif ( 'btn' === $type ) {
				$calc_settings                                = CCBSettingsData::get_calc_single_settings( $calc_id );
				$calc_settings['sticky_calc']['display_type'] = 'btn';
				CCBSettingsData::update_calc_single_settings( $calc_id, $calc_settings );
			}

			$result['success'] = true;
			$result['message'] = 'Sticky banner updated';
		}

		wp_send_json( $result );
	}


	public static function ccb_extend_pdf_template() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$result = array(
			'success' => false,
			'message' => 'Something went wrong',
			'data'    => array(),
		);

		$request_body = file_get_contents( 'php://input' );
		$request_data = json_decode( $request_body, true );
		$data         = apply_filters( 'stm_ccb_sanitize_array', $request_data );

		if ( ! empty( $data ) && isset( $data['key'] ) ) {
			$key       = $data['key'];
			$templates = CCBPdfManagerTemplates::ccb_get_templates();

			if ( isset( $templates[ $key ] ) ) {
				$new_key             = CCBPdfManagerTemplates::ccb_get_new_id_for_pdf_template();
				$new_template        = CCBPdfManagerTemplates::ccb_get_template_skeleton( $templates[ $key ] );
				$new_template['key'] = $new_key;

				$result['success'] = true;
				$result['message'] = 'Success';
				$result['data']    = $new_template;
			}
		}

		wp_send_json( $result );
	}

	public static function ccb_edit_pdf_template() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$result = array(
			'success' => false,
			'message' => 'Something went wrong',
			'data'    => array(),
		);

		$request_body = file_get_contents( 'php://input' );
		$request_data = json_decode( $request_body, true );
		$data         = apply_filters( 'stm_ccb_sanitize_array', $request_data );

		if ( ! empty( $data ) && isset( $data['key'] ) ) {
			$key       = $data['key'];
			$templates = CCBPdfManager::ccb_get_pdf_template();

			if ( isset( $templates[ $key ] ) ) {
				CCBPdfManager::update_template_key( $key );
				$new_template = CCBPdfManagerTemplates::ccb_get_template_skeleton( $templates[ $key ] );

				$result['success'] = true;
				$result['message'] = 'Success';
				$result['data']    = $new_template;
			}
		}

		wp_send_json( $result );
	}

	public static function ccb_restore_pdf_template() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$result = array(
			'success' => false,
			'message' => 'Something went wrong',
			'data'    => array(),
		);

		$request_body = file_get_contents( 'php://input' );
		$request_data = json_decode( $request_body, true );
		$data         = apply_filters( 'stm_ccb_sanitize_array', $request_data );

		if ( ! empty( $data ) && isset( $data['key'] ) ) {
			$key              = $data['key'];
			$saved_templates  = CCBPdfManager::ccb_get_pdf_template();
			$templates        = CCBPdfManagerTemplates::ccb_get_templates();
			$default_template = $templates['default'];

			$default_template['key'] = $key;
			if ( isset( $saved_templates[ $key ] ) ) {
				$new_template = CCBPdfManagerTemplates::ccb_get_template_skeleton( $default_template );

				$result['success'] = true;
				$result['message'] = 'Templated reset successfully';
				$result['data']    = $new_template;
			}
		}

		wp_send_json( $result );
	}

	public static function ccb_delete_pdf_template() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
		}

		$result = array(
			'success' => false,
			'message' => 'Something went wrong',
			'data'    => array(),
		);

		$request_body = file_get_contents( 'php://input' );
		$request_data = json_decode( $request_body, true );
		$data         = apply_filters( 'stm_ccb_sanitize_array', $request_data );

		if ( ! empty( $data ) && isset( $data['key'] ) ) {
			$key         = $data['key'];
			$current_key = CCBPdfManager::get_template_key();

			if ( $key === $current_key ) {
				$result['message'] = 'Cannot delete current template';
				wp_send_json( $result );
			}

			$templates = CCBPdfManager::ccb_get_pdf_template();
			if ( ! isset( $templates[ $key ] ) ) {
				$result['message'] = 'Template does not exists';
				wp_send_json( $result );
			}

			unset( $templates[ $key ] );
			CCBPdfManager::update_tempaltes( $templates );

			$result['success']           = true;
			$result['message']           = 'Template deleted successfully';
			$result['data']['templates'] = CCBPdfManagerTemplates::ccb_get_templates_list();
		}

		wp_send_json( $result );
	}

	public static function ccb_calc_views() {
		check_ajax_referer( 'ccb_calc_views', 'nonce' );

		$result = array(
			'success' => false,
			'message' => 'Invalid data',
		);

		if ( isset( $_POST['data'] ) ) {
			$data = ccb_convert_from_btoa( $_POST['data'] );
			if ( ! ccb_is_convert_correct( $data ) ) {
				wp_send_json( $result );
			}
		}

		$data = CCBCleanHelper::cleanData( (array) json_decode( stripslashes( $data ) ) );

		if ( isset( $data['calc_id'] ) ) {
			$calc_id    = intval( $data['calc_id'] );
			$ip_address = $_SERVER['REMOTE_ADDR'];

			$views = AnalyticsViews::create_calculator_views( $calc_id, $ip_address );

			if ( $views ) {
				$result['success'] = true;
				$result['message'] = 'Success';
			}
		}

		wp_send_json( $result );
	}

	public static function ccb_calc_interactions() {
		check_ajax_referer( 'ccb_calc_interactions', 'nonce' );

		$result = array(
			'success' => false,
			'message' => 'Invalid data',
		);

		if ( isset( $_POST['data'] ) ) {
			$data = ccb_convert_from_btoa( $_POST['data'] );
			if ( ! ccb_is_convert_correct( $data ) ) {
				wp_send_json( $result );
			}
		}

		$data = CCBCleanHelper::cleanData( (array) json_decode( stripslashes( $data ) ) );

		if ( isset( $data['calc_id'] ) ) {
			$calc_id = intval( $data['calc_id'] );

			$interactions = AnalyticsInteractions::create_calculator_interactions( $calc_id );

			if ( $interactions ) {
				$result['success'] = true;
				$result['message'] = 'Success';
			}
		}

		wp_send_json( $result );
	}
}
