<?php

namespace App\Services\YunXinMessageCallBack;

use App\Models\ChatLinkman;
use App\Models\ChatMessage;



class Person 
{
    public function handle($params)
    {
        $this->saveMessage($params);
    }

    public function recall($params) {
        $clientId = $params["clientId"];
        if ($clientId) {
            ChatMessage::where('im_msg_id', $clientId)->update(['is_recall'=> 1]);
        }
    }

    /**
     * 写入群聊记录
     */
    private function saveMessage($params)
    {
        // $params = request()->all(['msgType', 'fromAccount', 'to', 'attach', 'body',"msgidClient"]);

        $im_msg_id = $params['msgidClient'];
        if ($im_msg_id) {
            $gm = ChatMessage::where('im_msg_id', $im_msg_id)->first();
            if ($gm) return;
        }


        if ($params['msgType'] == 'TEXT') {
            $content = json_encode(['body' => $params['body']],JSON_UNESCAPED_UNICODE);
        } else {
            $content = $params['attach'];
            $content_arr = json_decode($content, true);
            $content_arr["extend"] = $params["ext"]??"";
            $content = json_encode($content_arr);
        }
        $from_user_id = get_userid_by_accid($params['fromAccount']);
        $to_user_id = get_userid_by_accid($params['to']);
        if (!is_numeric($from_user_id) || !is_numeric($to_user_id)) return;
        $chat_message = new ChatMessage();
        $chat_message->from_user_id = $from_user_id;
        $chat_message->to_user_id = $to_user_id;
        $chat_message->content = $content;
        $chat_message->content_type = $params['msgType'];
        $chat_message->im_msg_id = $params['msgidClient']??null;
        $chat_message->msg_timestamp = $params["msgTimestamp"];
        $chat_message->data = json_encode($params, JSON_UNESCAPED_UNICODE);
        $chat_message->save();
        
        $chatLinkman1 = ChatLinkman::where("user_id",$from_user_id)->where("other_user_id",$to_user_id)->first();
        if ($chatLinkman1) {
            ChatLinkman::where("user_id",$from_user_id)->where("other_user_id",$to_user_id)->update(["last_msg_id"=>$chat_message->id, "is_hidden"=>0]);
        } else {
            ChatLinkman::create([
                "user_id"=>$from_user_id,
                "other_user_id"=>$to_user_id,
                "last_msg_id"=>$chat_message->id,
            ]);
        }

        $chatLinkman2 = ChatLinkman::where("user_id",$to_user_id)->where("other_user_id",$from_user_id)->first();
        if ($chatLinkman2) {
            ChatLinkman::where("user_id",$to_user_id)->where("other_user_id",$from_user_id)->update(["last_msg_id"=>$chat_message->id, "is_hidden"=>0]);
        } else {
            ChatLinkman::create([
                "user_id"=>$to_user_id,
                "other_user_id"=>$from_user_id,
                "last_msg_id"=>$chat_message->id,
            ]);
        }

    }

    public function receiveMsg($params) {
        $objects = $params["objects"];
        $objects = json_decode($objects, true);
        if (count($objects)) {
            $clientIds = [];
            foreach($objects as $object) {
                $clientId = $object["msgidClient"]??"";
                $clientIds[] = $clientId;
            }
            ChatMessage::whereIn('im_msg_id', $clientIds)->update(['status'=> 1]);
        }
    }
}