<?php

namespace App\Http\Controllers\Admin;

use App\Screen;
use App\ScreenDescription;
use App\Http\Requests\ScreenRequest;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;

class ScreenController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath = '/admin/screens';
    /** View folder */
    private $viewDirectory = 'admin.screens.';

    public function __construct()
    {
        $this->redirectSuccessPath = getCurrentLocale().$this->redirectSuccessPath;
    }
    /**
     * Display a listing of the resource.
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('screens.head');
        $request= $request->toArray();
        return view($this->viewDirectory.'index',compact('title'));
    }

    /**
     * return data of the screens.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $query = Screen::orderBy('screens.id', 'DESC')
            ->join('screen_descriptions AS screenDesc', 'screens.id', 'screenDesc.screen_id')
            ->join('languages', 'languages.id', 'screenDesc.language_id')
            ->where('screenDesc.language_id', currentLanguage()->id)
            ->select(['screenDesc.first_title', 'screenDesc.second_title', 'screens.*']);

        if ($request->date_from) {
            $query->whereDate('screens.created_at', '>=', $request->date_from);
        }

        if ($request->date_to) {
            $query->whereDate('screens.created_at', '<=', $request->date_to);
        }

        if ($request->name) {
            $query->where('screenDesc.title', 'LIKE', '%'.$request->name.'%');
        }

        if ( !is_null($request->status) && $request->status == 0) {
            $query->onlyTrashed();
        }

        $screens= $query->paginate(1);

        return view($this->viewDirectory.'grid',compact('screens'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param Screen $screen
     * @return View
     */
    public function edit(Screen $screen) :View
    {
        $action= route('screens.update', $screen->id);
        $head = metaFields('screens', 'edit', getCurrentLocale()) ?? __('screens.edit');

        $query = ScreenDescription::where('screen_id', $screen->id)
            ->join('languages', 'languages.id', 'screen_descriptions.language_id')
            ->select(['screen_descriptions.*', 'languages.local']);

        $screenDescription= $query->get();

        foreach ($screenDescription as $row) {
            $screen[$row->local]= $row;
        }

        $local = getCurrentLocale();

        return view( $this->viewDirectory.'form', compact('screen', 'action', 'head', 'local') );
    }

    /**
     * Update the specified resource in storage.
     *
     * @param ScreenRequest $request
     * @param Screen $screen
     * @return RedirectResponse|Redirector
     */
    public function update(ScreenRequest $request, Screen $screen)
    {
        $screen->update($request->all());

        // Delete old description
        ScreenDescription::where('screen_id', $screen->id)->delete();

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

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

    /**
     * Handle Save form data
     *
     * @param ScreenRequest $request
     * @param int $screen_id
     * @return void
     */

    private function saveData(ScreenRequest $request, int $screen_id ):void
    {
        $requestData= $request->all();

        foreach (languages() as $lang) {
            $data=[
                'screen_id'=> $screen_id,
                'language_id'=> $lang->id,
                'first_title'=> $requestData['first_title_'.$lang->local],
                'first_description'=> $requestData['first_description_'.$lang->local],
                'second_title'=> $requestData['second_title_'.$lang->local],
                'second_description'=> $requestData['second_description_'.$lang->local],
                'third_title'=> $requestData['third_title_'.$lang->local],
                'third_description'=> $requestData['third_description_'.$lang->local],
            ];
            ScreenDescription::create($data);
        }

    }
}
