<?php

namespace App\Http\Controllers\Webprofile\Backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\Webprofile\CategoryRepository;
use App\Repositories\Webprofile\En\CategoryRepository as EnCategoryRepository;
use Statickidz\GoogleTranslate;

class CategoryController extends Controller
{
    private $repo;
    private $repoEn;

    private $SOURCE = 'id';
    private $TARGET = 'en';

    public function __construct(
        CategoryRepository $repo,
        EnCategoryRepository $repoEn
    ){
        $this->repo = $repo;
        $this->repoEn = $repoEn;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $data = $this->repo->get(['rEn']);
            return $this->repo->datatable($data);
        }

        return view('webprofile.backend.categories.index')->withTitle(trans('feature.category'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('webprofile.backend.categories.create')->withTitle(trans('feature.create_category'));
    }

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

        array_key_exists('is_active', $data) ? $data['is_active'] = 1 : $data['is_active'] = 0;

        $category = $this->repo->store($data);

        $this->createEn($data, $category);

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

    private function createEn($data, $category)
    {
        $trans = new GoogleTranslate();
        $name = $trans->translate($this->SOURCE, $this->TARGET, $data['name']);

        $dataEn['category_id'] = $category->id;
        $dataEn['name'] = $name;

        $this->repoEn->store($dataEn);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = $this->repo->findId($id);

        $data = [
            'data' => $data,
        ];

        return view('webprofile.backend.categories.edit', $data)->withTitle(trans('feature.edit_category'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->except(['_token', 'id', 'name_en']);
        $dataEn = $request->except(['_token', 'id']);

        array_key_exists('is_active', $data) ? $data['is_active'] = 1 : $data['is_active'] = 0;

        $category = $this->repo->findId($id, ['rEn']);
        $edit = $this->repo->update($data, $category);

        $this->updateEn($dataEn, $category);

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

    public function updateEn($data, $category)
    {
        $dataEn['name'] = $data['name_en'];

        $this->repoEn->update($dataEn, $category->rEn);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = $this->repo->findId($id, ['rEn']);
        $this->repo->destroy($data);

        if ($data->rEn) {
            $this->repoEn->destroy($data->rEn);
        }

        return response()->json(['done']);
    }
}
