<?php

namespace Modules\Office\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Modules\Office\Repositories\GeneratorInterface;
use Modules\Office\Repositories\GeneratorRepository;

class GeneratorController extends Controller
{
    private $generatorRepository;

    public function __construct(GeneratorInterface $generatorRepository)
    {
        $this->generatorRepository = $generatorRepository;
    }
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $data['title'] = "Generator Lists";
        $data['generatorLists'] = $this->generatorRepository->findAll();
        return view('office::generators.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $data['title'] = "Create Generator";
        $data['editable'] = false;
        return view('office::generators.create', $data);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request): RedirectResponse
    {
        try {

            $this->generatorRepository->create($request->all());
            toastr()->success('Generator Created Successfully.');

        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());
        }
        return redirect()->route('generator.index');
    }

    /**
     * Show the specified resource.
     */
    public function show($id)
    {
        return view('office::generators.show');
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit($id)
    {
        $data['title'] = "Edit Generator";
        $data['editable'] = true;
        $data['generator'] = $this->generatorRepository->getGeneratorById($id);
        return view('office::generators.edit', $data);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id): RedirectResponse
    {
        try {

            $this->generatorRepository->update($id, $request->all());
            toastr()->success('Generator Updated Successfully.');

        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());

        }

        return redirect()->route('generator.index');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        try {
            $this->generatorRepository->delete($id);
            toastr()->success('Generator deleted successfully.');
        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());
        }

        return redirect()->route('generator.index');
    }
}