<?php

namespace App\Http\Controllers\Admin;

use Exception;
use App\Models\User;
use Illuminate\View\View;
use App\Services\Notification\NotificationService;
use Illuminate\Http\Request;
use App\Models\Notification;
use App\Models\NotificationDescription;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\NotificationRequest;
use Illuminate\Http\RedirectResponse;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class NotificationController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/notifications';
    /** View folder */
    private $viewDirectory= 'admin.notifications.';
    private $notification_service ;
    public function __construct(NotificationService $notification_service)
    {
        $this->notification_service = $notification_service;
    }

    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $action= route('notifications.store');
        $users = User::cursor();
        $action= route('notifications.store');
        $head= __('notifications.new');
        return view( $this->viewDirectory.'form', compact('action', 'head','users','action') );
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param NotificationRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(NotificationRequest $request) :RedirectResponse
    {
        $type = 'management';
        if(is_null($request->user_id))
        {
            $users = User::get();
            foreach($users as $user)
            {
                $this->notification_service->sendNotification($type,$user->id,null,$request->title_ar,$request->title_en,$request->description_ar,$request->description_en);
            }
        }
        else
        {
            $user = User::find($request->user_id);
            $this->notification_service->sendNotification($type,$user->id,null,$request->title_ar,$request->title_en,$request->description_ar,$request->description_en);
        }

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

    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param City $area
     * @return View
     */
    public function edit(Area $area) :View
    {
        $action= route('notifications.update', $area->id);

        $query = AreaDescription::where('area_id', $area->id)
            ->join('languages', 'languages.id', 'area_descriptions.language_id')
            ->select(['area_descriptions.*', 'languages.local']);

        $areaDescription= $query->get();
        $cities = City::where('country_id','4')->get();
        foreach ($areaDescription as $row) {
            $area[$row->local]= $row;
        }

        return view( $this->viewDirectory.'form', compact('area','cities', 'action') );
    }

    /**
     * Update the specified resource in storage.
     *
     * @param NotificationRequest $request
     * @param City $area
     * @return RedirectResponse|Redirector
     */
    public function update(NotificationRequest $request, Area $area) :RedirectResponse
    {

        // Update image and category id if request has it
        $area->update($request->all());

        // update image to youtube api thumbnail if request have not image


        // Delete old description
        AreaDescription::where('area_id', $area->id)->delete();


        // Insert new description
        $this->saveData( $request, $area->id );

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

    /**
     * Handle Save form data
     *
     * @param NotificationRequest $request
     * @param int $area_id
     * @return void
     */

    private function saveData(NotificationRequest $request, int $area_id ):void
    {
        $requestData= $request->all();
        foreach (languages() as $lang) {
            $data=[
                'area_id'=> $area_id,
                'language_id'=> $lang->id,
                'name'=> $requestData['name_'.$lang->local],

            ];

            AreaDescription::create($data);
        }



    }






}
