<?php

namespace App\Listeners;

use App\Models\User\FeedType4RemindModel;
use App\Models\User\UserModel;
use App\Notifications\CompletedInfo;
use App\Notifications\CompleteInfoRemind;
use Illuminate\Contracts\Queue\ShouldQueue;
use PocketBE\MsyPush\Jobs\RegistEventJob;

/**
 * Class CompleteInfoListen
 * @package App\Listeners
 */
class CompleteInfoListen implements ShouldQueue
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
    }

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle($event)
    {
    }

    /**
     * @param  \Illuminate\Events\Dispatcher  $events
     */
    public function subscribe($events)
    {
        $events->listen(
            "App\Events\CompletedInfo",
            "App\Listeners\CompleteInfoListen@completedReply"
        );

        $events->listen(
            "App\Events\CompleteInfoRemind",
            "App\Listeners\CompleteInfoListen@remind"
        );
    }

    /**
     * 提醒补全信息
     * @param  \App\Events\CompleteInfoRemind  $events
     * @return bool
     */
    public function remind($events)
    {
        $to_uid = $events->is_remind_user->uid;

        $buildData = array(
            'remind_nickname' => $events->remind_nickname,
            'is_remind_nickname' => $events->is_remind_user->nickname
        );

        // 通知队列
        $user = UserModel::find($to_uid);
        $payload = [
            'to_user' => $user->getAuth(),
            'user' => $user->toArray(),
            'reminder' => $buildData,
        ];
        dispatch(new RegistEventJob(10702, $payload))->onQueue("{push}");
    }

    /**
     * 补全信息完成提醒查看
     * @param  \App\Events\CompletedInfo  $events
     * @return bool
     */
    public function completedReply($events)
    {
        $models = FeedType4RemindModel::where([
            ['is_remind_uid', $events->is_remind_uid],
            ['is_update_feedback_at', 0],
            ['type', 1]
        ])->get();
        $is_remind_user = UserModel::find($events->is_remind_uid);
        foreach ($models as $model) {
            /** @var FeedType4RemindModel $model */
            $model->is_update_feedback_at = time();
            $model->save();
            $buildData = array(
                'is_remind_nickname' => $is_remind_user->nickname,
                'is_remind_user_partner_id' => $is_remind_user->partner_id
            );

            $user = UserModel::find($model->uid);
            $payload = [
                'to_user' => $user->getAuth(),
                'user' => $user->toArray(),
                'reminder' => $buildData,
            ];
            dispatch(new RegistEventJob(10703, $payload))->onQueue("{push}");
        }
    }
}
