<?php

namespace App\Http\Controllers\Frontend\Forum;

use App\Http\Controllers\Controller;
use App\Models\Forums\ForumReply;
use App\Models\Forums\ForumThread;
use App\Models\Messages\ExtendedThread;
use Illuminate\Http\Request;

class ForumReplyController extends Controller
{
    //

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        dd('index');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $params = $request->all();
        $params['user_id'] = auth()->id();
        // truncate body length
        if (strlen($params['body']) > 20000) {
            $params['body'] = mb_strcut($params['body'], 1, 20000, "utf8");
        }
        // strip dodgy tags
        $params['body'] = strip_tags($params['body'], '<img><a><p><strong><b><i><em><strike><ol><ul><li>');
       // create teh reply
        $reply = ForumReply::create($params);
        // notify thread user
        //@TODO make it mute-able, and filter out notifications from blocked users
        // Notify people involved in this post
        $actions = array(['text' => 'View', 'buttonclass' => 'btn-outline-success', 'route' => $reply->thread->path()]);
        $notification = new ExtendedThread();
        // notify author
        $message = auth()->user()->name . " replied to your post " . $reply->thread->title . " at " . $reply->created_at;
        $notification->makeNotification('Reply to your post' . $reply->thread->title, $message, $actions, [$reply->thread->user->id], auth()->user()->id);
        // notify other paricipants
        // build a list of participants
        //@TODO add mute conversation funcitonality
        $replyusers = [];
        foreach ($reply->thread->replies as $reply) {
            if (!in_array($reply->user_id, $replyusers)) {
                $replyusers[] = $reply->user_id;
            }
        }
        // notify other participants
        $message = auth()->user()->name . " replied to a post you are involved in " . $reply->thread->title . " at " . $reply->created_at;
        $notification->makeNotification('Activity on a post' . $reply->thread->title, $message, $actions, [$replyusers], auth()->user()->id);
        return array(
            'status' => (($reply->id > 0) ? 0 : 1),
        );

    }

    public function create(Request $request)
    {
        //dd($id);
        //dd($request->all());
        $params = $request->all();
        $params['user_id'] = auth()->id();
        // $params['thread_id']=$id;
        //    dd($params);
        // $thread = ForumThread::find($id);
        $reply = ForumReply::create($params);
        return array(
            'status' => (($reply->id > 0) ? 0 : 1),
        );

    }

    public function update($id, Request $request)
    {
        $input = $request->all();
        if (strlen($input['body']) > 20000) {
            $input['body'] = mb_strcut($input['body'], 1, 20000, "utf8");
        }
        $input['body'] = strip_tags($input['body'], '<img><a><p><strong><b><i><em><strike><ol><ul><li>');
        return array(
            'status' => (ForumReply::find($id)->update($input) ? 0 : 1),
        );
    }

    public function show($id)
    {
        return ForumReply::find($id);
    }

    public function destroy($id)
    {
        $status = (ForumReply::destroy($id) > 0 ? '0' : '1');
        $response = array(
            'status' => $status,
        );
        return $response;
    }

}
