<?php

namespace App\Modules\Bookings;

use App\Modules\Bookings\KeychainRepositoryInterface;
use Illuminate\Http\Request;
use App\Http\Controllers\BaseController;

class KeychainControllerREST extends BaseController
{
    /**
     * @var \App\Modules\Bookings\KeychainRepositoryInterface
     */
    private $repo;

    /**
     * Constructor.
     *
     * @param \App\Modules\Bookings\KeychainRepositoryInterface $repo
     */
    public function __construct(KeychainRepositoryInterface $repo)
    {
        $this->repo = $repo;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(Request $request)
    {
        $data = null;
        if ($request->has('client_id') && $request->has('client_type')) {
            $data = $this->repo->getClientKeychains($request->input('client_type'), $request->input('client_id'));
        }

        if ($request->has('task_id')) {
            $data = $this->repo->getTaskKeychains($request->input('task_id'));
        }

        $response = [
            'success'      => true,
            'data'         => $data,
        ];

        return response()->json($response);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
    }

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

        if ($keychain->id) {
            if ($request->has('task_id')) {
                $task = \App\Modules\Bookings\Task::findOrFail($request->input('task_id'));
                $task->passwords()->attach($keychain->id);
            }

            return $keychain;
        } else {
            return response()->json(['errors' => $keychain->errors->all()], 400);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @param  Request $request
     *
     * @return Response
     */
    public function update($id, Request $request)
    {
        $keychain = \App\Models\Keychain::findOrFail($id);

        $keychain->fill($request->all());

        if ($keychain->save()) {
            return $keychain;
        } else {
            return response()->json(['errors' => $keychain->errors->all()], 400);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @param  Request $request
     *
     * @return Response
     */
    public function destroy($id, Request $request)
    {
        try {
            //If send a task id, remove the link to this password
            if ($request->has('task_id')) {
                $task = \App\Modules\Bookings\Task::findOrFail($request->input('task_id'));
                $task->passwords()->detach($id);
            }

            if ($request->input('disabled', false)) {
                $keychain = \App\Models\Keychain::findOrFail($id);
                $keychain->disabled = true;
                $keychain->save();
            }

            return response()->json('Keychain deleted successful!', 200);
        } catch (\Exception $e) {
            return response()->json(['errors' => $e->getMessage()], 400);
        }
    }

    /**
     * Return a list of allowed types for keychain.
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function getTypes()
    {
        return response()->json(['data' => \App\Models\Keychain::getTypes()], 200);
    }
}
