<?php

namespace App\Http\Controllers\Webprofile\Backend;

use App\Http\Controllers\Controller;
use App\Models\Webprofile\Categories;
use App\Repositories\Webprofile\De\InformationRepository as DeInformationRepository;
use App\Repositories\Webprofile\En\InformationRepository as EnInformationRepository;
use App\Repositories\Webprofile\InformationRepository;
use App\Repositories\Webprofile\Sa\InformationRepository as SaInformationRepository;
use App\Repositories\Webprofile\Zh\InformationRepository as ZhInformationRepository;
use Illuminate\Http\Request;
use Statickidz\GoogleTranslate;

class InformationController 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(
        InformationRepository $repo,
        EnInformationRepository $repoEn,
        DeInformationRepository $repoDe,
        SaInformationRepository $repoSa,
        ZhInformationRepository $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.informations.index')->withTitle(trans('feature.information'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categories = Categories::pluck('name', 'id');

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

        return view('webprofile.backend.informations.create', $data)->withTitle(trans('feature.create_information'));
    }

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

        array_key_exists('info_status', $data) ? $data['info_status'] = 1 : $data['info_status'] = 0;
        $data['slug'] = str_slug($request->input('title'));

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

        if (webprofilesetting()['auto_translate'] == 1) {
            // save translate
            $this->createEn($data, $save);
            $this->createDe($data, $save);
            $this->createSa($data, $save);
            $this->createZh($data, $save);
        }

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

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

        if (strip_tags($data['content']) == null) {
            $data['content'] = 'kosong';
        }

        $content = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['content']));

        $dataEn['information_id'] = $information->id;
        $dataEn['title'] = $title;
        $dataEn['content'] = $content;

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

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

        if (strip_tags($data['content']) == null) {
            $data['content'] = 'kosong';
        }

        $content = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['content']));

        $dataDe['information_id'] = $information->id;
        $dataDe['title'] = $title;
        $dataDe['content'] = $content;

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

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

        if (strip_tags($data['content']) == null) {
            $data['content'] = 'kosong';
        }

        $content = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['content']));

        $dataSa['information_id'] = $information->id;
        $dataSa['title'] = $title;
        $dataSa['content'] = $content;

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

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

        if (strip_tags($data['content']) == null) {
            $data['content'] = 'kosong';
        }

        $content = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['content']));

        $dataZh['information_id'] = $information->id;
        $dataZh['title'] = $title;
        $dataZh['content'] = $content;

        $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']);
        $categories = Categories::pluck('name', 'id');

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

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

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

        array_key_exists('info_status', $data) ? $data['info_status'] = 1 : $data['info_status'] = 0;
        $data['slug'] = str_slug($request->input('title'));

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

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

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

    public function updateEn($data, $information)
    {
        $dataEn['title'] = $data['title_en'];
        $dataEn['content'] = $data['content_en'];

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

    public function updateDe($data, $information)
    {
        $dataDe['title'] = $data['title_de'];
        $dataDe['content'] = $data['content_de'];

        $this->repoDe->update($dataDe, $information);
    }

    public function updateSa($data, $information)
    {
        $dataSa['title'] = $data['title_sa'];
        $dataSa['content'] = $data['content_sa'];

        $this->repoSa->update($dataSa, $information);
    }

    public function updateZh($data, $information)
    {
        $dataZh['title'] = $data['title_zh'];
        $dataZh['content'] = $data['content_zh'];

        $this->repoZh->update($dataZh, $information);
    }

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