<?php

namespace app\entity\gateway\ws;

use app\enum\gateway\message\MessageType;
use app\enum\gateway\message\ScriptEnum;
use app\enum\gateway\message\SystemEnum;

/**
 * Ws消息入参
 */
class PutMessage
{
    readonly public ?MessageType $type;
    readonly public SystemEnum|ScriptEnum|null $event;
    readonly public array $data;
    public string $wsClientId;

    public int $userId = 0;
    public string $userType = '';
    public array $userClient = [];

    public function __construct(string $ws_client_id, string $type, string $event, array $data = [])
    {
        $this->wsClientId = $ws_client_id;
        if ($type == MessageType::System->value) {
            $this->type = MessageType::System;
            $this->event = SystemEnum::tryFrom($event);
        }else{
            $this->type = null;
        }
        $this->data = $data;
    }

    public function get($name, $default = null)
    {
        return $this->data[$name] ?? $default;
    }

    public function login(int $user_id, string $user_type, array $user_client = []): bool
    {
        if (empty($user_id) || empty($user_type) || count($user_client) == 0) {
            return false;
        }
        $this->userId = $user_id;
        $this->userType = $user_type;
        $this->userClient = $user_client;

        return true;
    }

    public function getClientId(): string
    {
        return $this->userClient['id'] ?? '';
    }

    public function getClientName()
    {
        return $this->userClient['name'] ?? '';
    }

    public function getClientVersion()
    {
        return $this->userClient['version'] ?? '';
    }
}