<?php

namespace App\Http\Services\V1_1;

use App\Generated\V1_1\Messages\Mock\DeleteBlackListMessage;
use App\Generated\V1_1\Messages\Mock\AddBlackListMessage;
use App\Generated\V1_1\Messages\Mock\SendSystemMessageMessage;
use App\Generated\V1_1\Messages\Mock\ThumbPartnerMessage;
use App\Exceptions\AlertException;
use App\Generated\V1_1\Messages\Mock\SendFriendApplyMessage;
use App\Managers\FriendManager;
use App\Models\BlackListModel;
use App\Models\Friends\FriendApply;
use App\Models\Friends\FriendContact;
use App\Models\NoticeModel;
use App\Models\PartnerModel;
use App\Models\RiskListModel;
use App\Models\User\UserModel;
use App\Services\IM\CustomElem;
use App\Utils\ImUtil;
class MockService
{
    private $friendManager;
    public function __construct(FriendManager $friendManager)
    {
        $this->friendManager = $friendManager;
    }
    /**
     * 发送好友申请
     * @param  SendFriendApplyMessage  $message
     * @throws AlertException
     */
    public function sendFriendApply(SendFriendApplyMessage $message)
    {
        $uid = $message->getUid();
        $contactUid = $message->getContactUid();
        if (FriendContact::where([array('uid', $uid), array('friend_uid', $contactUid), array('friend_level', 2)])->exists()) {
            throw new AlertException("你们已经是好友了", 401);
        }
        if (!$this->friendManager->canIm($uid, $contactUid)) {
            throw new AlertException("你们处于拉黑关系", 402);
        }
        FriendApply::updateOrCreate(array('uid' => $uid, 'apply_uid' => $contactUid), array('last_at' => time(), 'handle' => 0));
        $receiveUser = UserModel::find($contactUid, ['uid', 'im_account']);
        $message = new CustomElem('EventFriendApply', ['FromUid' => $uid, 'ToUid' => $contactUid]);
        $imUtil = new ImUtil();
        $imUtil->sendMessage("system_event", $receiveUser->im_account, [$message->toArray()]);
    }
    /**
     * 点赞卡片
     * @param  ThumbPartnerMessage  $message
     * @throws AlertException
     */
    public function thumbPartner(ThumbPartnerMessage $message)
    {
        $uid = $message->getUid();
        $partnerId = $message->getPartnerId();
        $thumb = $message->getThumb();
        $ps = new \App\Services\User\PartnerService();
        $data = $ps->thumb($uid, $partnerId, $thumb);
        $message->setResponse($data);
    }
    /**
     * 发送系统消息
     * @param  SendSystemMessageMessage  $message
     */
    public function sendSystemMessage(SendSystemMessageMessage $message)
    {
        $uid = $message->getUid();
        // 系统消息
        NoticeModel::create(['uid' => $uid, 'title' => '测试通知', 'content' => "测试。。。", 'type' => 5, 'type_id' => 0, 'create_at' => time(), 'update_at' => time(), 'tab_content' => '查看详情']);
    }

    /**
     * 添加黑名单
     * @param  AddBlackListMessage  $message
     */
    public function addBlackList(AddBlackListMessage $message)
    {
        $uid = $message->getUid();
        $end_time = 1893427200;
        $account_type = 'uid';
        $type = 'all';
        $state = 3;
        $data['end_at'] = $end_time;
        $data['account_type'] = $account_type;
        $data['account'] = $uid;
        BlackListModel::create($data);
        // 永久封禁
        if ($account_type == 'uid' && $type == 'all') {
            $user = UserModel::find($uid);
            $user->headimgurl = 'https://oss.pocketuniversity.cn/media/2019-08-14/5d53e27a8d81e.png';
            $user->save();
            // 封禁卡片
            if ($user->partner_id) {
                $partner = PartnerModel::find($user->partner_id);
                $partner->is_sell = -1;
                $partner->black_at = $end_time;
                $partner->save();
            }
            // 加入风险帐号列表
            RiskListModel::create(['uid' => $user->uid, 'weixin' => $user->weixin, 'qq' => $user->qq, 'phone' => $user->phone]);
            // 手机号码封禁
            if ($state == 3) {
                if ($user->phone) {
                    BlackListModel::create(['type' => 'all', 'account_type' => 'phone', 'account' => $user->phone, 'end_at' => 1893427200, 'state' => 3]);
                }
            }
        }
    }

    /**
     * 删除黑名单
     * @param  DeleteBlackListMessage  $message
     */
    public function deleteBlackList(DeleteBlackListMessage $message)
    {
        $uid = $message->getUid();
        $black = BlackListModel::where('account_type','uid')
            ->where('account', $uid)
            ->first();

        $user = UserModel::find($uid);
        // 卡片解禁
        $partner = PartnerModel::find($user->partner_id);
        $partner->black_at = 0;
        $partner->save();

        $black->delete();

        if ($user->phone) {
            BlackListModel::where(['account_type' => 'phone', 'account' => $user->phone])->delete();
        }
    }
}