<?php

namespace App\Http\Controllers;

use App\Http\Resources\MenuResource;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use App\Menu;

class MenuController extends Controller
{
    const ITEM_PER_PAGE = 25;

    public function index()
    {
        $list = Menu::with('slider')->select('id', 'name', 'type', 'link', 'category_id', 'parent_id', 'is_show_card')
        ->isPublished()
        ->orderBy('order')
        ->orderBy('id')
        ->get();
        $rs = $this->getChildItems($list, 0);

        return response()->json(['data' => $rs], 200);
    }

    public function getChildItems($list, $parent = 0) {
        $rs = [];
        foreach ($list as $item) {
            if($item->parent_id == $parent) {
                $item['is_show_card'] = ($item['is_show_card'] == 1);
                $item['elements'] = $this->getChildItems($list, $item->id);
                $item['hasChild'] = (count($item['elements']) > 0) ? true : false;
                $rs[] = $item;
            }
        }

        return $rs;
    }

    public function header(Request $request) {
        $params = $request->all();
        $type = (isset($params['type']) && $params['type'] == 'nested') ? 'nested' : 'list';

        if ($type == 'nested') {
            $list = Cache::remember(env('APP_CACHE_PREFIX', 'gxomens') . '_menu_header_nested', env('APP_CACHE_TIME', 604800), function () {
                $items = Menu::with('slider')->select('id', 'name', 'type', 'link', 'image', 'parent_id', 'position', 'is_show_card')
                    ->isPublished()
                    ->where('position', 'header')
                    ->orderBy('order')
                    ->orderBy('id')
                    ->get();
                $rs = $this->getChildItems($items, 0);

                return ['items' => $rs, 'lastModify' => (gmdate('D, d M Y H:i:s') . ' GMT')];
            });
        } else {
            $list = Cache::remember(env('APP_CACHE_PREFIX', 'gxomens') . '_menu_header', env('APP_CACHE_TIME', 604800), function () {
                $items = Menu::with('slider')->select('id', 'name', 'type', 'link', 'image', 'position', 'is_show_card')
                    ->isPublished()
                    ->where('position', 'header')
                    ->orderBy('order')
                    ->orderBy('id')
                    ->get();

                return ['items' => $items, 'lastModify' => (gmdate('D, d M Y H:i:s') . ' GMT')];
            });
        }

        return response()->json(['data' => $list['items']], 200)->header('Last-Modified', $list['lastModify']);
    }

    public function footer() {
        $list = Cache::remember(env('APP_CACHE_PREFIX', 'gxomens') . '_menu_footer', env('APP_CACHE_TIME', 604800), function () {
            $items = Menu::with('element')->select('id', 'name', 'type', 'link', 'position', 'parent_id')
                ->isPublished()
                ->where('position', 'footer')
                ->where('parent_id', 0)
                ->orderBy('order')
                ->orderBy('id')
                ->limit(6)
                ->get();

            return ['items' => $items, 'lastModify' => (gmdate('D, d M Y H:i:s') . ' GMT')];
        });

        return response()->json(['data' => $list['items']], 200)->header('Last-Modified', $list['lastModify']);
    }
}
