<?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)
    {
        // dd($request);
        $request->validate([
            'name' => 'required'
        ], [
            'name.required' => 'Kategori wajib diisi',
            // 'name.regex' => 'Kategori tidak valid! Kategori hanya berupa angka dan huruf',
            // 'name.max' => 'Kategori terlalu panjang'
        ]);

        $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']);
        $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,
        ];
        // dd($data);

        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)
    {
        // dd($request);
        $request->validate([
            'name' => 'required',
        ], [
            'name.required' => 'Kategori wajib diisi',
            // 'name.regex' => 'Kategori tidak valid! Kategori hanya berupa angka dan huruf',
            // 'name.max' => 'Kategori terlalu panjang',
        ]);
        // dd($request);
        $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']);
        // // dd($request, $dataEn);
        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);
        // dd($dataEn, $category, $request->manual);
        
        
        $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);
        // dd($data, $category, $request->manual);
        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);
        // }
            // dd($data, $category, $manual);
            
            // dd($data, $category, $manual);
    
            if($manual==1){
                $dataEn['name'] = $data['name_en'];
            }
            else{
                if (strip_tags($data['name']) == null) {
                    $data['name'] = 'kosong';
                }

                $trans = new GoogleTranslate();
                $name = $trans->translate($this->SOURCE, $this->TARGET, $data['name']);
                $dataEn['name'] = $name;
                // dd($dataEn);
            }
            // dd($category);
            $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'] = $data['name_de'];
        }
        else{
            if (strip_tags($data['name']) == null) {
                $data['name'] = 'kosong';
            }

            $trans = new GoogleTranslate();
            $name = $trans->translate($this->SOURCE, $this->TARGETDE, $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'] = $data['name_sa'];
        }
        else{
            if (strip_tags($data['name']) == null) {
                $data['name'] = 'kosong';
            }

            $trans = new GoogleTranslate();
            $name = $trans->translate($this->SOURCE, $this->TARGETSA, $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'] = $data['name_zh'];
        }
        else{
            if (strip_tags($data['name']) == null) {
                $data['name'] = 'kosong';
            }

            $trans = new GoogleTranslate();
            $name = $trans->translate($this->SOURCE, $this->TARGETZH, $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']);
    }
}
