<?php

namespace App\Http\Controllers\Webprofile\Backend;

use App\Http\Controllers\Controller;
use App\Repositories\Webprofile\CategoryRepository;
use App\Repositories\Webprofile\De\CategoryRepository as DeCategoryRepository;
use App\Repositories\Webprofile\En\CategoryRepository as EnCategoryRepository;
use App\Repositories\Webprofile\Sa\CategoryRepository as SaCategoryRepository;
use App\Repositories\Webprofile\Zh\CategoryRepository as ZhCategoryRepository;
use Illuminate\Http\Request;
use Statickidz\GoogleTranslate;

class CategoryController extends Controller
{
    private $repo;
    private $repoEn;
    private $repoDe;
    private $repoSa;
    private $repoZh;

    private $SOURCE = 'id';
    private $TARGET = 'en';
    private $TARGETDE = 'de';
    private $TARGETSA = 'ar';
    private $TARGETZH = 'zh';

    public function __construct(
        CategoryRepository $repo,
        EnCategoryRepository $repoEn,
        DeCategoryRepository $repoDe,
        SaCategoryRepository $repoSa,
        ZhCategoryRepository $repoZh
    ) {
        $this->repo = $repo;
        $this->repoEn = $repoEn;
        $this->repoDe = $repoDe;
        $this->repoSa = $repoSa;
        $this->repoZh = $repoZh;
    }

    /**
     * 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.
     *
     * @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);
        $this->createDe($data, $category);
        $this->createSa($data, $category);
        $this->createZh($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);
    }

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

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

        $this->repoDe->store($dataDe);
    }

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

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

        $this->repoSa->store($dataSa);
    }

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

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

        $this->repoZh->store($dataZh);
    }

    /**
     * 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, ['rEn', 'rDe', 'rSa', 'rZh']);

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

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

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

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

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

        $this->updateEn($dataEn, $category);
        $this->updateDe($dataDe, $category);
        $this->updateSa($dataEn, $category);
        $this->updateZh($dataEn, $category);

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

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

        if ($category->rEn) {
            $this->repoDe->update($dataEn, $category->rEn);
        } else {
            $dataEn['category_id'] = $category->id;
            $dataEn['name'] = $data['name_en'];

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

    public function updateDe($data, $category)
    {
        $dataDe['name'] = $data['name_de'];

        if ($category->rDe) {
            $this->repoDe->update($dataDe, $category->rDe);
        } else {
            $dataDe['category_id'] = $category->id;
            $dataDe['name'] = $data['name_de'];

            $this->repoDe->store($dataDe);
        }
    }

    public function updateSa($data, $category)
    {
        $dataSa['name'] = $data['name_sa'];

        if ($category->rSa) {
            $this->repoSa->update($dataSa, $category->rSa);
        } else {
            $dataSa['category_id'] = $category->id;
            $dataSa['name'] = $data['name_sa'];

            $this->repoSa->store($dataSa);
        }
    }

    public function updateZh($data, $category)
    {
        $dataZh['name'] = $data['name_zh'];

        if ($category->rZh) {
            $this->repoZh->update($dataZh, $category->rZh);
        } else {
            $dataZh['category_id'] = $category->id;
            $dataZh['name'] = $data['name_zh'];

            $this->repoZh->store($dataZh);
        }
    }

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

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

            if ($data->rDe) {
                $this->repoDe->destroy($data->rDe);
            }

            if ($data->rSa) {
                $this->repoSa->destroy($data->rSa);
            }

            if ($data->rZh) {
                $this->repoZh->destroy($data->rZh);
            }
        }

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