<?php

namespace App\Http\Controllers\Webprofile\Backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Webprofile\Categories;
use App\Repositories\Webprofile\En\PagesRepository as EnPagesRepository;
use App\Repositories\Webprofile\PagesRepository;
use Statickidz\GoogleTranslate;

class PageController extends Controller
{
    private $repo;
    private $repoEn;

    private $SOURCE = 'id';
    private $TARGET = 'en';

    public function __construct(
        PagesRepository $repo,
        EnPagesRepository $repoEn
    ){
        $this->repo = $repo;
        $this->repoEn = $repoEn;
    }
    /**
     * 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.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->except('_token');
        $data['slug'] = str_slug($request->input('title'));

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

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

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

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

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

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

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

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

    /**
     * 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_en', 'content_en']);
        $dataEn = $request->except(['_token', 'id']);

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

        $this->updateEn($dataEn, $page);

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

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

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = $this->repo->findId($id, ['rEn']);
        $this->repo->destroy($data);

        if ($data->rEn) {
            $this->repoEn->destroy($data->rEn);
        }

        return response()->json(['done']);
    }
}
