<?php

namespace App\Http\Controllers\Admin;

use App\Facade\Theme;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreLabelRequest;
use App\Label;
use App\Photo;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\View;
use Symfony\Component\HttpFoundation\Request;

class LabelController extends Controller
{
    public function __construct()
    {
        $this->middleware(['auth', 'max_post_size_exceeded']);
        View::share('is_admin', true);
    }

    /**
     * Applies a comma-separated string of label IDs and/or new label texts to a photo. This is called from the
     * PhotoController - not directly via a route.
     * @param Photo $photo Photo to apply the labels to
     * @param string $labelString CSV string of label IDs and new labels to create (e.g. "1,2,Florida,nature" would
     *                            link label IDs 1 and 2, and create 2 new labels called Florida and nature.)
     */
    public function applyLabelsToPhoto(Photo $photo, $labelString)
    {
        foreach (explode(',', $labelString) as $labelText)
        {
            $labelID = intval($labelText);
            if (intval($labelID) == 0)
            {
                // Check if the label already exists
                $labelToUse = Label::where('name', $labelText)->first();

                if (is_null($labelToUse))
                {
                    // Create new label
                    $labelToUse = new Label();
                    $labelToUse->name = $labelText;
                    $labelToUse->save();
                }

                $labelID = $labelToUse->id;
            }

            $photo->labels()->attach(intval($labelID));
        }
    }

    public function delete($id)
    {
        $this->authorizeAccessToAdminPanel();

        $label = $this->loadLabel($id);
        return Theme::render('admin.delete_label', ['label' => $label]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request, $id)
    {
        $this->authorizeAccessToAdminPanel('admin:manage-labels');

        $label = $this->loadLabel($id);
        $label->delete();

        $request->session()->flash('success', trans('admin.delete_label_success_message', ['name' => $label->name]));

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

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $labels = Label::withCount('photos')->get();
        return Theme::render('admin.list_labels', [
            'labels' => $labels
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreLabelRequest $request)
    {
        $this->authorizeAccessToAdminPanel('admin:manage-labels');

        $label = new Label();
        $label->fill($request->only(['name']));
        $label->save();

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

    /**
     * @param $id
     * @return Album
     */
    private function loadLabel($id)
    {
        $label = Label::where('id', intval($id))->first();
        if (is_null($label))
        {
            App::abort(404);
            return null;
        }

        return $label;
    }
}