<?php

namespace UkrSolution\BarcodeScanner\API;

use UkrSolution\BarcodeScanner\API\actions\CartScannerActions;
use UkrSolution\BarcodeScanner\API\actions\DbActions;
use UkrSolution\BarcodeScanner\API\actions\ManagementActions;
use UkrSolution\BarcodeScanner\API\actions\OrdersActions;
use UkrSolution\BarcodeScanner\API\actions\PostActions;
use UkrSolution\BarcodeScanner\API\actions\UsersActions;
use UkrSolution\BarcodeScanner\API\classes\ACFRepeater;
use UkrSolution\BarcodeScanner\API\classes\BatchNumbers;
use UkrSolution\BarcodeScanner\API\classes\BatchNumbersWebis;
use UkrSolution\BarcodeScanner\API\classes\OrdersHelper;
use UkrSolution\BarcodeScanner\API\classes\PostsList;
use UkrSolution\BarcodeScanner\API\classes\Users;
use UkrSolution\BarcodeScanner\API\stripe\StripeTerminalService;
use UkrSolution\BarcodeScanner\Database;
use UkrSolution\BarcodeScanner\features\Debug\Debug;
use UkrSolution\BarcodeScanner\features\history\History;
use UkrSolution\BarcodeScanner\features\logs\Logs;
use UkrSolution\BarcodeScanner\features\mobile\MobileRouter;
use UkrSolution\BarcodeScanner\features\settings\PermissionsHelper;
use UkrSolution\BarcodeScanner\features\settings\Settings;
use UkrSolution\BarcodeScanner\features\settings\SettingsHelper;
use WP_REST_Request;


class AjaxRoutes
{
    private $coreInstance = null;

    public function __construct($post, $get, $coreInstance = null)
    {
        Debug::addPoint("AjaxRoutes->start");

        $this->coreInstance = $coreInstance;

        if (isset($post["rout"]) && $post["rout"]) {
            $route = sanitize_text_field($post["rout"]);

            add_filter('scanner_filter_cart_item_price', function ($productId, $price, $customFilter) {
                return $price;
            }, 1, 3);

            RequestHelper::initMemoryLimit();


            $settings = new Settings($this->coreInstance);
            $postActions = new PostActions();
            $managementActions = new ManagementActions();
            $ordersActions = new OrdersActions();
            $usersActions = new UsersActions();
            $dbActions = new DbActions();
            $cartActions = new CartScannerActions();
            $request = new WP_REST_Request("", "");

            $token = $this->getParam($get, "token", "");
            $routes = new Routes();
            $request->set_param("token", $token);

            $tokenUserId = $routes->getUserId($request);
            $request->set_param("platform", $this->getParam($get, "platform", ""));
            $checker = $routes->permissionCallback($request);
            $request->set_param("token_user_id", $tokenUserId);

            if ($tokenUserId) {
                $userLocale = get_user_meta($tokenUserId, 'locale', true);
                if ($userLocale) {
                    switch_to_locale($userLocale);
                }

                $userRole = $tokenUserId ? Users::getUserRole($tokenUserId) : '';
                Users::setUserId($tokenUserId);
                Users::setUserRole($userRole);

                $platform = $this->getParam($get, "platform", "");

                if ($platform == "android" || $platform == "ios") {
                    wp_set_current_user($tokenUserId);

                    Users::updateAppUsesTime($tokenUserId);
                }
            }

            if (!PluginsHelper::is_plugin_active('woocommerce/woocommerce.php')) {
                $MobileRouter = new MobileRouter();
                $platform = $this->getParam($get, "platform", "");
                echo json_encode(array(
                    "errors" => array("WooCommerce is not activated"),
                    "cartErrors" => array(
                        array("notice" => "WooCommerce is not activated", "htmlMessageClass" => "err_woocommerce_is_not_activated")
                    ),
                ));
                wp_die();
            }

            if (
                !$checker && !in_array($route, array(
                    "recalculate",
                    "backgroundIndexing",
                    "checkOtherPrices",
                    "checkFieldName",
                    "exportLog",
                    "saveLog",
                    "indexingClearTable",
                    "getHistory",
                    "getItemsList"
                ))
            ) {
                $MobileRouter = new MobileRouter();
                $platform = $this->getParam($get, "platform", "");

                $data = array("redirect" => 0, "data" => array("rout" => "invalid route"), "f" => 1);

                if ($platform == "android" || $platform == "ios") {
                    $urlData = $MobileRouter->getParamsFromPlainUrl();
                    $jsData = $this->coreInstance->adminEnqueueScripts(true, true, $urlData);
                    $usbs = $jsData && isset($jsData['usbs']) ? $jsData['usbs'] : array();
                    $data['usbs'] = $usbs;
                }

                echo json_encode($data);
                exit;
            }

            PermissionsHelper::init($request);

            $keysString = array(
                "id",
                "orderId",
                "noteId",
                "userId",
                "itemId",
                "customField",
                "recordId",
                "query",
                "withVariation",
                "productId",
                "quantity",
                "price",
                "title",
                "postId",
                "attachmentId",
                "field",
                "reqbs",
                "postAutoAction",
                "postAutoField",
                "postAutoActionQtyStep",
                "status",
                "autoFill",
                "byId",
                "setQty",
                "orderCustomPrice",
                "orderCustomShipping",
                "orderCustomShippingTax",
                "orderCustomSubPrice",
                "orderCustomTax",
                "orderCustomCashGot",
                "orderStatus",
                "shippingMethod",
                "paymentMethod",
                "key",
                "session",
                "sessionStamp",
                "orderUserId",
                "productQty",
                "tab",
                "param",
                "str",
                "orderAutoAction",
                "orderAutoStatus",
                "autoEnableIndexation",
                "slug",
                "country",
                "customAction",
                "userAction",
                "bsInstanceFrontendStatus",
                "confirmationLeftFulfillment",
                "isCheck",
                "isAddToList",
                "modifyAction",
                "isNew",
                "fulfillmentOrderId",
                "confirmed",
                "isUpdateShipping",
                "coupon",
                "customerId",
                "isPay",
                "fieldName",
                "parentId",
                "isMainImage",
                "withoutStatuses",
                "attributeName",
                "attributeValue",
                "loadCustomerData",
                "taxonomy",
                "ignoreIncrease",
                "note",
                "resultSortBy",
                "checkFulfillment",
                "request_priority",
                "content",
                "type",
                "integration",
            );
            $keysArray = array(
                "filter",
                "customFilter",
                "filterExcludes",
                "products",
                "fields",
                "searchAttributes",
                "progress",
                "userData",
                "currentItems",
                "itemsCustomPrices",
                "cartItem",
                "extraData",
                "orderCustomTaxes",
                "resetCustomPrices",
                "inputs",
                "data",
                "categories",
                "locations",
                "codes",
                "lines",
                "autoAction",
                "options",
                "items",
                "filterResult",
                "address",
                "batches",
                "batchesWebis",
                "shipmentTrackingItems",
                "currentIds",
                "postTypes",
                "value",
                "globalAttributes",
                "customAttributes",
                "globalOptions",
                "customOptions",
                "ids",
                "checkForDeletedIds",
                "prices",
                "values",
                "source",
                "metaFields",
                "payload"
            );
            $response = array();

            foreach ($keysString as $key) {
                $value = $this->getParam($post, $key, "");
                $value = wp_unslash(sanitize_post_field($key, $value, 0, 'db'));
                $request->set_param($key, $value);
            }

            foreach ($keysArray as $key) {
                $request->set_param($key, $this->getParam($post, $key, array()));
            }

            $_POST["bsInstanceFrontendStatus"] = $request->get_param("bsInstanceFrontendStatus");

            SettingsHelper::updateSettings($request);

            switch ($route) {
                case 'getPost':
                    PermissionsHelper::onePermRequired(['inventory', 'orders']);
                    $response = $postActions->postSearch($request);
                    break;
                case 'getProduct':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod', 'orders', 'cart']);
                    $response = $managementActions->productSearch($request);
                    break;
                case 'getProducts':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod', 'orders', 'cart']);
                    $response = $managementActions->getProducts($request);
                    break;
                case 'getOrder':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $managementActions->orderSearch($request);
                    break;
                case 'productEnableManageStock':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->productEnableManageStock($request);
                    break;
                case 'updateProductQuantity':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->productUpdateQuantity($request);
                    break;
                case 'updateProductQuantityPlus':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->productUpdateQuantityPlus($request);
                    break;
                case 'updateProductQuantityMinus':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->productUpdateQuantityMinus($request);
                    break;
                case 'updateProductRegularPrice':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->productUpdateRegularPrice($request);
                    break;
                case 'updateProductSalePrice':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->productUpdateSalePrice($request);
                    break;
                case 'updateProductCustomPrice':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->updateProductCustomPrice($request);
                    break;
                case 'updateProductMeta':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->productUpdateMeta($request);
                    break;
                case 'updatePostStatus':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->productUpdateStatus($request);
                    break;
                case 'updateTitle':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->productUpdateTitle($request);
                    break;
                case 'setImage':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->productSetImage($request);
                    break;
                case 'createNew':
                    PermissionsHelper::onePermRequired(['newprod']);
                    $response = $managementActions->productCreateNew($request);
                    break;
                case 'reloadNewProduct':
                    PermissionsHelper::onePermRequired(['newprod']);
                    $response = $managementActions->reloadNewProduct($request);
                    break;
                case 'update':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->productUpdateFields($request);
                    break;
                case 'uploadPostImage':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->uploadPostImage($request);
                    break;
                case 'removePostImages':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->removePostImages($request);
                    break;
                case 'removePostMainImage':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->removePostMainImage($request);
                    break;
                case 'sortProductGallery':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->sortProductGallery($request);
                    break;
                case 'changeStatus':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $managementActions->orderChangeStatus($request);
                    break;
                case 'changeCustomer':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $managementActions->orderChangeCustomer($request);
                    break;
                case 'changeOrderAddress':
                    PermissionsHelper::onePermRequired(['orders']);
                    PermissionsHelper::onePermRequired(['order_edit_address']);
                    $response = $managementActions->changeOrderAddress($request);
                    break;
                case 'orderItemUpdate':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $managementActions->orderItemUpdate($request);
                    break;
                case 'orderChangeShippingMethod':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $managementActions->orderChangeShippingMethod($request);
                    break;
                case 'orderChangePaymentMethod':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $managementActions->orderChangePaymentMethod($request);
                    break;
                case 'orderChangeOrderNote':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $managementActions->orderChangeOrderNote($request);
                    break;
                case 'orderChangeOrderCoupon':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $managementActions->orderChangeOrderCoupon($request);
                    break;
                case 'orderReCalculate':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $managementActions->orderReCalculate($request);
                    break;
                case 'updateOrderMeta':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $managementActions->updateOrderMeta($request);
                    break;
                case 'updateOrderMetaFields':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $managementActions->updateOrderMetaFields($request);
                    break;
                case 'orderUpdateItemsMeta':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $managementActions->orderUpdateItemsMeta($request);
                    break;
                case 'orderUpdateItemMeta':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = rest_ensure_response($managementActions->orderUpdateItemMeta($request));
                    break;
                case 'updateFoundCounter':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod', 'orders', 'cart']);
                    $response = $managementActions->updateFoundCounter($request);
                    break;
                case 'uploadPick':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->uploadPick($request);
                    break;
                case 'getProductCategories':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->getProductCategories($request);
                    break;
                case 'getProductTaxonomy':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->getProductTaxonomy($request);
                    break;
                case 'updateCategories':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->updateCategories($request);
                    break;
                case 'updateTaxonomy':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->updateTaxonomy($request);
                    break;
                case 'updateAttributes':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->updateAttributes($request);
                    break;
                case 'getGlobalAttributes':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->getGlobalAttributes($request);
                    break;
                case 'createGlobalAttributeValue':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = $managementActions->createGlobalAttributeValue($request);
                    break;
                case 'importCodes':
                    PermissionsHelper::onePermRequired(['inventory']);
                    $response = $managementActions->importCodes($request);
                    break;
                case 'getItemsList':
                    PermissionsHelper::onePermRequired(['inventory']);
                    $response = rest_ensure_response(array("productsList" => PostsList::getList(Users::getUserId($request))));
                    break;
                case 'updateItemsFromList':
                    PermissionsHelper::onePermRequired(['inventory']);
                    $response = $managementActions->updateItemsFromList($request);
                    break;
                case 'removeItemsListRecord':
                    PermissionsHelper::onePermRequired(['inventory']);
                    $response = $managementActions->removeItemsListRecord($request);
                    break;
                case 'clearItemsList':
                    PermissionsHelper::onePermRequired(['inventory']);
                    $response = $managementActions->clearItemsList($request);
                    break;
                case 'getOrdersList':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $managementActions->getOrdersList($request);
                    break;
                case 'getNextOrder':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $managementActions->getNextOrder($request);
                    break;
                case 'getPrevOrder':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $managementActions->getPrevOrder($request);
                    break;
                case 'updateOrderFulfilledObject':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $managementActions->updateOrderFulfilledObject($request);
                    break;
                case 'removeFulfillmentObject':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $managementActions->removeFulfillmentObject($request);
                    break;
                case 'ff2Search':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $ordersActions->ff2Search($request);
                    break;
                case 'ff2PickItem':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $ordersActions->ff2PickItem($request);
                    break;
                case 'ff2PickCustomField':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $ordersActions->ff2PickCustomField($request);
                    break;
                case 'ff2RepickItem':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $ordersActions->ff2RepickItem($request);
                    break;
                case 'update_wc_shipment_tracking_item':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $managementActions->update_wc_shipment_tracking_item($request);
                    break;
                case 'delete_wc_shipment_tracking_item':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $managementActions->delete_wc_shipment_tracking_item($request);
                    break;
                case 'create_wc_shipment_tracking_item':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $managementActions->create_wc_shipment_tracking_item($request);
                    break;
                case 'getItemsCustomFields':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $managementActions->getItemsCustomFields($request);
                    break;
                case 'removeOrderNote':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = OrdersHelper::removeOrderNote($request);
                    break;
                case 'addOrderNote':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = OrdersHelper::addOrderNote($request);
                    break;
                case 'orderAddProduct':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = OrdersHelper::orderAddProduct($request);
                    break;
                case 'removeProduct':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = OrdersHelper::removeProduct($request);
                    break;
                case 'checkEnableFFforOrder':
                    PermissionsHelper::onePermRequired(permissions: ['orders']);
                    $response = OrdersHelper::checkEnableFFforOrder($request);
                    break;
                case 'disableFFforOrder':
                    PermissionsHelper::onePermRequired(permissions: ['orders']);
                    $response = OrdersHelper::disableFFforOrder($request);
                    break;
                case 'setEditingFlagToOrder':
                    PermissionsHelper::onePermRequired(permissions: ['orders']);
                    $response = OrdersHelper::setEditingFlagToOrder($request);
                    break;
                case 'stripeFetchConnectionToken':
                    PermissionsHelper::onePermRequired(permissions: ['orders']);
                    $response = StripeTerminalService::stripeFetchConnectionToken($request);
                    break;
                case 'stripeCreatePaymentIntent':
                    PermissionsHelper::onePermRequired(permissions: ['orders']);
                    $response = StripeTerminalService::stripeCreatePaymentIntent($request);
                    break;
                case 'usersFind':
                    PermissionsHelper::onePermRequired(['linkcustomer', 'plugin_settings']);
                    $response = $usersActions->find($request);
                    break;
                case 'getUsersByIds':
                    PermissionsHelper::onePermRequired(['linkcustomer', 'plugin_settings']);
                    $response = $usersActions->getUsersByIds($request);
                    break;
                case 'getRoleData':
                    PermissionsHelper::onePermRequired(['linkcustomer', 'plugin_settings']);
                    $response = $usersActions->getRoleData($request);
                    break;
                case 'userCreate':
                    PermissionsHelper::onePermRequired(['orders', 'cart']);
                    PermissionsHelper::onePermRequired(['linkcustomer']);
                    $response = $usersActions->createUser($request);
                    break;
                case 'getStates':
                    PermissionsHelper::onePermRequired(['orders', 'cart']);
                    $response = $usersActions->getStates($request);
                    break;
                case 'addItem':
                    PermissionsHelper::onePermRequired(['cart']);
                    $response = $cartActions->addItem($request);
                    break;
                case 'removeItem':
                    PermissionsHelper::onePermRequired(['cart']);
                    $response = $cartActions->removeItem($request);
                    break;
                case 'updateQuantity':
                    PermissionsHelper::onePermRequired(['cart']);
                    $response = $cartActions->updateQuantity($request);
                    break;
                case 'updateCartItemAttributes':
                    PermissionsHelper::onePermRequired(['cart']);
                    $response = $cartActions->updateAttributes($request);
                    break;
                case 'clear':
                    PermissionsHelper::onePermRequired(['cart']);
                    $response = $cartActions->cartClear($request);
                    break;
                case 'orderCreate':
                    PermissionsHelper::onePermRequired(['cart']);
                    $response = $cartActions->orderCreate($request);
                    break;
                case 'getStatuses':
                    PermissionsHelper::onePermRequired(['orders', 'cart']);
                    $response = $cartActions->getStatuses($request);
                    break;
                case 'recalculate':
                    PermissionsHelper::onePermRequired(['cart']);
                    $response = $cartActions->cartRecalculate($request);
                    break;
                case 'resetCustomPrices':
                    PermissionsHelper::onePermRequired(['cart']);
                    $response = $cartActions->resetCustomPrices($request);
                    break;
                case 'createColumn':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod', 'orders', 'cart']);
                    $response = $dbActions->createColumn($request);
                    break;
                case 'saveSession':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod', 'orders', 'cart']);
                    $response = $dbActions->saveSession($request);
                    break;
                case 'saveSettings':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod', 'orders', 'cart']);
                    $response = $dbActions->saveSettings($request);
                    break;
                case 'backgroundIndexing':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod', 'orders', 'cart']);
                    Database::pluginUpdateHistory();
                    $response = $dbActions->backgroundIndexing($request);
                    break;
                case 'indexingClearTable':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod', 'orders', 'cart']);
                    $response = $dbActions->indexingClearTable($request);
                    break;
                case 'checkOtherPrices':
                    PermissionsHelper::onePermRequired(['plugin_settings']);
                    $response = $postActions->checkOtherPrices($request);
                    break;
                case 'checkFieldName':
                    PermissionsHelper::onePermRequired(['plugin_settings']);
                    $response = $postActions->checkFieldName($request);
                    break;
                case 'updateSettings':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod', 'orders', 'cart']);
                    $response = $settings->updateSettingsArray($request);
                    break;
                case 'loadSettings':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod', 'orders', 'cart']);
                    $response = $settings->loadSettingsArray($request);
                    break;
                case 'appUsersUpdate':
                    PermissionsHelper::onePermRequired(['plugin_settings']);
                    $MobileRouter = new MobileRouter();
                    $userSessions = $this->getParam($post, "str", "");
                    $platform = $this->getParam($get, "platform", "");
                    $settings->updateSettings("userSessions", $userSessions, "text");
                    $uid = $this->getParam($post, "userId", "");
                    $userAction = $this->getParam($post, "userAction", "");
                    $usersIds = $this->getParam($post, "ids", array());

                    $password = "";

                    if ($uid == 0 && $usersIds) {
                        foreach ($usersIds as $_uid) {
                            if ($_uid && $userAction == "add") {
                                $password = $usersActions->usersGenerateOtp();
                                update_user_meta($_uid, "barcode_scanner_app_otp", md5($password));
                                update_user_meta($_uid, "barcode_scanner_app_otp_expired_dt", time());
                            }
                            if ($_uid && $userAction == "remove") {
                                update_user_meta($_uid, "barcode_scanner_app_otp", "");
                                update_user_meta($_uid, "barcode_scanner_app_otp_expired_dt", "");
                                update_user_meta($_uid, "barcode_scanner_app_auth_method", "");
                            }
                        }
                        $password = "";
                    } else {
                        if ($uid && $userAction == "add") {
                            $password = $usersActions->usersGenerateOtp();
                            update_user_meta($uid, "barcode_scanner_app_otp", md5($password));
                            update_user_meta($uid, "barcode_scanner_app_otp_expired_dt", time());
                        }
                        if ($uid && $userAction == "remove") {
                            update_user_meta($uid, "barcode_scanner_app_otp", "");
                            update_user_meta($uid, "barcode_scanner_app_otp_expired_dt", "");
                            update_user_meta($uid, "barcode_scanner_app_auth_method", "");
                        }
                    }

                    $urlData = $MobileRouter->getParamsFromPlainUrl();
                    $jsData = $this->coreInstance->adminEnqueueScripts(true, true, $urlData);
                    $usbs = $jsData && isset($jsData['usbs']) ? $jsData['usbs'] : array();
                    $response = rest_ensure_response(array("usbs" => $usbs, "password" => $password));
                    break;
                case 'appUserUpdatePassword':
                    PermissionsHelper::onePermRequired(['plugin_settings']);
                    $response = $usersActions->updatePassword($request);
                    break;
                case 'setUserMeta':
                    PermissionsHelper::onePermRequired(['orders']);
                    $response = $usersActions->setUserMeta($request);
                    break;
                case 'exportLog':
                    PermissionsHelper::onePermRequired(['plugin_logs']);
                    $logs = new Logs();
                    $response = $logs->export($request);
                    break;
                case 'getHistory':
                    PermissionsHelper::onePermRequired(['inventory', 'orders', 'cart']);
                    $response = rest_ensure_response(array("history" => History::getByUser(Users::getUserId($request))));
                    break;

                case 'usbs_find_order_save_id':
                    PermissionsHelper::onePermRequired(['plugin_settings']);
                    $response = $settings->saveTemplateOrderId($request);
                    break;
                case 'usbs_find_order':
                    PermissionsHelper::onePermRequired(['plugin_settings']);
                    $response = $settings->findOrder($request);
                    break;

                case 'batchNumbersRemoveBatch':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = BatchNumbers::removeBatch($request);
                    break;
                case 'batchNumbersAddNewBatch':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = BatchNumbers::addNewBatch($request);
                    break;
                case 'batchNumbersSaveBatchField':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = BatchNumbers::saveBatchField($request);
                    break;

                case 'batchNumbersWebisRemoveBatch':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = BatchNumbersWebis::removeBatch($request);
                    break;
                case 'batchNumbersWebisAddNewBatch':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = BatchNumbersWebis::addNewBatch($request);
                    break;
                case 'batchNumbersWebisSaveBatchField':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $response = BatchNumbersWebis::saveBatchField($request);
                    break;
                case 'integrationsUpdate':
                    PermissionsHelper::onePermRequired(['inventory', 'newprod']);
                    $integration = "UkrSolution\BarcodeScanner\API\classes\\" . $request->get_param("integration");

                    if (class_exists($integration) && method_exists($integration, "update")) {
                        $response = $integration::update($request);
                    }
                    break;
                default: {
                    echo json_encode(array(
                        "errors" => array("Invalid input"),
                        "token" => $token
                    ));
                    exit;
                }
            }

            $filter = $this->getParam($post, "filter", array());
            if ($filter && in_array($route, array("checkCustomFields", "createColumn"))) {
                $settings = new Settings();
                $settings->updateSettings('search_filter', json_encode($filter));
            }

            PluginsHelper::responseHeaders();

            if ($response && $response->data) {
                $updatedTimestamp = $settings->getSettings("updated_timestamp");
                $response->data["settings_updated_timestamp"] = $updatedTimestamp ? $updatedTimestamp->value : "";
                $response->data["microtime"] = microtime(true);
                echo json_encode($response->data);
            } else {
                echo json_encode("error");
            }
            exit;
        }
    }

    private function getParam($post, $key, $default = null)
    {
        if (isset($post[$key])) {
            return $post[$key];
        } else {
            return $default;
        }
    }
}
