<?php

namespace app\admin\model\fastim;

use think\Model;
use addons\fastim\library\Common;
use addons\fastim\library\CommonCode;


class Records extends Model
{

    // 表名
    protected $name = 'fastim_message';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'status_text',
        'format_message'
    ];


    public function getTypeList()
    {
        return ['default' => __('Type default'), 'image' => __('Type image'), 'audio' => __('Type audio'), 'video' => __('Type video'), 'file' => __('Type file'), 'link' => __('Type link'), 'system' => __('Type system'), 'group_apply' => __('Type group_apply'), 'group_notice' => __('Type group_notice'), 'group_invitation' => __('Type group_invitation'), 'friend_apply' => __('Type friend_apply'), 'kbs_list' => __('Type kbs_list'), 'group_chat_notice' => __('Type group_chat_notice'), 'voice' => __('Type voice')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '4' => __('Status 4')];
    }

    public function getFormatMessageAttr($value, $data)
    {
        if (in_array($data['type'], CommonCode::$messageJsonTypes)) {
            $message = json_decode($data['message'], true);
            if ($data['type'] == 'group_invitation') {
                $invitationUser     = Common::getImUserInfo($message['invitation_user']);
                $message['message'] = $invitationUser['nickname'] . '邀请接受人入群';
            } elseif ($data['type'] == 'group_notice') {
                if ($message['action'] == 'quit') {
                    $userInfo           = Common::getImUserInfo($message['quit_user']);
                    $message['message'] = $userInfo['nickname'] . '退出了群聊';
                } elseif ($message['action'] == 'dismiss') {
                    $message['message'] = $message['notice'];
                } elseif ($message['action'] == 'refuse') {
                    $info     = Common::getImGroupChat($data['group_id']);
                    $userInfo = Common::getImUserInfo($message['refuse_user']);
                    if ($data['sender_id'] == $info['leader']) {
                        $message['message'] = '管理员拒绝' . $userInfo['nickname'] . '加入群聊';
                    } else {
                        $message['message'] = $userInfo['nickname'] . '拒绝加入群聊';
                    }
                } elseif ($message['action'] == 'propose') {
                    $message['message'] = '管理员将接收人请出群聊';
                } else {
                    $message['message'] = $message['notice'] ? $message['notice'] : $message['message'] ?? '';
                }
            }
            return $message;
        } else {
            return htmlspecialchars_decode($data['message']);
        }
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list  = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list  = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function groupchat()
    {
        return $this->belongsTo('app\admin\model\fastim\Groupchat', 'group_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function sender()
    {
        return $this->belongsTo('app\admin\model\fastim\User', 'sender_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function recipient()
    {
        return $this->belongsTo('app\admin\model\fastim\User', 'recipient_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

}
