<?php
namespace app\controller;
use support\Request;
use support\Response;
use think\facade\Db;
use app\model\User as UserModel;
class HookController{
    function index(){
        return 'ok';
    }
    function __call($method, $args):Response
    {
        //log_alert(Input());
        return json([
            "actionCode"    => 0,
            "errCode"   => 0,
            "errMsg"    => "",
            "errDlt"    => "",
            "nextCode"=> 0
        ]);
    }
    /**
     * 在发送单聊消息前的回调
     * @return void
     */
    public function callbackBeforeSendSingleMsgCommand(Request $request): Response
    {
        //log_alert(Input());
        
        $user_id = Input('sendID');
        $recv_user_id = Input( 'recvID');
        $status = Input('status',1);
        $sessionType = Input('sessionType',null);
        if($status == 1 && $sessionType != 4){
            //$max = 10000000000;//限制消息数量
            $user_rights = get_user_rights($user_id);
            $max = $user_rights['max_send_msg_count'];
            $sended_msg_count = cache('single_msg_count_'.$user_id)??0 + cache('group_msg_count_'.$user_id)??0;
            if($sended_msg_count > $max){
                return json([
                    "actionCode"    => 0,
                    "errCode"   => 1002,
                    "errMsg"    => "超出消息数量限制,请先开通或升级会员",
                    "errDlt"    => "超出消息数量限制,请先开通或升级会员",
                    "nextCode"=> 1
                ]);
            }
        }
        return json([
            "actionCode"    => 0,
            "errCode"   => 0,
            "errMsg"    => "",
            "errDlt"    => "",
            "nextCode"=> 0
        ]);
    }
    /**
     * 发送单聊消息后的回调
     * @return void
     */
    public function callbackAfterSendSingleMsgCommand(Request $request): Response
    {
        
        $user_id = Input('sendID');
        $recv_user_id = Input('recvID');
        $status = Input('status',1);
        $sessionType = Input('sessionType',null);
        if($status == 1 && $sessionType != 4){
            $key = '_msg_count_'.$user_id;
            if($sessionType == 1){
                $key = 'single'.$key;
            }
            if($sessionType == 2){
                $key = 'group'.$key;
            }
            cache_add($key,1);
        }
        return json([
            "actionCode"    => 0,
            "errCode"   => 0,
            "errMsg"    => "",
            "errDlt"    => "",
            "nextCode"=> 0
        ]);
    }
    /**
     * 发送群聊消息前的回调
     * @return void
     */
    public function callbackBeforeSendGroupleMsgCommand(Request $request): Response
    {
        //log_alert('callbackBeforeSendGroupleMsgCommand:');
        //log_alert(Input());
        return json([
            "actionCode"    => 0,
            "errCode"   => 0,
            "errMsg"    => "",
            "errDlt"    => "",
            "nextCode"=> 0
        ]);
    }
    /**
     * 发送群聊消息后的回调
     * @return void
     */
    public function callbackAfterSendGroupleMsgCommand(Request $request): Response
    {
        
        //log_alert('callbackAfterSendGroupleMsgCommand:');
        //log_alert(Input());
        return json([
            "actionCode"    => 0,
            "errCode"   => 0,
            "errMsg"    => "",
            "errDlt"    => "",
            "nextCode"=> 0
        ]);
    }
    
    /**
     * 发送好友申请之前的回调
     * @return void
     */
    public function callbackBeforeAddFriendCommand(Request $request): Response{
        $from_user_id = Input('fromUserID');
        $to_user_id = Input('toUserID');
        $handleResult = Input('handleResult');
        $key = 'friend_count_'.$from_user_id;
        $user_rights = get_user_rights($from_user_id);
        $max = $user_rights['max_friend_count'];
        if(cache($key) > $max){
            return json([
                "actionCode"    => 0,
                "errCode"   => 1001,
                "errMsg"    => "超出好友数量限制,请先开通或升级会员",
                "errDlt"    => "超出好友数量限制,请先开通或升级会员",
                "nextCode"  => 1
            ]);
        }
        return json([
            "actionCode"    => 0,
            "errCode"   => 0,
            "errMsg"    => "",
            "errDlt"    => "",
            "nextCode"=> 0
        ]);
    }
    
    /**
     * 发送好友申请之后的回调
     * @return void
     */
    public function callbackAfterAddFriendCommand(Request $request): Response
    {
        
        $from_user_id = Input('fromUserID');
        $to_user_id = Input('toUserID');
        cache_add('friend_count_'.$to_user_id,1);
        cache_add('friend_count_'.$from_user_id,1);
        return json([
            "actionCode"    => 0,
            "errCode"   => 0,
            "errMsg"    => "",
            "errDlt"    => "",
            "nextCode"=> 0
        ]);
    }
    /**
     * 在添加好友对方同意之前的回调
     * @return void
     */
    public function callbackBeforeAddFriendAgreeCommand(Request $request): Response
    {
        $from_user_id = Input('fromUserID');
        $to_user_id = Input('toUserID');
        $handleResult = Input('handleResult');
        if($handleResult == 1){
            $key = 'friend_count_'.$to_user_id;
            $user_rights = get_user_rights($to_user_id);
            $max = $user_rights['max_friend_count'];
            if(cache($key) > $max){
                return json([
                    "actionCode"    => 0,
                    "errCode"   => 1001,
                    "errMsg"    => "超出好友数量限制,请先开通或升级会员",
                    "errDlt"    => "超出好友数量限制,请先开通或升级会员",
                    "nextCode"=> 1
                ]);
            }
        }
        return json([
            "actionCode"    => 0,
            "errCode"   => 0,
            "errMsg"    => "",
            "errDlt"    => "",
            "nextCode"=> 0
        ]);
    }
    /**
     * 在添加好友对方同意之前的回调
     * @param Request $request
     * @return void
     */
    public function callbackAfterAddFriendAgreeCommand(Request $request):Response
    {
        return json([
            "actionCode"    => 0,
            "errCode"   => 0,
            "errMsg"    => "",
            "errDlt"    => "",
            "nextCode"=> 0
        ]);
    }
    /**
     * 用户在线状态回调
     * @return void
     */
    public function callbackAfterUserOnlineCommand(Request $request): Response
    {
        $user_id = Input('userID');
        //Db::name('user')->where('id',$user_id)->update(['online'=>1]);
        return json([
            "actionCode"    => 0,
            "errCode"   => 0,
            "errMsg"    => "",
            "errDlt"    => "",
            "nextCode"=> 0
        ]);
    }
    /**
     * 用户离线状态回调
     * @return void
     */
    public function callbackAfterUserOfflineCommand(Request $request): Response{
        
        $user_id = Input('userID');
        //Db::name('user')->where('id',$user_id)->update(['online'=>0]);
        return json([
            "actionCode"    => 0,
            "errCode"   => 0,
            "errMsg"    => "",
            "errDlt"    => "",
            "nextCode"=> 0
        ]);
    }
}