<?php


namespace App\Http\Controllers\V1;


use App\Models\User;
use App\Services\JPushService;
use App\Services\TencentImGroupService;
use Illuminate\Http\Request;
use function Symfony\Component\Translation\t;

class TencentController extends Controller
{
    public function notify(Request $request){
        $data = $request->post() ? : '';
        file_put_contents('./logs/tencent.log',var_export($data,true).PHP_EOL,FILE_APPEND);
        if(empty($data)) return;
        switch ($data['CallbackCommand']){
            /**
             * 在线状态
             */
            case "State.StateChange":
                //状态变更回调
                if($data['Info']['Action']=='Login'){
                    $online = 1;
                }else{
                    $online = 0;
                }
                //修改用户状态
                User::query()->where(['tencent_im_user_id'=>$data['Info']['To_Account'],'status'=>1])->update(['online'=>$online]);
                break;
            /**
             * 资料关系链
             */
            case "Sns.CallbackPrevFriendAdd":
                //添加好友之前回调

                break;
            case "Sns.CallbackPrevFriendResponse":
                //添加好友回应之前回调

                break;
            case "Sns.CallbackFriendAdd":
                //添加好友之后回调

                break;
            case "Sns.CallbackFriendDelete":
                //删除好友之后回调

                break;
            case "Sns.CallbackBlackListAdd":
                //添加黑名单之后回调

                break;
            case "Sns.CallbackBlackListDelete":
                //删除黑名单之后回调

                break;

            /**
             * 单聊消息
             */
            case "C2C.CallbackBeforeSendMsg":
                //发单聊消息之前回调

                break;
            case "C2C.CallbackAfterSendMsg":
                //发单聊消息之后回调

                $from_user = User::query()->where('tencent_im_user_id',$data['From_Account'])->first();
                $to_user = User::query()->where('tencent_im_user_id',$data['To_Account'])->first();
                $msgType = $data['MsgBody'][0]['MsgType'];
                $MsgContent = $data['MsgBody'][0]['MsgContent'];
                $content = '';
                if($msgType=='TIMTextElem'){
                    $content = $MsgContent['Text'];
                }elseif($msgType=='TIMCustomElem'){
                    $content = '['.trans('api.Custom_message',[],$to_user->lang).']';
                }elseif($msgType=='TIMImageElem'){
                    $content = '['.trans('api.Picture',[],$to_user->lang).']';
                }elseif($msgType=='TIMSoundElem'){
                    $content = '['.trans('api.Voice',[],$to_user->lang).']';
                }elseif($msgType=='TIMLocationElem'){
                    $content = '['.trans('api.Position',[],$to_user->lang).']';
                }
                //通过别名发送：
                JPushService::pushNotify([
                    'title' => trans('api.New_message',[],$to_user->lang),
                    'content' => $from_user->name.'：'.$content,
                    'alias' => 'user_id_' . $to_user->id,
                    'extras' => '{type:1}',
                    'type' => JPushService::PUSH_TYPE_ALIAS,
                ]);

                break;
            case "C2C.CallbackAfterMsgReport":
                //单聊消息已读上报后回调

                break;
            case "C2C.CallbackAfterMsgWithDraw":
                //单聊消息撤回后回调

                break;

            /**
             * 群组系统
             */
            case "Group.CallbackBeforeCreateGroup":
                //创建群组之前回调

                break;
            case "Group.CallbackAfterCreateGroup":
                //创建群组之后回调

                break;
            case "Group.CallbackBeforeApplyJoinGroup":
                //申请入群之前回调

                break;
            case "Group.CallbackBeforeInviteJoinGroup":
                //拉人入群之前回调

                break;
            case "Group.CallbackAfterNewMemberJoin":
                //新成员入群之后回调

                break;
            case "Group.CallbackAfterMemberExit":
                //群成员离开之后回调

                break;
            case "Group.CallbackBeforeSendMsg":
                //群内发言之前回调

                break;
            case "Group.CallbackAfterSendMsg":
                //群内发言之后回调
                $from_user = User::query()->where('tencent_im_user_id',$data['From_Account'])->first();
                $groupId = $data['GroupId'];
                $msgType = $data['MsgBody'][0]['MsgType'];
                $MsgContent = $data['MsgBody'][0]['MsgContent'];
                //获取群聊信息和成员信息
                $groupService = new TencentImGroupService();
                $groupInfo = $groupService->getGroupInfo($groupId);
                $groupName = $groupInfo['GroupInfo'][0]['Name'];
                $groupMemberList = $groupInfo['GroupInfo'][0]['MemberList'];
                foreach ($groupMemberList as $k=>$v){
                    if($v['Member_Account']==$data['From_Account']){
                        continue;
                    }
                    $to_user =  User::query()->where('tencent_im_user_id',$v['Member_Account'])->first();
                    $content = '';
                    if($msgType=='TIMTextElem'){
                        $content = $MsgContent['Text'];
                    }elseif($msgType=='TIMCustomElem'){
                        $content = '['.trans('api.Custom_message',[],$to_user->lang).']';
                    }elseif($msgType=='TIMImageElem'){
                        $content = '['.trans('api.Picture',[],$to_user->lang).']';
                    }elseif($msgType=='TIMSoundElem'){
                        $content = '['.trans('api.Voice',[],$to_user->lang).']';
                    }elseif($msgType=='TIMLocationElem'){
                        $content = '['.trans('api.Position',[],$to_user->lang).']';
                    }
                    //通过别名发送：
                    JPushService::pushNotify([
                        'title' => $groupName,
                        'content' => $from_user->name.'：'.$content,
                        'alias' => 'user_id_' . $to_user->id,
                        'extras' => '{type:1}',
                        'type' => JPushService::PUSH_TYPE_ALIAS,
                    ]);
                }
                break;
            case "Group.CallbackAfterGroupFull":
                //群组满员之后回调

                break;
            case "Group.CallbackAfterGroupDestroyed":
                //群组解散之后回调

                break;
            case "Group.CallbackAfterGroupInfoChanged":
                //群组资料修改之后回调

                break;

        }


    }
}
