<?php

namespace App\Managers;

use App\Models\BuyInviteListModel;
use App\Models\BuyInviteModel;
use App\Models\Deed\FriendsModel;
use App\Models\Deed\InvitationCardModel;
use App\Models\PartnerModel;
use App\Models\PraiseModel;
use App\Models\TagModel;
use App\Models\User\FeedType4RemindModel;
use App\Models\User\InviteConfigModel;
use App\Models\User\SuperLikeModel;
use App\Models\User\UserModel;

class PartnerManager
{
    public function partnerUser(int $uid, PartnerModel $partner)
    {
        if ($partner->photo_src && !str_contains($partner->photo_src, 'http')) {
            $partner->photo_src = "https://oss.pocketuniversity.cn" . $partner->photo_src;
        }
        if ($partner->photo_1 && !str_contains($partner->photo_1, 'http')) {
            $partner->photo_1 = "https://oss.pocketuniversity.cn" . $partner->photo_1;
        }
        if ($partner->photo_2 && !str_contains($partner->photo_2, 'http')) {
            $partner->photo_2 = "https://oss.pocketuniversity.cn" . $partner->photo_2;
        }
        if ($partner->photo_3 && !str_contains($partner->photo_3, 'http')) {
            $partner->photo_3 = "https://oss.pocketuniversity.cn" . $partner->photo_3;
        }
        if ($partner->photo_4 && !str_contains($partner->photo_4, 'http')) {
            $partner->photo_4 = "https://oss.pocketuniversity.cn" . $partner->photo_4;
        }

        $partner->black_at = $partner->black_at < time() ? 0 : $partner->black_at;

        /** @var UserModel $user */
        $user = UserModel::findOrfail($partner->uid, [
            'uid',
            'headimgurl',
            'nickname',
            'age',
            'star',
            'last_life',
            'why',
            'pair_voice',
            'identity_auth',
            'wx_auth',
            'qq',
            'weixin',
            'photo_src',
            'photo_1',
            'photo_2',
            'photo_3',
            'photo_4',
            'introduce',
            'expect',
            'height',
            'sex',
            'school',
            'address',
            'home',
            'tag_1',
            'tag_2',
            'tag_3',
            'tag_4',
            'login_at',
            'claim_tag',
            'work_state',
            'be_vip_at',
            'supvip_endat',
            'logoff_at',
            'bgcolor'
        ]);

        // 标签处理
        $tagModel = new TagModel();
        $user->tags = $tagModel->getAllTagByUser($user);

        if ($inviteConf = InviteConfigModel::find($partner->uid)) {
            $user->task_photo = $inviteConf->task_photo;
            $user->task_question = $inviteConf->task_question;
            $user->task_sing = $inviteConf->task_sing;
            $user->task_question_data = $inviteConf->task_question_data ? true : false;
            $user->task_sing_data = $inviteConf->task_sing_data ? true : false;
        } else {
            $user->task_photo = false;
            $user->task_question = false;
            $user->task_sing = false;
            $user->task_question_data = false;
            $user->task_sing_data = false;
        }

        $user->invite_cnt = InvitationCardModel::where('invite_uid', $user->uid)->count();
        $partner->headimgurl = $user->headimgurl;
        $partner->nickname = $user->nickname;

        if ($uid != $partner->uid) {
            if (1 != $partner->check_photo) {
                unset($partner->photo_src);
            }
            if (1 != $partner->photo_1_check) {
                unset($partner->photo_1);
            }
            if (1 != $partner->photo_2_check) {
                unset($partner->photo_2);
            }
            if (1 != $partner->photo_3_check) {
                unset($partner->photo_3);
            }
            if (1 != $partner->photo_4_check) {
                unset($partner->photo_4);
            }
            if (1 != $partner->voice_check) {
                unset($partner->voice);
            }
            unset($partner->check_photo);
            unset($partner->photo_1_check);
            unset($partner->photo_2_check);
            unset($partner->photo_3_check);
            unset($partner->photo_4_check);
            unset($partner->voice_check);
            unset($partner->check_contact);
            unset($partner->qq);
            unset($partner->weixin);

            // 与self的关系处理
            $self = array(
                'friend' => false,
                'praise' => false,
                'invite' => [],
                'superlike' => false,
                'is_remind' => FeedType4RemindModel::where([
                    ['uid', $uid],
                    ['is_remind_uid', $partner->uid],
                    ['type', 1]
                ])->exists()
            );
            $friend = FriendsModel::where([['uid', $uid], ['friend_uid', $partner->uid], ['is_friend', 1]])->exists();
            if (!$friend) {
                !empty($user->weixin) && $user->weixin = substr($user->weixin, 0, 1) . "****" . substr(
                    $user->weixin,
                    -1,
                    1
                );
                !empty($user->qq) && $user->qq = substr($user->qq, 0, 1) . "****" . substr($user->qq, -1, 1);
            }
            $self['friend'] = $friend;
            if (PraiseModel::where([['uid', $uid], ['partner_id', $partner->id], ['type', 1]])->exists()) {
                $self['praise'] = true;
            }
            $invite = InvitationCardModel::where([
                ['uid', $uid],
                ['invite_uid', $partner->uid],
                ['expired_at', '>', time()]
            ])->groupBy('question_type')->get(['question_type'])->pluck('question_type');
            $self['invite'] = $invite->toArray();
            if (SuperLikeModel::where([['uid', $uid], ['partner_id', $partner->id]])->exists()) {
                $self['superlike'] = true;
            }
            $partner->self = $self;
        } else {
            $self = array(
                'friend' => false,
                'praise' => false,
                'invite' => [],
                'superlike' => false,
                'is_remind' => false
            );
            $partner->self = $self;
        }

        $partner->setAttribute('user', $user);

        $praise = PraiseModel::where([['partner_id', $partner->id], ['uid', $uid]])->first();

        $partner->praise = isset($praise->type) ? $praise->type : 0;

        $buyInviteListModel = new BuyInviteListModel();
        $inviteList = $buyInviteListModel->where([['partner_id', $partner->id], ['uid', $uid]])->first();

        if (!collect($inviteList)->isEmpty()) {
            $partner->invite_id = $inviteList->id;
            $buyInviteModel = new BuyInviteModel();
            $partner->invite_percent = $buyInviteModel->effectiveInvite($uid, $partner->id);
        }

        return $partner;
    }
}
