<?php

namespace App\Services\YunXinMessageCallBack;

use App\Models\Group;
use App\Models\GroupFile;
use App\Models\GroupMessage;
use App\Models\GroupMessageConfig;
use App\Models\GroupUser;
use Illuminate\Support\Facades\Log;

class Team
{

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

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

            // 文件消息  撤回存储文件
            GroupFile::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 = GroupMessage::where('im_msg_id', $im_msg_id)->first();
            if ($gm)
                return;
        }

        $group_id = Group::where('im_chat_id', $params['to'])->value('id');

        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);
        }

        $group_message = new GroupMessage();
        $group_message->user_id = get_userid_by_accid($params['fromAccount']);
        $group_message->group_id = $group_id;
        $group_message->content = $content;
        $group_message->content_type = $params['msgType'];
        $group_message->im_msg_id = $params['msgidClient'] ?? null;
        $group_message->msg_timestamp = $params["msgTimestamp"];
        $group_message->data = json_encode($params, JSON_UNESCAPED_UNICODE);
        $group_message->save();

        Group::where("id", $group_id)->update(["last_msg_id" => $group_message->id]);
        GroupUser::where("group_id", $group_id)->where("is_hidden", 1)->update(['is_hidden' => 0]);


        // 文件另存一份
        if ($params["msgType"] == "FILE") {
            $fileJson = $params['attach'];
            $fileArr = json_decode($fileJson, true);
            $file = new GroupFile();
            $file->group_id = $group_id;
            $file->im_chat_id = $params['to'];
            $file->user_id = get_userid_by_accid($params['fromAccount']);
            $file->name = $fileArr["name"] ?? "";
            $file->ext = $fileArr["ext"] ?? "";
            $file->url = $fileArr["url"] ?? "";
            $file->size = $fileArr["size"] ?? "";
            $file->im_msg_id = $params['msgidClient'] ?? null;
            $file->msg_timestamp = $params["msgTimestamp"];
            $file->save();

        }
    }

    public function readMsg($params)
    {
        // 已读时间
        $ackMsgTime = $params["ackMsgTime"];
        // 已读群id
        $ackTeamId = $params["ackTeamId"];
        // accid
        $accid = $params["accid"];
        $user_id = get_userid_by_accid($accid);

        $group = Group::where('im_chat_id', $ackTeamId)->first();
        if (empty($group))
            return;

        $config = GroupMessageConfig::where("user_id", $user_id)->where("group_id", $group->id)->first();
        if (empty($config)) {
            GroupMessageConfig::create(["user_id" => $user_id, "group_id" => $group->id, "msg_time" => $ackMsgTime]);
        } else {
            GroupMessageConfig::where("user_id", $user_id)->where("group_id", $group->id)->update(["msg_time" => $ackMsgTime]);
        }

    }
}