<?php

namespace App\Http\Controllers\Admin;

use App\Language;
use App\Translation;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class TranslationController extends Controller
{
    public function __construct()
    {
        if ( alreadyInstalled() ) {
            $this->middleware('auth');
        }
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $show_page = $request->show_page ?? 10;
        $lang = $request->show_lang ?? 'en';
        $languages = Language::all();

        if (isset($request->search)) {
            $translations = Language::whereLocale($lang)->first()
                            ->translations()
                            ->whereNotIn('group', ['validation', 'auth', 'passwords'])
                            ->where('text', 'like', '%'.$request->search.'%')
                            ->latest()
                            ->paginate($show_page);
        }else{
            $translations = Language::whereLocale($lang)->first()
                            ->translations()
                            ->whereNotIn('group', ['validation', 'auth', 'passwords'])
                            ->latest()
                            ->paginate($show_page);
        }

        return view('translations.index', compact(['languages', 'translations', 'lang', 'show_page']));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $group_names = Translation::all()
                        ->whereNotIn('group', ['auth', 'validation', 'passwords'])
                        ->groupBy('group')
                        ->keys()
                        ->all();
        $languages = Language::all();

        return view('translations.create', compact(['group_names', 'languages']));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        Translation::create($request->except('_token'));

        return redirect()->route('admin.translation.index');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Translation  $translation
     * @return \Illuminate\Http\Response
     */
    public function edit(Translation $translation)
    {
        $group_names = Translation::all()
                        ->whereNotIn('group', ['auth', 'validation', 'passwords'])
                        ->groupBy('group')
                        ->keys()
                        ->all();

        return view('translations.edit', compact('translation', 'group_names'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Translation  $translation
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Translation $translation)
    {
        $lang = $translation->language()->first()->code;

        $translation->touch();
        $translation->update($request->except('_token'));
        return redirect()->route('admin.translation.index', ['show_lang' => $lang]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Translation  $translation
     * @return \Illuminate\Http\Response
     */
    public function destroy(Translation $translation)
    {
        $translation->delete();
        return redirect()->back();
    }

    public function createGroup()
    {
        return view('translations.create_group');
    }

    public function storeGroup(Request $request)
    {
        $languages = Language::all();

        foreach ($languages as $lang) {
            Translation::create([
                'locale' => $lang->locale,
                'group' => $request->group
            ]);
        }

        return redirect()->route('admin.translation.index');
    }

    public function deleteGroup()
    {
        $group_names = Translation::all()->groupBy('group')->keys()->all();
        return view('translations.delete_group', compact('group_names'));
    }

    public function destroyGroup(Request $request, Translation $translation)
    {
        $translation->where('group', $request->group)->delete();

        return redirect()->route('admin.translation.index');
    }
}
