import {native, Sprite} from 'cc'
import {JSB} from 'cc/env'

import {TMGContext} from '../TMGContext'

import {TMGAudioCtrlImpl} from './TMGAudioCtrlImpl';
import {TMGAudioEffectCtrlImpl} from './TMGAudioEffectCtrlImpl';
import {TMGPTTImpl} from './TMGPTTImpl';
import {TMGRoomImpl} from './TMGRoomImpl';

export class TMGContextImpl implements TMGContext {
  static getTMGInstance(): TMGContext {
    if (!TMGContextImpl.TmgInstance) {
      TMGContextImpl.TmgInstance = new TMGContextImpl();
    }
    return TMGContextImpl.TmgInstance;
  }

  static getAuthBuffer(appId: number, roomId: string, userId: string, key: string): string {
    if (TMGContextImpl.getTMGInstance() && TMGContextImpl.getTMGInstance().gmeContext) {
      return TMGContextImpl.getTMGInstance().gmeContext.getAuthBuffer(appId, roomId, userId, key);
    }

    return '';
  }

  private static TmgInstance: TMGContextImpl|null;
  private gmeContext: TMGContextBindings|undefined;
  private room: TMGRoomImpl|null;
  private ptt: TMGPTTImpl|null;
  private audioCtrl: TMGAudioCtrlImpl|null;
  private audioEffectCtrl: TMGAudioEffectCtrlImpl|null;

  private constructor() {
    this.gmeContext = new TMGContextBindings();
    this.room = new TMGRoomImpl(this.gmeContext);
    this.ptt = new TMGPTTImpl(this.gmeContext);
    this.audioCtrl = new TMGAudioCtrlImpl(this.gmeContext);
    this.audioEffectCtrl = new TMGAudioEffectCtrlImpl(this.gmeContext);
  }

  SetTMGDelegate(delegate: TMGDelegate): void {
    this.gmeContext.setTMGDelegate(delegate);
  }

  SetRegion(region: string): void {
    this.gmeContext.setRegion(region);
  }

  Init(appId: string, userID: string): number {
    return this.gmeContext.init(appId, userID);
  }

  Poll(): void {
    this.gmeContext.poll();
  }

  Pause(): number {
    return this.gmeContext.pause();
  }

  Resume(): number {
    return this.gmeContext.resume();
  }

  Uninit(): number {
    return this.gmeContext.uninit();
  }

  SetScene(scene: number): number {
    return this.gmeContext.setScene(scene);
  }

  SetAudioRole(role: number): number {
    return this.gmeContext.setAudioRole(role);
  }

  SetRangeAudioMode(mode: number): number {
    return this.gmeContext.setRangeAudioMode(mode);
  }

  SetRangeAudioTeamID(teamId: number): number {
    return this.gmeContext.setRangeAudioTeamID(teamId);
  }

  EnterRoom(roomId: string, roomType: number, userSig: string): number {
    return this.gmeContext.enterRoom(roomId, roomType, userSig);
  }

  ExitRoom(): number {
    return this.gmeContext.exitRoom();
  }

  IsRoomEntered(): boolean {
    return this.gmeContext.isRoomEntered();
  }

  SetLogLevel(logLevel: number, consoleLogLevel: number): number {
    return this.gmeContext.setLogLevel(logLevel, consoleLogLevel);
  }

  SetLogPath(logDir: string): number {
    return this.gmeContext.setLogPath(logDir);
  }

  GetLogPath(): string {
    return this.gmeContext.getLogPath();
  }

  GetSDKVersion(): string {
    return this.gmeContext.getSDKVersion();
  }

  SetAppVersion(appVersion: string): void {
    this.gmeContext.setAppVersion(appVersion);
  }

  ShowDebugView(show: boolean): number {
    return this.gmeContext.showDebugView(show);
  }

  GetRoom(): TMGRoom {
    return this.room;
  }

  GetPTT(): TMGPTT {
    return this.ptt;
  }

  GetAudioCtrl(): TMGAudioCtrl {
    return this.audioCtrl;
  }

  GetAudioEffectCtrl(): TMGAudioEffectCtrl {
    return this.audioEffectCtrl;
  }

  SetRecvMixStreamCount(count: number): number {
    return this.gmeContext.setRecvMixStreamCount(count);
  }

  SetAdvanceParams(key: string, value: string): number {
    return this.gmeContext.setAdvanceParams(key, value);
  }

  GetAdvanceParams(key: string): string {
    return this.gmeContext.getAdvanceParams(key);
  }

  CheckMicPermission(): number {
    return this.gmeContext.checkMicPermission();
  }
}
