<script setup lang="ts">
import { onMounted, onUnmounted, provide, ref } from 'vue'
import { KkDaikan } from 'kankan-components'
import type { DaikanUserInfoType } from 'kankan-components'
const loading = ref(false)

const userInfo = ref<DaikanUserInfoType>({
  userId: 'test-user-1',
  roomId: 'test-roomid-1',
  avatar: '',
  nickName: 'xxxxx',
  role: 'leader',
})
const socketUrl = ref('wss://testws.4dkankan.com')
const socketUrlLib = ref(
  'https://cdn.bootcdn.net/ajax/libs/socket.io/4.5.4/socket.io.min.js'
)

onMounted(() => {
  const __win = window as any
  if (!__win.__sdk) {
    const __sdk = (__win.__sdk = new __win.KanKan({
      num: 'KJ-t-wOXfx2SDFy',
      server: '#DEMOSEVER#',
    }))
    provide('__sdk', __sdk)
    __sdk.Scene.on('ready', () => (loading.value = true))

    __sdk.mount('#scene').render()
  }
})
onUnmounted(() => {
  const __win = window as any
  if (__win.__sdk) {
    __win.__sdk = null
  }
})
const handleJoin = () => {
  console.log('isJoin')
}
const handleError = (data: any) => {
  console.log('handleError', data)
}
const handleSync = (data: any) => {
  console.log('handleSync', data)
}
</script>

<template>
  <div id="scene" class="scene">
    <Teleport v-if="loading" to=".kankan-plugins">
      <div xui_daikan_view>
        <kk-daikan
          :socket-lib="socketUrlLib"
          :user-info="userInfo"
          :socket-url="socketUrl"
          @join="handleJoin"
          @error="handleError"
          @sync="handleSync"
        />
      </div>
    </Teleport>
  </div>
</template>
<style>
html,
body,
#app {
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
}
.scene {
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
}
</style>
