<?php

namespace App\Modules\Admin;

use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use App\Http\Controllers\BaseController;

class StatusController extends BaseController
{
    /**
     *	The layout that should be used for responses.
     */
    protected $layout = 'layouts.master';

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $statuses = \App\Models\Status::orderBy('for')->get();

        return view('admin::settings.statuses.index', compact('statuses'));
    }

    /**
     * Display the page for creating a new status.
     *
     * @return null
     */
    public function create()
    {
        $this->layout->content = View::make('admin::settings.statuses.create', ['status' => null]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        $status = \App\Models\Status::create(request()->all());

        if (! $status->exists) {
            return Redirect::route('admin.settings.statuses.create')->withError($status);
        }

        return Redirect::route('admin.settings.statuses.index')->with('message', 'Status created successfully.');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $status = \App\Models\Status::findOrFail($id);
        return Response::json($status);
    }

    /**
     * Display the edit a status page.
     *
     * @param  int $id
     *
     * @return null
     */
    public function edit($id)
    {
        $status = \App\Models\Status::findOrFail($id);
        $this->layout->content = View::make('admin::settings.statuses.edit', compact('status'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        $status = \App\Models\Status::findOrFail($id);
        $status->fill(request()->all());
        
        if (! $status->save()) {
            return Redirect::route('admin.settings.statuses.edit', $id)->withError($status);
        }

        return Redirect::route('admin.settings.statuses.edit', $id)->with('message', 'Your changes were successfully saved.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        if (! request()->get('status_id')) {
            throw new \Exception('A replacement status id is required before deleting this one.');
        }

        $status = \App\Models\Status::findOrFail($id);
        $new_status = \App\Models\Status::findOrFail(request()->get('status_id'));
        $success = false;

        DB::transaction(function () use ($id, &$status, &$success) {
            // Update all tasks using this status - need to check what task table exists
            DB::table('tasks')->where('status_id', $id)->update(['status_id' => request()->get('status_id')]);
            $status->delete();
            $success = true;
        });

        if (! $success) {
            throw new \Exception('Something went wrong');
        }

        Session::flash('message', 'Successfully deleted status: '.$status->name);

        return $status;
    }
}
