<?php

namespace App\Http\Controllers;

use App\DataTables\CategoriesDataTable;
use App\Http\Requests\CreateCategoriesRequest;
use App\Http\Requests\UpdateCategoriesRequest;
use App\Http\Controllers\AppBaseController;
use App\Models\Items;
use App\Repositories\CategoriesRepository;
use App\Services\ImageService;
use Illuminate\Http\Request;
use Flash;
use Illuminate\Validation\ValidationException;

class CategoriesController extends AppBaseController
{
  /** @var CategoriesRepository $categoriesRepository*/
  private $categoriesRepository;

  public function __construct(CategoriesRepository $categoriesRepo)
  {
    $this->categoriesRepository = $categoriesRepo;
  }

  /**
   * Display a listing of the Categories.
   */
  public function index(CategoriesDataTable $categoriesDataTable)
  {
    return $categoriesDataTable->render('categories.index');
  }


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

  /**
   * Store a newly created Categories in storage.
   */
  public function store(CreateCategoriesRequest $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;
    }
    $categories = $this->categoriesRepository->create($input);

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

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

  /**
   * Display the specified Categories.
   */
  public function show($id)
  {
    $categories = $this->categoriesRepository->find($id);

    if (empty($categories)) {
      Flash::error('Categories not found');

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

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

  /**
   * Show the form for editing the specified Categories.
   */
  public function edit($id)
  {
    $categories = $this->categoriesRepository->find($id);

    if (empty($categories)) {
      Flash::error('Categories not found');

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

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

  /**
   * Update the specified Categories in storage.
   */
  public function update($id, UpdateCategoriesRequest $request, ImageService $imageService)
  {
    $categories = $this->categoriesRepository->find($id);

    if (empty($categories)) {
      Flash::error('Categories not found');

      return redirect(route('categories.index'));
    }
    $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;
    }
    $categories = $this->categoriesRepository->update($input, $id);

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

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

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


      if (!empty($error)) {
        throw ValidationException::withMessages($error);
      }
    }
    $categories = $this->categoriesRepository->find($id);

    if (empty($categories)) {
      Flash::error('Categories not found');

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

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

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

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