<?php

namespace App\Services\Goodnight;

use App\Models\Goodnight\SubscribeModel;
use App\Models\Goodnight\VoiceModel;
use App\Models\User\UserModel;
use App\Services\Service;
use PocketBE\MsyPush\Jobs\RegistEventJob;

class NoticeService extends Service
{


    /**
     * 定制晚安（每日提醒）
     * @param  int  $to_uid
     * @param  int  $from_uid
     * @param  int  $voice_id
     * @return bool
     * @throws \App\Exceptions\AlertException
     * @throws \App\Exceptions\ApiException
     */
    public function subscribe(int $to_uid, int $from_uid, int $voice_id)
    {
        $user = UserModel::find($to_uid);
        $from_user = UserModel::find($from_uid);
        $voice = VoiceModel::find($voice_id);
        $subscribe_count = SubscribeModel::where('uid', $to_uid)->where('type', 0)->count();
        $payload = [
            'to_user' => $user->getAuth(),
            'from_user' => $from_user->toArray(),
            'voice' => $voice->toArray(),
            'meta' => [
                'subscribe_count' => $subscribe_count,
                'date' => date('Y-m-d'),
            ],
        ];
        dispatch(new RegistEventJob(10401, $payload))->onQueue("{push}");
    }

    /**
     * 公域语音审核（审核通过）
     * @param  int  $to_uid
     * @param  int  $voice_id
     * @return bool
     */
    public function checkSuccess(int $to_uid, int $voice_id)
    {
        $user = UserModel::find($to_uid);
        $voice = VoiceModel::find($voice_id);
        $payload = [
            'to_user' => $user->getAuth(),
            'voice' => $voice->toArray(),
            'meta' => [
                'date' => date('Y-m-d'),
            ],
        ];
        dispatch(new RegistEventJob(10402, $payload))->onQueue("{push}");
    }


    /**
     * 公域语音审核（审核失败）
     * @param  int  $to_uid
     * @param  int  $voice_id
     * @return bool
     */
    public function checkFail(int $to_uid, int $voice_id)
    {
        $user = UserModel::find($to_uid);
        $voice = VoiceModel::find($voice_id);
        $payload = [
            'to_user' => $user->getAuth(),
            'voice' => $voice->toArray(),
            'meta' => [
                'date' => date('Y-m-d'),
            ],
        ];
        dispatch(new RegistEventJob(10403, $payload))->onQueue("{push}");
    }

    /**
     * 晚安伴侣匹配成功
     * @param  int  $to_uid
     * @param int $match_uid
     */
    public function matchSuccess(int $to_uid, $match_uid)
    {
        $user = UserModel::find($to_uid);
        $match_user = UserModel::find($match_uid);
        $payload = [
            'to_user' => $user->getAuth(),
            'match_user' => $match_user->toArray(),
            'meta' => [
                'date' => date('Y-m-d'),
            ],
        ];
        dispatch(new RegistEventJob(10404, $payload))->onQueue("{push}");
    }


    /**
     * 晚安伴侣匹配失败
     * @param  int  $to_uid  要发送的人的uid
     * @return bool
     * @throws \Exception
     */
    public function matchFail(int $to_uid)
    {
        $user = UserModel::find($to_uid);
        $payload = [
            'to_user' => $user->getAuth(),
            'meta' => [
                'date' => date('Y-m-d'),
            ],
        ];
        dispatch(new RegistEventJob(10405, $payload))->onQueue("{push}");
    }


    /**
     * 晚安伴侣留言
     * @param  int  $from_uid  来自uid
     * @param  int  $to_uid  发送uid
     * @return bool
     */
    public function receiveRoomComment(int $to_uid, int $from_uid)
    {
        $user = UserModel::find($to_uid);
        $from_user = UserModel::find($from_uid);
        $payload = [
            'to_user' => $user->getAuth(),
            'from_user' => $from_user->toArray(),
            'meta' => [
                'date' => date('Y-m-d'),
            ],
        ];
        dispatch(new RegistEventJob(10406, $payload))->onQueue("{push}");
    }


    /**
     * 语音卡片留言
     * @param  int  $to_uid  要发送的人的uid
     * @param  int  $voice_id  被留言的语音
     * @param  int  $from_uid
     * @return bool
     * @throws \App\Exceptions\AlertException
     * @throws \App\Exceptions\ApiException
     */
    public function voiceComment(int $to_uid, int $from_uid, int $voice_id)
    {
        $user = UserModel::find($to_uid);
        $from_user = UserModel::find($from_uid);
        $voice = VoiceModel::find($voice_id);
        $payload = [
            'to_user' => $user->getAuth(),
            'from_user' => $from_user->toArray(),
            'voice' => $voice->toArray(),
            'meta' => [
                'date' => date('Y-m-d'),
            ],
        ];
        dispatch(new RegistEventJob(10407, $payload))->onQueue("{push}");
    }


    /**
     * 漂流瓶留言回复提醒
     * @param  int  $to_uid  要发送的人的uid
     * @param  int  $voice_id  被回复的语音
     * @param  string  $content  回复内容
     * @return bool
     */
    public function receiveCommentReply(int $to_uid, int $voice_id, $auther, string $content = "")
    {
        $user = UserModel::find($to_uid);
        $from_user = UserModel::find($auther);
        $voice = VoiceModel::find($voice_id);
        $payload = [
            'to_user' => $user->getAuth(),
            'from_user' => $from_user->toArray(),
            'voice' => $voice->toArray(),
            'meta' => [
                'date' => date('Y-m-d'),
            ],
        ];
        dispatch(new RegistEventJob(10408, $payload))->onQueue("{push}");
    }
}
