<?php

namespace App\Http\Controllers\Admin;

use App\Module;
use App\Setting;
use App\Log;
use App\Language;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Routing\Redirector;
use App\Http\Controllers\Controller;
use App\Http\Requests\ModuleRequest;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests\SettingRequest;
use Illuminate\Http\RedirectResponse;

class SettingController extends Controller
{
    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/dashboard/settings';
    /** View folder */
    private $viewDirectory= 'admin.settings.';

    /**
     * Display a listing of the resource.
     * @return View
     */
    public function index() :View
    {
        $title= __('settings.head');
        $head = metaFields('settings', 'add_new', getCurrentLocale()) ?? __('settings.new');

        $settingsAction= route('settings.store');
        $modulesAction= route('settings.modules');
        $settings = Setting::all();
        $modules = Module::get();
        
        
        
        if(count($settings) > 0){
            $head = metaFields('settings', 'edit', getCurrentLocale()) ?? __('settings.edit');
            foreach ($settings as $setting) {
                $settings->{$setting['key']} = $setting['value'];
            }
        } else {
            $settings = null;
        }

        return view($this->viewDirectory.'form',compact('title','head', 'settingsAction','modulesAction', 'settings', 'modules'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param SettingRequest $settingRequest
     * @return RedirectResponse|Redirector
     */
    public function store(SettingRequest $settingRequest) :RedirectResponse
    {
       $this->saveData($settingRequest);
        Log::create(['user_id'=>\Auth::user()->id,'type'=>'الاعدادات','action'=>'تعديل']);
        return redirect($this->redirectSuccessPath)->with('message', __('dashboard.saveDone'));
    }

    /**
     * Store modules metas in modules table.
     *
     * @param ModuleRequest $moduleRequest
     * @return RedirectResponse|Redirector
     */
    public function modules(ModuleRequest $moduleRequest)
    {
        $fields = $moduleRequest->fields;
        $moduleName = $moduleRequest->module_name;

        $data = array_merge_recursive($moduleName, $fields);

        foreach ($data as $path => $field) {
            Module::where('path', $path)->update([
                'meta' => json_encode($field)
            ]);
        }

        return redirect($this->redirectSuccessPath)->with('message', __('dashboard.saveDone'));
    }


    /**
     * Handle Save form data
     *
     * @param SettingRequest $request
     * @return void
     */

    private function saveData(SettingRequest $request):void
    {
        $data = $request->all();
        $userRole = Auth::user()->role;

        !$request->has('website_disable') && $userRole == 'super_admin' ? $data['website_disable'] = 0 : 1;
        !$request->has('dashboard_disable') && $userRole == 'super_admin' ? $data['dashboard_disable'] = 0 : 1;

        if ($request->has('update')){
            foreach ($data as $key => $value) {
                Setting::where('key', $key)->update([
                    'value' => $value
                ]);
            }
        } else {
            foreach ($data as $key => $value) {
                Setting::create([
                    'key' => $key,
                    'value' => $value,
                ]);
            }
        }
    }

}
