package com.adealink.weparty.room

import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.IError
import com.adealink.frame.base.Rlt
import com.adealink.frame.coroutine.dispatcher.Dispatcher
import com.adealink.frame.media.IMediaOperator
import com.adealink.frame.media.MediaConflictConfig
import com.adealink.frame.media.MediaInfo
import com.adealink.frame.room.data.LeaveRoomReason
import com.adealink.frame.spi.RegisterService
import com.adealink.frame.util.AppUtil
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.commonui.widget.floatview.data.IFloatData
import com.adealink.weparty.media.MediaConflictDialog
import com.adealink.weparty.media.MediaType
import com.adealink.weparty.module.room.IRoomService
import com.adealink.weparty.room.create.CreateRoomDialog
import com.adealink.weparty.room.manager.roomManager
import com.adealink.weparty.room.minimize.manager.minimizeRoomManager
import com.adealink.weparty.room.minimize.view.MinimizedRoomFloatData
import com.adealink.weparty.room.minimize.view.MinimizedRoomFloatView
import com.adealink.weparty.room.sdk.service.roomService
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext

@RegisterService(IRoomService::class)
class RoomServiceImpl : IRoomService {

    override fun init() {
        minimizeRoomManager.init()
    }

    override fun getMediaOperator(): IMediaOperator {
        return object : IMediaOperator {

            override suspend fun isMediaIn(): Boolean {
                return roomService.joinController.isRoomJoiningOrJoined()
            }

            override suspend fun leaveMedia(): Rlt<Any> {
                roomService.joinController.leaveRoom(LeaveRoomReason.ENTER_OTHER_ROOM, true)
                return Rlt.Success(Any())
            }

            override suspend fun confirmExitConflictMedia(
                exitMediaInfo: MediaInfo,
                enterMediaInfo: MediaInfo
            ): Rlt<Any> {
                val currentActivity =
                    AppUtil.currentActivity as? BaseActivity ?: return Rlt.Success(Any())
                return withContext(Dispatcher.UI) {
                    suspendCancellableCoroutine { continuation ->
                        var confirm = false
                        MediaConflictDialog.show(
                            exitMediaInfo,
                            enterMediaInfo,
                            currentActivity.supportFragmentManager,
                            onPositive = {
                                confirm = true
                            },
                            onNegative = null,
                            onDismiss = {
                                if (continuation.isActive) {
                                    if (confirm) {
                                        continuation.resume(Rlt.Success(Any()), null)
                                    } else {
                                        continuation.resume(Rlt.Failed(IError()), null)
                                    }
                                }
                            }
                        )
                    }
                }
            }

            override fun getConflictConfig(): MediaConflictConfig {
                return MediaConflictConfig(
                    MediaInfo(
                        MediaType.CHAT_ROOM.type,
                        getCompatString(R.string.room_chat)
                    ), false
                )
            }

            override fun getJoinedRoomId(): String? {
                return roomService.joinController.getJoinedRoomId()
            }

            override suspend fun rejoinRoom(): Rlt<Any> {
                return Rlt.Success(Unit)
            }

        }
    }

    override fun checkRoomPermission() {
        roomManager.checkRoomPermission()
    }

    override fun createRoom(activity: BaseActivity) {
        CreateRoomDialog().show(activity.supportFragmentManager)
    }

    override fun getMinimizedRoomView(data: IFloatData): MinimizedRoomFloatView {
        return MinimizedRoomFloatView(data as MinimizedRoomFloatData)
    }

    override fun getJoinedRoomId(): String? {
        return roomService.joinController.getJoinedRoomId()
    }

    override fun isInRoom(): Boolean {
        return !roomService.joinController.getJoinedRoomId().isNullOrEmpty()
    }

    override fun getService(): IRoomService {
        return this
    }

    override fun onLogout() {
        roomService.clear()
        minimizeRoomManager.removeMinimizedRoomView()
        roomManager.clear()
    }


}