<?php

namespace App\Services\V2_17_3\Friends;

use App\Models\Fpdx\PairModel;
use App\Models\Fpdx\RoomModel as fpdxRoomModel;
use App\Models\Friends\FriendApplyModel;
use App\Models\Friends\FriendModel;
use App\Models\Friends\LogModel;
use App\Models\Goodnight\RoomModel as goodnightRoomModel;
use App\Models\Gteam\RoomModel as gameRoomModel;
use App\Models\NoticeModel;
use App\Models\PraiseModel;
use App\Models\User\LikeInviteQuestionModel;
use App\Models\User\UserModel;
use App\Services\Service;

/**
 * @deprecated
 */
class FriendService extends Service
{
    /**
     * 好友页概览
     * @param  int  $uid
     */
    public function overview(int $uid)
    {
    }

    /**
     * 好友列表数字 全部：未读 | 相互喜欢：未读 | 收到邀请：未读 | 发出邀请：未读
     * @param  int  $uid
     * @return array
     */
    public function getListCnt(int $uid)
    {
        /** @var UserModel $user */
        $user = UserModel::findOrFail($uid);

        // 喜欢我的
        /** @var PraiseModel $like_me */
        $like_me = PraiseModel::where([['read', 0], ['partner_id', $user->partner_id], ['type', 1]])->count();

        // 相互喜欢的
        /** @var FriendModel $model */
        $eachlike = FriendModel::where([
            ['uid', $uid],
            ['each_like', 1],
            ['is_hide', 0],
        ])->sum('unread_cnt');

        $invite_me = FriendApplyModel::where([
            ['uid', $uid],
            ['is_hide', 0],
        ])->sum('unread_cnt');
        $send_invite = 0;

        // 系统消息
        $sys_notice = NoticeModel::where([['uid', $uid], ['is_read', 0]])->count();

        // 全部好友
        $allFriend = FriendModel::where([
            ['uid', $uid],
            ['is_hide', 0],
        ])->sum('unread_cnt');

        return array(
            'all' => (int)$allFriend + $invite_me + $send_invite + $sys_notice,
            'each_like' => (int)$eachlike,
            'invite_me' => (int)$invite_me,
            'send_invite' => (int)$send_invite,
            'like_me' => (int)$like_me,
            'sys_notice' => (int)$sys_notice,
        );
    }

    /**
     * 全部好友列表
     * @param  int  $uid
     * @param  array  $pages
     * @return array
     */
    public function getAllList(int $uid, array $pages): array
    {
        $total = FriendModel::where(array(['uid', $uid], ['is_hide', 0]))->count();
        $datas = FriendModel::where([['uid', $uid], ['is_hide', 0]])->orderBy('last_at', 'desc')
            ->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->get();
        $friend_uids = $datas->pluck('friend_uid')->toArray();
        $users = UserModel::whereIn('uid', $friend_uids)->get([
            'uid',
            'headimgurl',
            'nickname',
            'sex',
            'weixin',
            'qq',
            'identity_auth',
            'wx_auth',
            'be_vip_at',
            'supvip_endat',
        ]);
        $datas->map(function (/** @var FriendModel $data */ &$data) use ($users) {
            $data->setAttribute('friend', $users->where('uid', $data->friend_uid)->first());
        });

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

    /**
     * 相互心动列表
     * @param  int  $uid
     * @param  array  $pages
     * @return array
     */
    public function getEachLikeList(int $uid, array $pages): array
    {
        $total = FriendModel::where(array(['uid', $uid], ['each_like', 1], ['is_hide', 0]))->count();
        $datas = FriendModel::where([['uid', $uid], ['each_like', 1], ['is_hide', 0]])->orderByDesc('last_at')
            ->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->get();
        $friend_uids = $datas->pluck('friend_uid')->toArray();
        $users = UserModel::whereIn('uid', $friend_uids)->get([
            'uid',
            'headimgurl',
            'nickname',
            'sex',
            'weixin',
            'qq',
            'identity_auth',
            'wx_auth',
            'be_vip_at',
            'supvip_endat',
        ]);
        $datas->map(function (/** @var FriendModel $data */ &$data) use ($users) {
            $data->setAttribute('friend', $users->where('uid', $data->friend_uid)->first());
        });

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

    /**
     * 星标好友列表
     * @param  int  $uid
     * @param  array  $pages
     * @return array
     */
    public function getStarList(int $uid, array $pages): array
    {
        $total = FriendModel::where(array(['uid', $uid], ['star_at', '>', 0], ['is_hide', 0]))->count();
        $datas = FriendModel::where([['uid', $uid], ['star_at', '>', 0], ['is_hide', 0]])->orderByDesc('last_at')
            ->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->get();
        $friend_uids = $datas->pluck('friend_uid')->toArray();
        $users = UserModel::whereIn('uid', $friend_uids)->get([
            'uid',
            'headimgurl',
            'nickname',
            'sex',
            'weixin',
            'qq',
            'identity_auth',
            'wx_auth',
            'be_vip_at',
            'supvip_endat',
        ]);
        $datas->map(function (&$data) use ($users) {
            $data->friend = $users->where('uid', $data->friend_uid)->first();
        });

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

    /**
     * 交互历史
     * @param  int  $uid
     * @param  int  $friend_uid
     * @param  array  $pages
     * @return array
     */
    public function getHistory(int $uid, int $friend_uid, array $pages)
    {
        $total = LogModel::where(array(['uid', $uid], ['tuid', $friend_uid]))
            ->orWhere(array(['uid', $friend_uid], ['tuid', $uid]))
            ->count();
        $datas = LogModel::where(array(['uid', $uid], ['tuid', $friend_uid]))
            ->orWhere(array(['uid', $friend_uid], ['tuid', $uid]))
            ->skip(($pages['page'] - 1) * $pages['limit'])
            ->take($pages['limit'])
            ->orderBy('created_at')
            ->get();

        $datas->map(function (/** @var LogModel $data */ &$data) use ($uid) {
            switch ($data->do) {
                case 0: // 相互心动
                    $data->setAttribute('type', 'each_like');
                    $data->setAttribute('app_type', 'each_like');
                    if ($data->uid == $uid) {
                        $data->setAttribute("msg", "这里是消息1");
                    } else {
                        $data->setAttribute("msg", "这里是消息2");
                    }
                    break;
                case 1: // 取消心动
                    $data->setAttribute('type', 'each_like');
                    $data->setAttribute('app_type', 'each_like');
                    if ($data->uid == $uid) {
                        $data->setAttribute("msg", "这里是消息");
                    } else {
                        $data->setAttribute("msg", "这里是消息");
                    }
                    break;
                case 2: // 72h匹配
                    $attach = $data->attach;
                    /** @var fpdxRoomModel $room */
                    $room = fpdxRoomModel::find($attach['room_id'], ['created_at', 'room_id', 'stage_id']);
                    if (PairModel::where(['room_id' => $room->room_id])->value('activity_type') == 'qbj') {
                        $data->setAttribute('type', 'qbj');
                    } else {
                        $data->setAttribute('type', 'pair');
                    }
                    $data->setAttribute('app_type', 'pair');
                    $data->attach = $room;
                    if ($data->uid == $uid) {
                        $data->setAttribute("msg", "这里是消息");
                    } else {
                        $data->setAttribute("msg", "这里是消息");
                    }
                    break;
                case 3: // 好友申请
                    if ($data->uid == $uid) {
                        $data->setAttribute('type', 'my_invite');
                    } else {
                        $data->setAttribute('type', 'friend_invite');
                    }
                    if ($data->uid == $uid) {
                        $data->setAttribute("msg", "这里是消息");
                    } else {
                        $data->setAttribute("msg", "这里是消息");
                    }
                    $data->setAttribute('app_type', 'invalid');
                    $attachs = $data->attach;
                    foreach ($attachs as &$attach) {
                        if (isset($attach['type'])) {
                            switch ($attach['type']) {
                                case 0:  # 打招呼
                                    $data->setAttribute('app_type', 'friend_invite_hello');
                                    $data->setAttribute("msg", $attach['answer']['value']);
                                    break;
                                case -1: # 无考验
                                case 1:  # 爆照
                                    break;
                                case 2: # 问答
                                    $data->setAttribute('app_type', 'friend_invite_anwser');
                                    $data->setAttribute("msg", "我还回答了你的一个问题");
                                    $question = LikeInviteQuestionModel::withTrashed()->find($attach['question']);
                                    $question->question = json_decode($question->question, true);
                                    $attach['question'] = $question;
                                    break;
                                case 3: # 接唱
                                    $data->setAttribute('app_type', 'friend_invite_sing');
                                    $data->setAttribute("msg", "我还给你唱了一首歌");
                                    $question = LikeInviteQuestionModel::withTrashed()->find($attach['question']);
                                    $question->question = json_decode($question->question, true);
                                    $attach['question'] = $question;
                                    break;
                                case 4: # 走心一画
                                    $data->setAttribute('app_type', 'friend_invite_draw');
                                    $data->setAttribute("msg", "我还给你画了一幅画");
                                    break;
                                default:
                                    $data->setAttribute('app_type', 'invalid');
                                    $question = LikeInviteQuestionModel::withTrashed()->find($attach['question']);
                                    $question->question = json_decode($question->question, true);
                                    $attach['question'] = $question;
                                    break;
                            }
                        } else {
                            $attachs = [$attachs];
                            break;
                        }
                    }
                    $data->attach = $attachs;
                    break;
                case 4: // 同意好友申请
                    $data->setAttribute('type', 'agree_apply');
                    $data->setAttribute('app_type', 'agree_apply');
                    if ($data->uid == $uid) {
                        $data->setAttribute("msg", "这里是消息");
                    } else {
                        $data->setAttribute("msg", "这里是消息");
                    }
                    break;
                case 5: // 开黑-王者荣耀
                    $data->setAttribute('type', 'game_wz');
                    $data->setAttribute('app_type', 'game_wz');
                    $attach = $data->attach;
                    /** @var gameRoomModel $room */
                    $room = gameRoomModel::find($attach['id'], ['id', 'created_at', 'type']);
                    $data->attach = $room;
                    if ($data->uid == $uid) {
                        $data->setAttribute("msg", "这里是消息");
                    } else {
                        $data->setAttribute("msg", "这里是消息");
                    }
                    break;
                case 6: // 开黑-吃鸡
                    $data->setAttribute('type', 'game_cj');
                    $data->setAttribute('app_type', 'game_cj');
                    $attach = $data->attach;
                    /** @var gameRoomModel $room */
                    $room = gameRoomModel::find($attach['id'], ['id', 'created_at', 'type']);
                    $data->attach = $room;
                    if ($data->uid == $uid) {
                        $data->setAttribute("msg", "这里是消息");
                    } else {
                        $data->setAttribute("msg", "这里是消息");
                    }
                    break;
                case 7: // 晚安
                    $data->setAttribute('type', 'goodnight');
                    $data->setAttribute('app_type', 'goodnight');
                    $attach = $data->attach;
                    $room = goodnightRoomModel::find($attach['id'], ['id', 'created_at']);
                    $data->attach = $room;
                    if ($data->uid == $uid) {
                        $data->setAttribute("msg", "这里是消息");
                    } else {
                        $data->setAttribute("msg", "这里是消息");
                    }
                    break;
                default:
                    $data->setAttribute('app_type', 'invide');
                    break;
            }
        });
        $apply_friend = FriendModel::applyOrFriend($uid, $friend_uid);
        // 清空未读标记
        FriendModel::where([['uid', $uid], ['friend_uid', $friend_uid]])->update(['unread_cnt' => 0]);
        FriendApplyModel::where([['uid', $uid], ['apply_uid', $friend_uid]])->update(['unread_cnt' => 0]);
        LogModel::whereIn('id', $datas->pluck('id')->toArray())->update(['read_at' => time()]);

        return array(
            'apply_friend' => $apply_friend,
            'total' => $total,
            'page' => $pages['page'],
            'limit' => $pages['limit'],
            'list' => $datas,
        );
    }
}
