<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use App\Http\Resources\CategoryDetailResource;
use App\Http\Resources\CategoryResource;
use App\Category;

class CategoryController extends Controller
{
    const ITEM_PER_PAGE = 25;

    public function index(Request $request)
    {
        $searchParams = $request->all();
        $limit = Arr::get($searchParams, 'limit', static::ITEM_PER_PAGE);
        $keyword = Arr::get($searchParams, 'keyword', '');
        $status = Arr::get($searchParams, 'status', '');
        $list = Category::select('id', 'name', 'slug', 'sub_title', 'group', 'image', 'description', 'parent_id')->isPublished();

        if (!empty($keyword)) {
            $list->where('name', 'LIKE', '%' . $keyword . '%');
        }

        if ($status != '') {
            $list->where('is_activated', $status);
        }

        $list->orderBy('position', 'ASC')
            ->orderBy('id', 'ASC');

        return CategoryResource::collection($list->paginate($limit));
    }

    public function top(Request $request)
    {
        $searchParams = $request->all();
        $limit = Arr::get($searchParams, 'limit', static::ITEM_PER_PAGE);
        $list = Category::select('id', 'name', 'slug', 'sub_title', 'group', 'image', 'description')
            ->isPublished()
            ->where('parent_id', 0)
            ->orderBy('position')
            ->orderBy('id', 'DESC');

        return CategoryResource::collection($list->paginate($limit));
    }

    public function slug($slug = '')
    {
        $category = Category::select('id', 'name', 'slug', 'sub_title', 'group', 'image', 'description')
            ->isPublished()
            ->where('slug', $slug)
            ->first();

        return new CategoryResource($category);
    }

    public function show($id = '')
    {
        $category = Category::select('id', 'name', 'slug', 'sub_title', 'group', 'image', 'description')
            ->isPublished()
            ->where('id', $id)
            ->first();

        return new CategoryDetailResource($category);
    }

    public function onlyParent(Request $request)
    {
        $searchParams = $request->all();
        $limit = Arr::get($searchParams, 'limit', static::ITEM_PER_PAGE);
        $list = Category::select('id', 'name', 'slug', 'sub_title', 'group', 'image', 'description', 'parent_id')
            ->isPublished()
            ->where('parent_id', 0)
            ->orderBy('id', 'ASC');

        return CategoryResource::collection($list->paginate($limit));
    }

    public function onlyChildren(Request $request)
    {
        $searchParams = $request->all();
        $limit = Arr::get($searchParams, 'limit', static::ITEM_PER_PAGE);
        $parentId = Arr::get($searchParams, 'parentId', '');
        $parentName = Arr::get($searchParams, 'parentName', '');

        if (empty($parentId) && !empty($parentName)) {
            $parentIds = Category::where('name', $parentName)->pluck('id')->toArray();
            $parentId = $parentIds ?: [0];
        }

        $list = Category::select('id', 'name', 'slug', 'sub_title', 'group', 'image', 'description', 'parent_id')
            ->isPublished()
            ->whereNotNull('parent_id')
            ->where('parent_id', '!=', 0);

        if (is_array($parentId)) {
            $list->whereIn('parent_id', $parentId);
        } else {
            $list->where('parent_id', $parentId);
        }

        $list->orderBy('id', 'ASC');

        return CategoryResource::collection($list->paginate($limit));
    }

    public function progressImage(Request $request) {
        $params = $request->all();
        $content = json_decode(file_get_contents(public_path("data.json")), true);
        $imgId = md5(rand(1, 9999) . date('YmdHis'));
        $content[] = ['id' => $imgId, 'source' => $params['thumbnail'], 'target' => $params['target'], 'output' => $imgId . '.png', '_token' => $params['csrfToken']];
        file_put_contents(public_path("data.json"), json_encode($content));

        return response()->json(['status' => 'progressing', 'key' => $imgId], 200);
    }

    public function checkProgressImage($id = '') {
        $status = 'progressing';
        $imgName = '';

        if ($id != '') {
            $content = json_decode(file_get_contents(public_path("done.json")), true);
            $imgName = $id . '.png';
            $newList = [];
            foreach ($content as $item) {
                if ($item == $imgName) {
                    $status = 'success';
                } else {
                    $newList[] = $item;
                }
            }
            file_put_contents(public_path("done.json"), json_encode($newList));
        }

        return response()->json(['status' => $status, 'key' => $id, 'file_name' => $imgName], 200);
    }
}
