<?php

namespace Lavalite\Industry\Http\Controllers;

use App\Http\Controllers\AdminController as AdminController;
use Former;
use Lavalite\Industry\Http\Requests\IndustryAdminRequest;
use Lavalite\Industry\Interfaces\IndustryRepositoryInterface;
use Response;

/**
 * Admin controller class.
 */
class IndustryAdminController extends AdminController
{
    /**
     * Initialize industry controller.
     *
     * @param type IndustryRepositoryInterface $industry
     *
     * @return type
     */
    public function __construct(IndustryRepositoryInterface $industry)
    {
        $this->model = $industry;
        parent::__construct();
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(IndustryAdminRequest $request)
    {
        if ($request->wantsJson()) {
            $array = $this->model->json();
            foreach ($array as $key => $row) {
                $array[$key] = array_only($row, config('industry.industry.listfields'));
            }

            return ['data' => $array];
        }

        $this->theme->prependTitle(trans('industry::industry.names').' :: ');

        return $this->theme->of('industry::admin.industry.index')->render();
    }

    /**
     * Display the specified resource.
     *
     * @param Request $request
     * @param int     $id
     *
     * @return Response
     */
    public function show(IndustryAdminRequest $request, $id)
    {
        $industry = $this->model->find($id);

        if (empty($industry)) {
            if ($request->wantsJson()) {
                return [];
            }

            return view('industry::admin.industry.new');
        }

        if ($request->wantsJson()) {
            return $industry;
        }

        Former::populate($industry);

        return view('industry::admin.industry.show', compact('industry'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function create(IndustryAdminRequest $request)
    {
        $industry = $this->model->findOrNew(0);
        Former::populate($industry);

        return view('industry::admin.industry.create', compact('industry'));
    }

    /**
     * Display the specified resource.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function store(IndustryAdminRequest $request)
    {
        try {
            $attributes = $request->all();
            $industry = $this->model->create($attributes);

            return $this->success(trans('messages.success.created', ['Module' => trans('industry::industry.name')]));
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param Request $request
     * @param int     $id
     *
     * @return Response
     */
    public function edit(IndustryAdminRequest $request, $id)
    {
        $industry = $this->model->find($id);

        Former::populate($industry);

        return view('industry::admin.industry.edit', compact('industry'));
    }

    /**
     * Update the specified resource.
     *
     * @param Request $request
     * @param int     $id
     *
     * @return Response
     */
    public function update(IndustryAdminRequest $request, $id)
    {
        try {
            $attributes = $request->all();
            $industry = $this->model->update($attributes, $id);

            return $this->success(trans('messages.success.updated', ['Module' => trans('industry::industry.name')]));
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * Remove the specified resource.
     *
     * @param int $id
     *
     * @return Response
     */
    public function destroy(IndustryAdminRequest $request, $id)
    {
        try {
            $this->model->delete($id);

            return $this->success(trans('message.success.deleted', ['Module' => trans('industry::industry.name')]), 200);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }
}