<?php

namespace App\Providers;

use App\Extensions\PaymentGateways\PayPal\PayPalSettings;
use App\Models\UsefulLink;
use App\Settings\GeneralSettings;
use App\Settings\MailSettings;
use Exception;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
use Qirolab\Theme\Theme;


class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Paginator::useBootstrap();
        Schema::defaultStringLength(191);

        Validator::extend('multiple_date_format', function ($attribute, $value, $parameters, $validator) {
            $ok = true;
            $result = [];

            // iterate through all formats
            foreach ($parameters as $parameter) {
                //validate with laravels standard date format validation
                $result[] = $validator->validateDateFormat($attribute, $value, [$parameter]);
            }

            //if none of result array is true. it sets ok to false
            if (!in_array(true, $result)) {
                $ok = false;
                $validator->setCustomMessages(['multiple_date_format' => 'The format must be one of ' . implode(',', $parameters)]);
            }

            return $ok;
        });

        // Force HTTPS if APP_URL is set to https
        if (config('app.url') && parse_url(config('app.url'), PHP_URL_SCHEME) === 'https') {
            URL::forceScheme('https');
        }

        //get the Github Branch the panel is running on
        try {
            $stringfromfile = file(base_path() . '/.git/HEAD');

            $firstLine = $stringfromfile[0]; //get the string from the array

            $explodedstring = explode('/', $firstLine, 3); //seperate out by the "/" in the string

            $branchname = $explodedstring[2]; //get the one that is always the branch name
        } catch (Exception $e) {
            $branchname = 'unknown';
            Log::notice($e);
        }
        config(['BRANCHNAME' => $branchname]);

        // Do not run this code if no APP_KEY is set
        if (config('app.key') == null) return;

        try {
            if (Schema::hasColumn('useful_links', 'position')) {
                $useful_links = UsefulLink::where("position", "like", "%topbar%")->get()->sortby("id");
                view()->share('useful_links', $useful_links);
            }
        } catch (Exception $e) {
            Log::error("Couldnt find useful_links. Probably the installation is not completet. " . $e);
        }

        $generalSettings = $this->app->make(GeneralSettings::class);
        if (!file_exists(base_path('themes') . "/" . $generalSettings->theme)) {
            $generalSettings->theme = "default";
        }

        if ($generalSettings->theme && $generalSettings->theme !== config('theme.active')) {
            Theme::set($generalSettings->theme, "default");
        } else {
            Theme::set("default", "default");
        }


        $settings = $this->app->make(MailSettings::class);
        $settings->setConfig();

    }
}
