<?php

namespace App\Managers;

use App\Models\Deed\InvitationCardModel;
use App\Models\NoticeModel;
use App\Models\PraiseModel;
use App\Models\User\UserModel;
use Illuminate\Support\Facades\Config;

class UserNoticeManager
{

    /**
     * 获取系统通知
     * @param  int  $uid
     * @param  array  $pages
     * @param  int  $unread  不自动读
     * @return array
     */
    public function systemList(int $uid, array $pages, int $unread): array
    {
        if (in_array(Config::get("platform"), ['ios', 'android'])) {
            // APP通知
            if (Config::get("version") < "2.17.6") {
                $shield = [20, 13, 14];
            } else {
                $shield = [20];
            }
        } else {
            // 小程序通知
            $shield = [5];
        }
        $total = NoticeModel::where('uid', $uid)->whereIn('type', $shield)->count();
        $datas = NoticeModel::where('uid', $uid)->whereIn('type', $shield)
            ->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->orderBy('id', 'desc')->get();
        $tag_count = NoticeModel::where([['uid', $uid], ['is_read', 0]])->count();

        0 == $unread && NoticeModel::where([['uid', $uid], ['is_read', 0]])->update(['is_read' => time()]);

        return [
            'total' => $total,
            'page' => $pages['page'],
            'limit' => $pages['limit'],
            'tag_count' => $tag_count,
            'list' => $datas
        ];
    }

    /**
     * 获取最近系统通知
     * @param  int  $uid
     * @return NoticeModel|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder|object|null
     */
    public function systemListDesc(int $uid)
    {
        if (in_array(Config::get("platform"), ['ios', 'android'])) {
            // APP通知
            $shield = [20, 13, 14];
        } else {
            // 小程序通知
            $shield = [5, 13, 14];
        }
        $data = NoticeModel::where([
            ['uid', $uid],
            ['is_read', 0]
        ])->whereIn('type', $shield)->orderByDesc('id')->first(['id', 'uid', 'title', 'content', 'create_at', 'type']);
        if (collect($data)->isEmpty()) {
            return $data;
        }
        switch ($data->type) {
            case 13:
                $invite_me = NoticeModel::where([['is_read', 0], ['type', 13], ['uid', $uid]])->count();
                $data->content = "收到{$invite_me}条新的好友申请消息，点击查看";
                break;
            case 14:
                $total = NoticeModel::where([['is_read', 0], ['type', 14], ['uid', $uid]])->count();
                $data->content = "收到{$total}条新的心动消息，点击查看";
                break;
        }
        return $data;
    }

    /**
     * 喜欢我的通知概览
     * @param  int  $uid
     * @return array
     */
    public function likeMeOverView(int $uid)
    {
        try {
            $user = UserModel::findOrFail($uid);
            // 喜欢我的人数
            $total = PraiseModel::where([['partner_id', $user->partner_id], ['type', 1], ['read', 0]])->count();
            // 最近喜欢我的三个用户信息：昵称 + 头像
            $praise = PraiseModel::where([
                ['partner_id', $user->partner_id],
                ['type', 1],
                ['read', 0]
            ])->orderBy('updated_at', 'desc')->take(3)->get();
            $users = UserModel::whereIn('uid', $praise->pluck('uid'))->get(['uid', 'nickname', 'headimgurl']);
            return array(
                'users' => $users,
                'total' => $total,
                'notice' => $total,
                'likeme' => $total,
            );
        } catch (\Exception $exception) {
            return array(
                'users' => null,
                'likeme' => 0,
                'total' => 0,
                'notice' => 0
            );
        }
    }

    /**
     * 邀请通知概览
     * @param  int  $uid
     * @return array
     */
    public function inviteOverView(int $uid)
    {
        // 未读消息-我发出的邀请
        $send_invite = InvitationCardModel::where([['send_read', 0], ['uid', $uid]])->count();
        // 未读消息-收到的邀请
        $invite_me = InvitationCardModel::where([['read', 0], ['state', 0], ['invite_uid', $uid]])->count();

        // 最近我未读的邀请用户信息
        $invites = InvitationCardModel::where([['invite_uid', $uid], ['read', 0]])->orWhere([
            ['send_read', 0],
            ['uid', $uid]
        ])->take(3)->orderBy('id', 'desc')->get();
        $users = UserModel::whereIn('uid', $invites->pluck('uid'))->get(['uid', 'nickname', 'headimgurl']);

        return array(
            'users' => $users,
            'send_invite' => $send_invite,
            'invite_me' => $invite_me,
            'total' => $send_invite + $invite_me,
            'notice' => $send_invite + $invite_me
        );
    }
}
