<?php

namespace App\Http\Controllers\Admin\Shop;

use App\Datatables\Admin\Shop\ArticlesDataTable;
use App\Http\Requests\Admin\Shop\StoreArticlePost;
use App\Repositories\Shop\ArticleInherited;
use App\Repositories\Shop\ArticleNatures;
use App\Repositories\Shop\Articles;
use App\Repositories\Shop\Categories;
use App\Repositories\Shop\Tags;
use Illuminate\Http\Request;

class ArticleController extends Controller
{
    public function autocomplete(Request $request, $str = false)
    {
        $str = $str ? $str : $request->input('q');

        return response()->json(Articles::autocomplete($str));
    }

    public function index(ArticlesDataTable $dataTable)
    {
        $data = [
            'article_natures' => ArticleNatures::getOptions(),
            'categories' => Categories::getOptions(),
            'tags' => Tags::getOptionsFullName(),
            'filters' => request()->only(['article_nature_id', 'category_id', 'tag_id']),
        ];

        return $dataTable->render('Admin.Shop.Articles.list', $data);
    }

    public function create()
    {
        $data = Articles::getMeta();

        return view('Admin.Shop.Articles.create', $data);
    }

    public function store(StoreArticlePost $request)
    {
        $data = $request->all();
        $data['visible'] = $data['visible'] ?? false;
        $data['homepage'] = $data['homepage'] ?? false;

        Articles::storeFull($data);

        return redirect()->route('Admin.Shop.Articles.index');
    }

    public function show($id)
    {
        $data['article'] = Articles::get($id);

        return view('Admin.Shop.Articles.view', $data);
    }

    public function edit($id)
    {
        $data = Articles::getFull($id);

        return view('Admin.Shop.Articles.edit', $data);
    }

    public function duplicate($id)
    {
        $data = Articles::getFull($id);
        // Prepare for creation: blank id/slug, tweak name to indicate copy
        $data['article']['id'] = null;
        $data['article']['slug'] = null;
        $data['article']['name'] = ($data['article']['name'] ?? '').' (copie)';

        return view('Admin.Shop.Articles.create', $data);
    }

    public function destroy($id)
    {
        return Articles::destroy($id);
    }

    public function getProductDescription($productId, $model)
    {
        $data = [
            'article' => [
                'inherited' => ArticleInherited::getInheritedByProduct($productId, base64_decode($model)),
            ],
        ];

        return view('Admin.Shop.Articles.partials.product.description', $data);
    }

    public function getProductTags($productId, $model)
    {
        $data = ArticleInherited::getInheritedByProduct($productId, base64_decode($model));

        return view('Admin.Shop.Articles.partials.product.tags', $data);
    }

    public function getProductImages($productId)
    {
        $data = [
            'article' => [
                'product_id' => $productId,
            ],
            'no_popup' => false,
        ];

        return view('Admin.Shop.Articles.partials.product.images', $data);
    }

    public function getImages(Request $request, $id = false)
    {
        $id = $id ? $id : $request->input('id');
        $data = [
            'images' => Articles::getImages($id),
        ];

        return view('components.uploader.mini-gallery-items', $data);
    }

    public function deleteImage(Request $request)
    {
        $id = $request->input('id');
        $index = $request->input('index');

        return Articles::deleteImage($id, $index);
    }

    public function toggleVisible(Request $request)
    {
        $data = Articles::toggleVisible($request->input('id'), $request->input('visible') === 'true' ? 1 : 0);

        return response()->json(['error' => 0]);
    }

    public function toggleHomepage(Request $request)
    {
        $data = Articles::toggleHomepage($request->input('id'), $request->input('homepage') === 'true' ? 1 : 0);

        return response()->json(['error' => 0]);
    }
}
