<?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)
    {
        $request['name'] = strip_tags($request->name);
        $request->validate([
            'name' => 'required'
        ], [
            'name.required' => 'Kategori wajib diisi',
        ]);

        $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, strip_tags($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, strip_tags($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, strip_tags($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, strip_tags($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']);
        $manual=0;

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

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

    public function editPerBahasa($id)
    {
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
        $manual=1;

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

        return view('webprofile.backend.categories.edit_per_bahasa', $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)
    {
        $request['name'] = strip_tags($request->name);
        $request->validate([
            'name' => 'required',
        ], [
            'name.required' => 'Kategori wajib diisi',

        ]);
        $data = $request->except(['_token', 'manual', 'id', 'name_en', 'name_de', 'name_sa', 'name_zh']);
        // // $dataEn = $request->except(['_token', 'id', 'name_de']);
        $dataEn = $request->except(['_token','id', 'manual']);
        // $dataEn = $request->except(['_token', 'manual', 'id', 'name_en', 'name_de', 'name_sa', 'name_zh']);
        // $dataEn['name_en'] = htmlspecialchars($request->name_en);
        // $dataEn['name_de'] = htmlspecialchars($request->name_de);
        // $dataEn['name_sa'] = htmlspecialchars($request->name_sa);
        // $dataEn['name_zh'] = htmlspecialchars($request->name_zh);
        // // $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, $request->manual);
        $this->updateDe($dataEn, $category, $request->manual);
        $this->updateSa($dataEn, $category, $request->manual);
        $this->updateZh($dataEn, $category, $request->manual);
        $this->repo->update($data, $category);

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

    public function updateEn($data, $category, $manual)
    {
        // $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);
        // }

            if($manual==1){
                $dataEn['name'] = strip_tags($data['name_en']);
            }
            else{
                if ($data['name'] == null) {
                    $data['name'] = 'kosong';
                }

                $trans = new GoogleTranslate();
                $name = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['name']));
                $dataEn['name'] = $name;
            }

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

    public function updateDe($data, $category, $manual)
    {
        // $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);
        // }

        if($manual==1){
            $dataDe['name'] = strip_tags($data['name_de']);
        }
        else{
            if ($data['name'] == null) {
                $data['name'] = 'kosong';
            }

            $trans = new GoogleTranslate();
            $name = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['name']));
            $dataDe['name'] = $name;
        }
        $this->repoDe->update($dataDe, $category);
    }

    public function updateSa($data, $category, $manual)
    {
        // $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);
        // }

        if($manual==1){
            $dataSa['name'] = strip_tags($data['name_sa']);
        }
        else{
            if ($data['name'] == null) {
                $data['name'] = 'kosong';
            }

            $trans = new GoogleTranslate();
            $name = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['name']));
            $dataSa['name'] = $name;
        }
        $this->repoSa->update($dataSa, $category);
    }

    public function updateZh($data, $category, $manual)
    {
        // $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);
        // }

        if($manual==1){
            $dataZh['name'] = strip_tags($data['name_zh']);
        }
        else{
            if ($data['name'] == null) {
                $data['name'] = 'kosong';
            }

            $trans = new GoogleTranslate();
            $name = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['name']));
            $dataZh['name'] = $name;
        }
        $this->repoZh->update($dataZh, $category);
    }

    /**
     * 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']);
    }
}
