<?php

namespace App\Http\Controllers;

use App\DataTables\MenuDataTable;
use App\Http\Requests\CreateMenuRequest;
use App\Http\Requests\UpdateMenuRequest;
use App\Http\Controllers\AppBaseController;
use App\Models\Categories;
use App\Repositories\MenuRepository;
use App\Services\ImageService;
use Illuminate\Http\Request;
use Flash;
use Illuminate\Validation\ValidationException;

class MenuController extends AppBaseController
{
  /** @var MenuRepository $menuRepository*/
  private $menuRepository;

  public function __construct(MenuRepository $menuRepo)
  {
    $this->menuRepository = $menuRepo;
  }

  /**
   * Display a listing of the Menu.
   */
  public function index(MenuDataTable $menuDataTable)
  {
    return $menuDataTable->render('menus.index');
  }


  /**
   * Show the form for creating a new Menu.
   */
  public function create()
  {
    return view('menus.create');
  }

  /**
   * Store a newly created Menu in storage.
   */
  public function store(CreateMenuRequest $request, ImageService $imageService)
  {
    $input = $request->all();
    if (request()->file('image_name')) {
      $image_name = request()->file('image_name');

      if (isset($image_name)) {
        $imageService = new ImageService();
        $file_name = $imageService->uploadWithSize(request(), null, 200);
        $input['image_name'] = $file_name;
      }
    }
    if (!isset($input['status'])) {
      $input['status'] = null;
    }
    $menu = $this->menuRepository->create($input);

    Flash::success('Menu saved successfully.');

    return redirect(route('menus.index'));
  }

  /**
   * Display the specified Menu.
   */
  public function show($id)
  {
    $menu = $this->menuRepository->find($id);

    if (empty($menu)) {
      Flash::error('Menu not found');

      return redirect(route('menus.index'));
    }

    return view('menus.show')->with('menu', $menu);
  }

  /**
   * Show the form for editing the specified Menu.
   */
  public function edit($id)
  {
    $menu = $this->menuRepository->find($id);

    if (empty($menu)) {
      Flash::error('Menu not found');

      return redirect(route('menus.index'));
    }

    return view('menus.edit')->with('menu', $menu);
  }

  /**
   * Update the specified Menu in storage.
   */
  public function update($id, UpdateMenuRequest $request, ImageService $imageService)
  {
    $menu = $this->menuRepository->find($id);
    $input = $request->all();
    if (empty($menu)) {
      Flash::error('Menu not found');

      return redirect(route('menus.index'));
    }
    if (request()->file('image_name')) {
      $image_name = request()->file('image_name');

      if (isset($image_name)) {
        $imageService = new ImageService();
        $file_name = $imageService->uploadWithSize(request(), null, 200);
        $input['image_name'] = $file_name;
      }
    }
    if (!isset($input['status'])) {
      $input['status'] = null;
    }
    $menu = $this->menuRepository->update($input, $id);

    Flash::success('Menu updated successfully.');

    return redirect(route('menus.index'));
  }

  /**
   * Remove the specified Menu from storage.
   *
   * @throws \Exception
   */
  public function destroy($id)
  {
    $categories = Categories::where('menu_id', $id)->get();
    if ($categories->count() > 0) {
      $error['error'] = 'This menu is linked with category.';


      if (!empty($error)) {
        throw ValidationException::withMessages($error);
      }
    }

    $menu = $this->menuRepository->find($id);

    if (empty($menu)) {
      Flash::error('Menu not found');

      return redirect(route('menus.index'));
    }

    $this->menuRepository->delete($id);

    Flash::success('Menu deleted successfully.');

    return redirect(route('menus.index'));
  }
}
