import { Component, OnInit } from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {Params} from '@angular/router';
import {WebsocketService} from '../../../services/websocket.service';

export enum SEND_TYPE {
    ALL = 'all',
    SINGLE = 'single'
}
@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.scss']
})
export class TestComponent implements OnInit {

    messages = [];                      // 消息列表
    message;                            // 发送的消息内容
    error: any;                         // 异常信息
    completed = false;                  // 发送完成
    type = SEND_TYPE.ALL;               // 默认类型发送给所有人
    users = [];                         // 登陆的用户
    sendToUser;                         // 需要发送消息的用户
    currentUser;                        // 当前用户

    constructor(
        private webSocketService: WebsocketService,
        private activatedRoute: ActivatedRoute
    ) {

        this.activatedRoute.queryParams.subscribe(queryParams => {
            console.log(queryParams.id);
            this.currentUser = queryParams.id;
        });
    }

    ngOnInit(): void {
        console.log(this.currentUser);
        // 连接websocket
        // this.webSocketService.connect(`ws://139.159.177.244:9302/brest/WebSocket/` + this.currentUser);
        // 接收消息
        this.webSocketService.messageSubject.subscribe(
            data => {
                // 如果是用户登陆,则添加到登陆列表中
                if (data.users) {
                    this.users = data.users;
                } else {
                    // 否则添加到消息列表
                    this.messages.push(data.msg);
                }
            },
            err => this.error = err,
            () => this.completed = true
        );
    }

    // send() {
    //     // 创建消息对象
    //     const msg = {
    //         msg: this.message,                                                    // 消息内容
    //         type: this.type === SEND_TYPE.ALL ? SEND_TYPE.ALL : SEND_TYPE.SINGLE, // 类型
    //         to: this.type === SEND_TYPE.SINGLE ? this.sendToUser : undefined            // 要发送的对象
    //     };
    //     // 发送
    //     this.webSocketService.sendMessage(JSON.stringify(msg));
    //     // 发送完成,情况message内容
    //     this.message = '';
    // }


}
