<?php

namespace App\Http\Controllers\Webprofile\Backend;

use App\Http\Controllers\Controller;
use App\Models\Webprofile\Categories;
use App\Repositories\Webprofile\De\PagesRepository as DePagesRepository;
use App\Repositories\Webprofile\En\PagesRepository as EnPagesRepository;
use App\Repositories\Webprofile\PagesRepository;
use App\Repositories\Webprofile\Sa\PagesRepository as SaPagesRepository;
use App\Repositories\Webprofile\Zh\PagesRepository as ZhPagesRepository;
use Illuminate\Http\Request;
use Statickidz\GoogleTranslate;

class PageController 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(
        PagesRepository $repo,
        EnPagesRepository $repoEn,
        DePagesRepository $repoDe,
        SaPagesRepository $repoSa,
        ZhPagesRepository $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.pages.index')->withTitle(trans('feature.page'));
    }

    /**
     * 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.pages.create', $data)->withTitle(trans('feature.create_page'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request['content']  = strip_tags($request->content, ['a', 'br', 'p', '<b>', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe','section','small','button', 'span','style','h1','h2','h3','h4','h5','h6']);
        $request['title']  = strip_tags($request->title);
        $request->validate([
            'title' => 'required',
            'content' => 'required|min:3',
            'keys' => 'max:100'
        ], [
            'title.required' => 'Judul wajib diisi',
            'content.required' => 'Konten wajib diisi',
            'content.min' => 'Konten terlalu singkat',
            'keys.max' => 'Keyword terlalu panjang'
        ]);

        $data = $request->except('_token');
        // $data['content'] = htmlspecialchars($request->content);

        $data['slug'] = str_slug($request->input('title'));

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

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

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

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

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

        $content = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['content'], ['a', 'br', 'p', '<b>', 'i', 'u', 'ul', 'li', 'ol', 'img',  'table', 'td', 'th', 'tr', 'iframe','section','small','button', 'span','style','h1','h2','h3','h4','h5','h6']));

        $dataEn['page_id'] = $page->id;
        $dataEn['title'] = $title;
        $dataEn['content'] = $content;

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

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

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

        $content = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['content'], ['a', 'br', 'p', '<b>', 'i', 'u', 'ul', 'li', 'ol', 'img',  'table', 'td', 'th', 'tr', 'iframe','section','small','button', 'span','style','h1','h2','h3','h4','h5','h6']));

        $dataDe['page_id'] = $page->id;
        $dataDe['title'] = $title;
        $dataDe['content'] = $content;

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

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

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

        $content = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['content'], ['a', 'br', 'p', '<b>', 'i', 'u', 'ul', 'li', 'ol', 'img',  'table', 'td', 'th', 'tr', 'iframe','section','small','button', 'span','style','h1','h2','h3','h4','h5','h6']));

        $dataSa['page_id'] = $page->id;
        $dataSa['title'] = $title;
        $dataSa['content'] = $content;

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

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

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

        $content = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['content'], ['a', 'br', 'p', '<b>', 'i', 'u', 'ul', 'li', 'ol', 'img',  'table', 'td', 'th', 'tr', 'iframe','section','small','button', 'span','style','h1','h2','h3','h4','h5','h6']));

        $dataZh['page_id'] = $page->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');
        $manual=0;

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

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

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

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

        return view('webprofile.backend.pages.edit_per_bahasa', $data)->withTitle(trans('feature.edit_page'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request['content'] = strip_tags($request->content, ['a', 'br', 'p', '<b>', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe','section','small','button', 'span','style','h1','h2','h3','h4','h5','h6']);
        $request['content_en'] = strip_tags($request->content_en, ['a', 'br', 'p', '<b>', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe','section','small','button', 'span','style','h1','h2','h3','h4','h5','h6']);
        $request['content_de'] = strip_tags($request->content_de, ['a', 'br', 'p', '<b>', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe','section','small','button', 'span','style','h1','h2','h3','h4','h5','h6']);
        $request['content_sa'] = strip_tags($request->content_sa, ['a', 'br', 'p', '<b>', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe','section','small','button', 'span','style','h1','h2','h3','h4','h5','h6']);
        $request['content_zh'] = strip_tags($request->content_zh, ['a', 'br', 'p', '<b>', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe','section','small','button','span','style','h1','h2','h3','h4','h5','h6']);
        $request['title'] = strip_tags($request->title);
        $request['title_en'] = strip_tags($request->title_en);
        $request['title_de'] = strip_tags($request->title_de);
        $request['title_sa'] = strip_tags($request->title_sa);
        $request['title_zh'] = strip_tags($request->title_zh);

        $request->validate([
            'content' => 'required|min:3',
            'keys' => 'max:100'
        ], [
            'content.required' => 'Konten wajib diisi',
            'content.min' => 'Konten terlalu singkat',
            'keys.max' => 'Keyword terlalu panjang'
        ]);

        $data = $request->except(['_token', 'id', 'title_en', 'content_en', 'title_de', 'content_de', 'title_sa', 'content_sa', 'title_zh', 'content_zh', 'manual']);
        $dataEn = $request->except(['_token', 'id', 'manual']);
        // $data['title'] = htmlspecialchars($request->title);
        // $data['content'] = htmlspecialchars($request->content);

        // $dataEn = $request->except(['_token', 'id', 'manual', 'title_en', 'content_en', 'title_de', 'content_de', 'title_sa', 'content_sa', 'title_zh', 'content_zh']);
        // $dataEn['title'] = htmlspecialchars($request->title);
        // $dataEn['title_en'] = htmlspecialchars($request->title_en);
        // $dataEn['title_de'] = htmlspecialchars($request->title_de);
        // $dataEn['title_sa'] = htmlspecialchars($request->title_sa);
        // $dataEn['title_zh'] = htmlspecialchars($request->title_zh);
        // $dataEn['content'] = htmlspecialchars($request->content);
        // $dataEn['content_en'] = htmlspecialchars($request->content_en);
        // $dataEn['content_de'] = htmlspecialchars($request->content_de);
        // $dataEn['content_sa'] = htmlspecialchars($request->content_sa);
        // $dataEn['content_zh'] = htmlspecialchars($request->content_zh);

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

        $this->updateEn($dataEn, $page, $request->manual);
        $this->updateDe($dataEn, $page, $request->manual);
        $this->updateSa($dataEn, $page, $request->manual);
        $this->updateZh($dataEn, $page, $request->manual);
        $this->repo->update($data, $page);

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

    public function updateEn($data, $page, $manual)
    {
        if ($manual==1){
            $dataEn['title'] = strip_tags($data['title_en']);
            $dataEn['content'] = strip_tags($data['content_en'], ['a', 'br', 'p', '<b>', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe','section','small','button', 'span','style','h1','h2','h3','h4','h5','h6']);
        }
        else{
            if ($data['content'] == null) {
                $data['content'] = 'kosong';
            }

            $trans = new GoogleTranslate();
            $title = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['title']));
            if (strlen($data['content']) < 5000) {
            $content = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['content'], ['a', 'br', 'p', '<b>', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe','section','small','button', 'span','style','h1','h2','h3','h4','h5','h6']));
            $dataEn['content'] = $content;
            }
            $dataEn['title'] = $title;
        }

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

    public function updateDe($data, $page, $manual)
    {
        if($manual==1){
            $dataDe['title'] = strip_tags($data['title_de']);
            $dataDe['content'] = strip_tags($data['content_de'], ['a', 'br', 'p', '<b>', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe','section','small','button', 'span','style','h1','h2','h3','h4','h5','h6']);
        }
        else{
            if($data['content']==null){
                $data['content'] = 'kosong';
            }
            $trans = new GoogleTranslate();
            $title = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['title']));
            if (strlen($data['content']) < 5000) {
            $content =  $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['content'], ['a', 'br', 'p', '<b>', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe','section','small','button', 'span','style','h1','h2','h3','h4','h5','h6']));
            $dataDe['content'] = $content;
            }
            $dataDe['title'] = $title;
        }

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

    public function updateSa($data, $page, $manual)
    {
        if($manual==1){
            $dataSa['title'] = strip_tags($data['title_sa']);
            $dataSa['content'] = strip_tags($data['content_sa'], ['a', 'br', 'p', '<b>', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe','section','small','button', 'span','style','h1','h2','h3','h4','h5','h6']);
        }
        else{
            if($data['content']==null){
                $data['content'] = 'kosong';
            }

            $trans = new GoogleTranslate();
            $title = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['title']));
            if (strlen($data['content']) < 5000) {
            $content = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['content'], ['a', 'br', 'p', '<b>', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe','section','small','button', 'span','style','h1','h2','h3','h4','h5','h6']));
            $dataSa['content'] = $content;
            }
            $dataSa['title'] = $title;
        }

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

    public function updateZh($data, $page, $manual)
    {
        if($manual==1){
            $dataZh['title'] = strip_tags($data['title_zh']);
            $dataZh['content'] = strip_tags($data['content_zh'],['a', 'br', 'p', '<b>', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe','section','small','button', 'span','style','h1','h2','h3','h4','h5','h6']);
        }
        else{
            if($data['content']==null){
                $data['content'] = 'kosong';
            }

            $trans = new GoogleTranslate();
            $title = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['title']));
            if (strlen($data['content']) < 5000) {
            $content = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['content'], ['a', 'br', 'p', '<b>', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe','section','small','button', 'span','style','h1','h2','h3','h4','h5','h6']));
            $dataZh['content'] = $content;
            }
            $dataZh['title'] = $title;
        }

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

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