<?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\PostRepository as EnPostRepository;
use App\Repositories\Webprofile\PostRepository;
use Statickidz\GoogleTranslate;

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

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

    public function __construct(
        PostRepository $repo,
        EnPostRepository $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.posts.index')->withTitle(trans('feature.post'));
    }

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

    /**
     * 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('post_status', $data) ? $data['post_status'] = 1 : $data['post_status'] = 0;
        array_key_exists('cover_status', $data) ? $data['cover_status'] = 1 : $data['cover_status'] = 0;
        $data['slug'] = str_slug($request->input('title'));

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

        $tipe = 'thumbnail';
        if ($request->hasFile($tipe)) {
            $img[$tipe] = $save->id.'.'.$request->file($tipe)->guessClientExtension();

            $this->repo->upload($img[$tipe], $request, $tipe);
            $this->repo->update($img, $save);
        }

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

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

    private function createEn($data, $post)
    {
        $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['post_id'] = $post->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)
    {
        $setting = webprofilesetting();
        $data = $this->repo->findId($id, ['rEn']);
        $categories = Categories::pluck('name', 'id');

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

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

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

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

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

        $tipe = 'thumbnail';
        if ($request->hasFile($tipe)) {
            $img[$tipe] = $post->id.'.'.$request->file($tipe)->guessClientExtension();

            $this->repo->upload($img[$tipe], $request, $tipe);
            $this->repo->update($img, $post);
        }

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

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

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

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

    /**
     * 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, 'thumbnail');
        $this->repo->deletefile($data, 'thumbnail');

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

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