<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Request;

use App\Models\AppSession;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected $performer = null;
    protected $user = null;
    protected $sessionKey = null;
    protected $appInternalName = null;
    protected $stringMappingConfig = null;

    public  function __construct()
    {
        $this->sessionKey = Cookie::get(config('app.sessionKeyName'));
        $this->performer = AppSession::where('session_key', $this->sessionKey)->where('is_active', true)->first();
        if($this->performer){
            $this->user = $this->performer ? $this->performer->user : null;
        }

        $this->appInternalName = config('app.internalName');
        $this->stringMappingConfig = config('constants.' . $this->appInternalName);
        
        view()->share('appInternalName', $this->appInternalName);
        view()->share('stringMappingConfig', $this->stringMappingConfig);
        view()->share('performer', $this->performer);
        view()->share('user', $this->user);
    }

    public function callJava($endPoint, $data, $sessionKey)
    {
        $url = config('app.backendUrl') . $endPoint;
        $response =   Http::asForm()
            ->withHeaders([
                'sessionKey' => $sessionKey
            ])
            ->post($url, $data)
            ->body();

        return json_decode($response, true);
    }
    public function pass($data = null): array
    {
        return [
            'success' => true,
            'data' => $data,
        ];
    }
    public function fail($message): array
    {
        return [
            'success' => false,
            'message' => $message
        ];
        return null;
    }

    public function filterMultiQuery(Request $request, $query, $columnName, $keyName, $valueName1, $valueName2) {
        switch($request->input($keyName)) {
            case 'EXACTLY':
                if($request->input($valueName1)) {
                    $query->where($columnName, $request->input($valueName1));
                }
                break;
            case 'LESS_THAN':
                if($request->input($valueName1)) {
                    $query->where($columnName, '<', $request->input($valueName1));
                }
                break;
            case 'GREATER_THAN':
                if($request->input($valueName1)) {
                    $query->where($columnName, '>', $request->input($valueName1));
                }
                break;
            case 'BETWEEN':
                if($request->input($valueName1) && $request->input($valueName2)) {
                    $query
                        ->where($columnName, '>=', $request->input($valueName1))
                        ->where($columnName, '<=', $request->input($valueName2));
                }
                break;
            case 'NOT_BETWEEN':
                if($request->input($valueName1) && $request->input($valueName2)) {
                    $query
                        ->where(function ($q) use ($request, $columnName, $valueName1, $valueName2) {
                            $q->where($columnName, '<', $request->input($valueName1))
                                ->orWhere($columnName, '>', $request->input($valueName2));
                        });
                }
                break;
        }
    }
    public function filterDateMultiQuery(Request $request, $query, $columnName, $keyName, $valueName1, $valueName2) {
        switch($request->input($keyName)) {
            case 'EXACTLY':
                if($request->input($valueName1)) {
                    $query->whereDate($columnName, $request->input($valueName1));
                }
                break;
            case 'LESS_THAN':
                if($request->input($valueName1)) {
                    $query->whereDate($columnName, '<', $request->input($valueName1));
                }
                break;
            case 'GREATER_THAN':
                if($request->input($valueName1)) {
                    $query->whereDate($columnName, '>', $request->input($valueName1));
                }
                break;
            case 'BETWEEN':
                if($request->input($valueName1) && $request->input($valueName2)) {
                    $query
                        ->whereDate($columnName, '>=', $request->input($valueName1))
                        ->whereDate($columnName, '<=', $request->input($valueName2));
                }
                break;
            case 'NOT_BETWEEN':
                if($request->input($valueName1) && $request->input($valueName2)) {
                    $query
                        ->whereDate(function ($q) use ($request, $columnName, $valueName1, $valueName2) {
                            $q->whereDate($columnName, '<', $request->input($valueName1))
                                ->orWhereDate($columnName, '>', $request->input($valueName2));
                        });
                }
                break;
        }
    }
    public function filterSimpleQuery(Request $request, $query, $columnName, $valueName) {
        if($request->input($valueName)) {
            $query->where($columnName, $request->input($valueName));
        }
    }
    public function filterSimpleQueryByValue(Request $request, $query, $columnName, $valueName) {
        if($valueName) {
            $query->where($columnName, $valueName);
        }
    }
    
}
