<?php

namespace Mehrab\Front\Providers;


use Illuminate\Support\Facades\Gate;
use Mamad\Cart\Repository\CartRepository;
use Mamad\Category\Repositories\CategoryRepository;
use Mamad\Club\Services\ClubService;
use Mamad\Menu\Repository\MenuRepository;
use Mamad\Page\Repository\PageRepository;
use Mamad\Product\Service\ProductService;
use Mamad\Setting\Repository\SettingRepository;
use Mamad\Shop\Repository\ShopRepository;
use Mamad\Shop\Service\ShopService;
use Mamad\ThemeSetting\Repository\ThemeSettingRepository;
use Mamad\Wallet\Services\WalletService;
use Mehrab\Front\Http\Middleware\DashboardUserMiddleware;
use Mehrab\Front\Policies\FrontPolicy;
use Mehrab\Front\Policies\PanelUserPolicy;
use Mehrab\Service\Repositories\ServiceRepo;
use Mehrab\User\Models\User;
use Mehrab\User\Policies\UserPolicy;

class FrontServiceProvider extends \Illuminate\Support\ServiceProvider
{
    public function register()
    {
        $this->loadRoutesFrom(__DIR__."/../Routes/front_routes.php");
        $this->loadViewsFrom(__DIR__.'/../Resources/Views/','Front');
//        $this->loadJsonTranslationsFrom(__DIR__.'/../Resources/Lang/');
        $this->app['router']->aliasMiddleware('DashboardUserMiddleware',DashboardUserMiddleware::class);
//        Gate::policy(User::class,FrontPolicy::class);

    }

    public function boot()
    {
        view()->composer('Front::Layout.head',function ($view){
            $meta=[
                "bing_api"=>SettingRepository::getOption("bing_api"),
                "google_api"=>SettingRepository::getOption("google_api")
            ];
            $view->with(compact("meta"));
        });
        view()->composer('Front::Layout.header',function ($view){
            $categories = (new ServiceRepo())->all_Parent_active();
            $menu=MenuRepository::get_menu_with_children();
            $cart=CartRepository::get_cart();
            $options["site_number"]=ThemeSettingRepository::getOption("site_number")[0]??null;
            $cart_products=ProductService::get_cart_products($cart);
            $cart_total_price=ProductService::get_cart_products_total_price();
            $cart_products_total_number=ProductService::get_cart_products_total_number();
            $product_categories = CategoryRepository::get_parent_with_children_by_two_type("product","group_product");
            $view->with(compact('options','cart','menu','categories','product_categories','cart_products','cart_total_price','cart_products_total_number'));
        });

        $theme_cart_use_wallet_path="Front::cart_use_wallet";
        view()->composer($theme_cart_use_wallet_path, function ($view) {
            $wallet=WalletService::user_has_amount_in_wallet();
            $view->with([
                "wallet"=>$wallet
            ]);
        });

        $theme_cart_use_club_path="Front::cart_use_club";
        view()->composer($theme_cart_use_club_path, function ($view) {
            $club=ClubService::user_can_use_club(null,true,false);
            $view->with([
                "club"=>$club
            ]);
        });

        $cart_total_box_view="Front::cart_total_box";
        view()->composer($cart_total_box_view, function ($view) {
            $amount=ProductService::get_cart_products_total_price(false);
            $cart_products_total_price=ProductService::get_cart_products_total_price(true,null,false);
            $total_price=ShopService::add_delivery_price_to_amount($amount);
            $club_point=ClubService::get_point_number($amount,"product");
            $use_wallet=WalletService::decreasing_wallet_amount($total_price);
            $use_club=ClubService::decreasing_club_amount($total_price);
            $total_price=WalletService::decrease_wallet_from_amount($total_price);
            $total_price=ClubService::decrease_club_from_amount($total_price);
            $cart_products_offer_price=ProductService::get_cart_products_offer_price();
            $delivery_price=ShopService::get_delivery_price($amount);
            $view->with([
                "total_box_cart_products_total_price"=>$cart_products_total_price,
                "total_box_cart_products_offer_price"=>$cart_products_offer_price,
                "delivery_price"=>$delivery_price,
                "total_price"=>$total_price,
                "use_wallet"=>$use_wallet,
                "use_club"=>$use_club,
                "club_point"=>$club_point,
            ]);
        });

        view()->composer("Front::service_use_club", function ($view) {

            $club=ClubService::user_can_use_club();
            $view->with([
                "club"=>$club
            ]);
        });


        view()->composer("Front::Layout.footer", function ($view) {
            $options["footer_main"]=ThemeSettingRepository::getOption("footer_main")[0]??null;
            $options["footer_description"]=ThemeSettingRepository::getOption("footer_description")[0]??null;
            $options["footer_two"]=ThemeSettingRepository::getOption("footer_two")[0]??null;
            $options["footer_four"]=ThemeSettingRepository::getOption("footer_four")[0]??null;
            $pages=PageRepository::get_by_limit(5);
            $view->with([
                "options"=>$options,
                "pages"=>$pages
            ]);
        });
    }

}

