<?php

namespace App\Helpers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Yajny\Member\Models\Currencies;
use Yajny\Models\Region;

class FormateHelper
{
    public static function cashback_type($cashback_amount, $amount_type, $rate_type = 'upto', $show_cashback = 'always')
    {
        return __trans('common.cashback.' . $rate_type) . (($amount_type == 'percent') ? round($cashback_amount, 2) . '%' : self::currencyFilter($cashback_amount)) . ($show_cashback == 'always' || $show_cashback == $rate_type ? ' ' . __trans('common.cashback.cashback') : null);
    }

    public static function was_cashback_type($amount_type, $current_cb = 0)
    {
        $cashback_amount = $current_cb / 2;
        if ($cashback_amount == 0) return null;

        $cashback_txt = (($amount_type == 'percent') ? round($cashback_amount, 2) . '%' : self::currencyFilter($cashback_amount));
        return __trans('common.store_box.was_cashback') . ' ' . $cashback_txt;
    }

    /** Success Response **/
    public static function successResponse($message, $data, $responseCode, $code)
    {
        return [
            'status' => 'success',
            'message' => $message,
            'data' => $data,
            'responseCode' => $responseCode,
            'code' => $code,
        ];
    }

    /** Error Response **/
    public static function errorResponse($message, $errors, $responseCode, $code)
    {
        if (isset($message['captcha'])) {
            if (Auth::guard('member')->check()) Auth::guard('member')->logout();
        }

        return [
            'status' => 'fail',
            'message' => $message,
            'errors' => $errors,
            'responseCode' => $responseCode,
            'code' => $code,
        ];
    }

    public static function currencyFilter($amount, Currencies $currency = null)
    {
        $amountInCurrency = self::getAmountInCurrency($amount, $currency);
        $currency = self::getCurrentCurrency($currency);
        if ($currency->display_as == 'prefix')
            return "{$currency->symbol} {$amountInCurrency}";
        return "{$amountInCurrency} {$currency->symbol}";
    }

    public static function getAmountInCurrency($amount, Currencies $currency = null): float
    {
        $currency = self::getCurrentCurrency($currency);
        return round($amount * $currency->conversion_rate, 2);
    }

    public static function currencyFormat($amount, Currencies $currency = null)
    {
        $currency = self::getCurrentCurrency($currency);
        if ($currency->display_as == 'prefix')
            return "{$currency->symbol} {$amount}";
        return "{$amount} {$currency->symbol}";
    }

    public static function getCurrentCurrency(Currencies $currency = null)
    {
        if (isset($currency))
            return $currency;

        $allCurrencies = Cache::rememberForever('currencies', function () {
            return Currencies::all()->mapWithKeys(function ($currency) {
                return [$currency->iso_code => $currency];
            });
        });

        if (Auth::guard('member')->check()) {
            $user = Auth::guard('member')->user();
            if (isset($user->country_code)) {
                return $user->getUserCurrency();
            }
        }

        $region = request('region');
        $regionCurrencyMapper = Cache::rememberForever('regionCurrencyMapper', function () {
            return Region::getRegionsCurrency();
        });

        if (isset($region) && isset($regionCurrencyMapper[$region])) {
            return $allCurrencies[$regionCurrencyMapper[$region]];
        } else {
            return $allCurrencies['USD'];
        }
    }

    public static function getCurrency()
    {
        return Cache::rememberForever('currencies', function () {
            return Currencies::all()->mapWithKeys(function ($currency) {
                return [$currency->iso_code => $currency];
            });
        });
    }

    public static function random_string($length)
    {
        return substr(str_shuffle("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
    }

    public static function searchTitleTranslation($key)
    {
        $mapping = [];
        $mapping["categories"] = __trans('common.sidebar.coupon_filter_categories_title');
        $mapping["brands"] = __trans('common.sidebar.coupon_filter_brands_title');
        $mapping["tags"] = __trans('common.sidebar.tags');
        $mapping["stores"] = __trans('common.sidebar.stores');
        $mapping["coupons"] = __trans('common.side_bar.refine_offers_type_coupons');
        $mapping["categories_coupon"] = __trans('common.sidebar.coupon_filter_categories_title');

        if ($key)
            return $mapping[$key];
        else
            return $mapping;
    }
}