/*
 * @Author: seimin
 * @Date: 2024-09-24 16:34:47
 * @LastEditors: seimin
 * @LastEditTime: 2024-11-28 03:12:59
 * @Description: 来电弹屏websocket服务
 */
import { Injectable } from "@angular/core";
import { Observable } from "rxjs";
import { WebsocketPhoneService } from './websocket-phone.service';
import { WebsocketHjzxService } from './websocket-hjzx.service';
import { MainService } from './main.service';
import http from "../../assets/js/http";

@Injectable({
  providedIn: "root",
})
export class WebsocketIncomingService {
  constructor(
    private mainService: MainService,
    private phoneService: WebsocketPhoneService,
    private hjzxService: WebsocketHjzxService,
  ) { }

  // 连接websocket { userCount }
  private user = JSON.parse(localStorage.getItem("user")); //用户信息
  connectWs(phoneNumber): Observable<any> {
    let userCount = this.user.user.account + "|" + phoneNumber;
    if(this.getSign() === 'box'){
      return this.phoneService.connectWs(http.phoneWs, { userCount });
    }
  }
  // 断开websocket
  closeWs(flag: boolean = false) {
    console.log('sign', this.getSign())
    if(this.getSign() === 'box'){
      this.phoneService.closeWs(flag);
    }else if(this.getSign() === 'callCenter'){
      this.hjzxService.closeWs(flag);
    }
  }

  // 绑定的分机号码
  private phoneNumber:string = '';
  // 获取绑定分机号码
  getPhoneNumber() {
    return this.phoneNumber || sessionStorage.getItem('phoneNumber');
  }
  // 设置绑定分机号码
  setPhoneNumber(phoneNumber: string) {
    this.phoneNumber = phoneNumber;
    sessionStorage.setItem('phoneNumber', phoneNumber);
  }
  // 删除绑定分机号码
  removePhoneNumber() {
    this.phoneNumber = '';
    sessionStorage.removeItem('phoneNumber');
  }

  // 上次绑定的分机号码
  private phoneNumberPre:string = '';
  // 获取上次绑定分机号码
  getPhoneNumberPre() {
    return this.phoneNumberPre || sessionStorage.getItem('phoneNumberPre');
  }
  // 设置上次绑定分机号码
  setPhoneNumberPre(phoneNumberPre: string) {
    this.phoneNumberPre = phoneNumberPre;
    sessionStorage.setItem('phoneNumberPre', phoneNumberPre);
  }
  // 删除上次绑定分机号码
  removePhoneNumberPre() {
    this.phoneNumberPre = '';
    sessionStorage.removeItem('phoneNumberPre');
  }

  // 标识(呼叫中心或录音盒)
  // callCenter|box
  private sign:string = '';
  // 获取绑定标识
  getSign() {
    return this.sign || sessionStorage.getItem('sign');
  }
  // 设置绑定标识
  setSign(sign: string) {
    this.sign = sign;
    sessionStorage.setItem('sign', sign);
  }
  // 删除绑定标识
  removeSign() {
    this.sign = '';
    sessionStorage.removeItem('sign');
  }

  // 呼叫中心服务地址
  private socketUrl:string = '';
  // 获取呼叫中心服务地址
  getSocketUrl() {
    return this.socketUrl || sessionStorage.getItem('socketUrl');
  }
  // 设置呼叫中心服务地址
  setSocketUrl(socketUrl: string) {
    this.socketUrl = socketUrl;
    sessionStorage.setItem('socketUrl', socketUrl);
  }
  // 删除呼叫中心服务地址
  removeSocketUrl() {
    this.socketUrl = '';
    sessionStorage.removeItem('socketUrl');
  }
}
