<?php

namespace App\Http\Controllers\Frontend\Forum;

use App\Models\Forums\ForumChannel;
use App\Models\Forums\ForumThread;
use App\Models\Lab;
use App\Models\Tag;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ForumThreadController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $threads = ForumThread::with('channels')->latest()->get();
        //@todo put some filters on this
        // dd($threads[0]->channels);
        $channels = ForumChannel::all();
        return view('frontend.forum.thread.index')
            ->with('threads', $threads)
            ->with('channels', $channels);
    }



    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     * @todo validation
     */
    public function store(Request $request)
    {
        // store the thread
        $thread = ForumThread::create([
            'title' => $request['title'],
            'body' => $request['body'],
            'user_id' => auth()->id(),
            'forum_channels_id' => $request['forum_channels_id'],
        ]);

        // save tags
        if (isset($request['tags'])) {
            $thread->tags()->sync($request['tags']);
        }

        return array(
            'status' => (($thread->id > 0) ? 0 : 1),
        );

    }


    public function create(Request $request)
    {
        // cut down the body to a sensible size
        if (strlen($request['body']) > 20000) {
            $request['body'] = mb_strcut($request['body'], 1, 20000, "utf8");
        }
        // sanitize any 'naughty' html in the body
        $request['body'] = strip_tags($request['body'], '<img><a><p><strong><b><i><em><strike><ol><ul><li>');

        $thread = ForumThread::create([
            'title' => $request['title'],
            'body' => $request['body'],
            'user_id' => auth()->id(),
            'forum_channels_id' => $request['forum_channels_id'],
        ]);

        // save tags
        if (isset($request['tags'])) {
            $thread->tags()->sync($request['tags']);
        }

        return array(
            'status' => (($thread->id > 0) ? 0 : 1),
        );
    }

    /**
     * Display the specified resource.
     *
     * @param \App\Models\ForumThread $forumThread
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $forumThread = ForumThread::find($id);
        $tags = Tag::where(['primary' => 1])->get();
        $channels = ForumChannel::all();
        return view('frontend.forum.thread.view')
            ->with('thread', $forumThread)
            ->with('tags', $tags)
            ->with('channels', $channels);

    }

    /**
     * Display the specified resource.
     *
     * @param \App\Models\ForumThread $forumThread
     * @return \Illuminate\Http\Response
     */
    public function labshow($lab_id, $thread_id)
    {
        $forumThread = ForumThread::find($thread_id);
        // is the user in this lab (or an admin?)
        if (\Auth::user()->labs()->get()->pluck('id')->contains($forumThread->channel->lab->id) || \Auth::user()->hasRole('administrator')) {

            $lab = Lab::find($lab_id);
            $tags = Tag::where(['primary' => 1])->get();
            $channels = ForumChannel::all();
            return view('frontend.forum.thread.view')
                ->with('thread', $forumThread)
                ->with('tags', $tags)
                ->with('channels', $channels)
                ->with('lab', $lab);
        } else {
            return redirect(route('frontend.index'))->withFlashDanger("Not your lab!");
        }

    }

    /**
     * Display the specified resource.
     *
     * @param \App\Models\ForumThread $forumThread
     * @return \Illuminate\Http\Response
     */
    public function get($id)
    {

        return ForumThread::with('channels')->find($id);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param \App\Models\ForumThread $forumThread
     * @return \Illuminate\Http\Response
     */
    public function edit(ForumThread $forumThread)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param \App\Models\ForumThread $forumThread
     * @return \Illuminate\Http\Response
     */
    public function update($id, Request $request)
    {
        if (strlen($request['body']) > 20000) {
            $request['body'] = mb_strcut($request['body'], 1, 20000, "utf8");
        }
        $request['body'] = strip_tags($request['body'], '<img><a><p><strong><b><i><em><strike><ol><ul><li>');
        $thread = ForumThread::updateOrCreate(['id' => $id], [
            'title' => $request['title'],
            'body' => $request['body'],
            'user_id' => auth()->id(),
            'forum_channels_id' => $request['forum_channels_id'],
        ]);

        // save tags
        if (isset($request['tags'])) {
            $thread->tags()->sync($request['tags']);
        }

        return array(
            'status' => (($thread->id > 0) ? 0 : 1),
        );
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param \App\Models\ForumThread $forumThread
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $status = (ForumThread::destroy($id) > 0 ? '0' : '1');
        $response = array(
            'status' => $status,
        );
        return $response;
    }

}
