<?php

namespace App\Http\Controllers\Webprofile\Backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\Webprofile\DesignRepository;
use App\Repositories\Webprofile\De\DesignRepository as DeDesignRepository;
use App\Repositories\Webprofile\En\DesignRepository as EnDesignRepository;
use App\Repositories\Webprofile\Sa\DesignRepository as SaDesignRepository;
use App\Repositories\Webprofile\Zh\DesignRepository as ZhDesignRepository;
use Statickidz\GoogleTranslate;
use Crypt;

class LayoutController 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(
        DesignRepository $repo,
        EnDesignRepository $repoEn,
        DeDesignRepository $repoDe,
        SaDesignRepository $repoSa,
        ZhDesignRepository $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()
    {
        $layouts = $this->repo->get();
        

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

        return view('webprofile.backend.design.index', $data)->withTitle(trans('feature.layout'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return redirect()->route('layouts.index');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->except('_token');
        array_key_exists('title_show', $data) ? $data['title_show'] = 1 : $data['title_show'] = 0;

        $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('layouts.index');
    }

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

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

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

        $dataEn['design_id'] = $layouts->id;
        $dataEn['title_design'] = $title;
        $dataEn['value_design'] = $content;

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

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

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

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

        $dataDe['design_id'] = $layouts->id;
        $dataDe['title_design'] = $title;
        $dataDe['value_design'] = $content;

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

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

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

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

        $dataSa['design_id'] = $layouts->id;
        $dataSa['title_design'] = $title;
        $dataSa['value_design'] = $content;

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

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

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

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

        $dataZh['design_id'] = $layouts->id;
        $dataZh['title_design'] = $title;
        $dataZh['value_design'] = $content;

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

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data = [
            'position' => $id,
        ];

        return view('webprofile.backend.design.create', $data)->withTitle(trans('label.create') . ' ' . trans('feature.' . $id));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $layout = $this->repo->findId(Crypt::decrypt($id), ['rEn', 'rDe', 'rSa', 'rZh']);

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

        return view('webprofile.backend.design.edit', $data)->withTitle(trans('label.edit') . ' ' . trans('feature.' . $layout->name_design));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->except(['_token', 'id', 'title_design_en', 'content_en', 'title_design_de', 'content_de', 'title_design_sa', 'content_sa', 'title_design_zh', 'content_zh']);
        array_key_exists('title_show', $data) ? $data['title_show'] = 1 : $data['title_show'] = 0;
        $dataEn = $request->except(['_token', 'id']);

        $layout = $this->repo->findId($id);
        $edit = $this->repo->update($data, $layout);

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

        return redirect()->route('layouts.index');
    }
    public function updateEn($data, $layout)
    {
        $dataEn['title_design'] = $data['title_design_en'];
        $dataEn['value_design'] = $data['content_en'];

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

    public function updateDe($data, $layout)
    {
        $dataDe['title_design'] = $data['title_design_de'];
        $dataDe['value_design'] = $data['content_de'];

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

    public function updateSa($data, $layout)
    {
        $dataSa['title_design'] = $data['title_design_sa'];
        $dataSa['value_design'] = $data['content_sa'];

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

    public function updateZh($data, $layout)
    {
        $dataZh['title_design'] = $data['title_design_zh'];
        $dataZh['value_design'] = $data['content_zh'];

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = $this->repo->findId(Crypt::decrypt($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 redirect()->route('layouts.index');
    }
}