package com.adealink.weparty.room.manager

import com.adealink.frame.base.CommonDataNullError
import com.adealink.frame.base.Rlt
import com.adealink.frame.base.fastLazy
import com.adealink.frame.frame.BaseFrame
import com.adealink.weparty.App
import com.adealink.weparty.module.room.data.MyRoomInfo
import com.adealink.weparty.module.room.data.createRoomPermissionLD
import com.adealink.weparty.module.room.listener.IRoomListener
import com.adealink.weparty.room.data.CreateRoomReq
import com.adealink.weparty.room.datasource.remote.RoomHttpService
import kotlinx.coroutines.launch

val roomManager: IRoomManager by lazy { RoomManager() }

class RoomManager : BaseFrame<IRoomListener>(), IRoomManager {

    private val roomHttpService by fastLazy {
        App.instance.networkService.getHttpService(RoomHttpService::class.java)
    }

    private var myRoomInfo: MyRoomInfo? = null
    private var lastCheckTs = 0L

    override fun getLocalMyRoomInfo(): MyRoomInfo? {
        return myRoomInfo
    }

    override suspend fun getMyRoomInfo(): Rlt<MyRoomInfo> {
        return getMyRoomInfoInner()
    }

    private var checkRoomFail = false


    override fun checkRoomPermission() {
        launch {
            if (!checkRoomFail && (System.currentTimeMillis() - lastCheckTs) < 300_000) {
                // 每5分钟只拉取一次
                return@launch
            }
            val rlt = getMyRoomInfoInner()
            lastCheckTs = System.currentTimeMillis()
            when (rlt) {
                is Rlt.Failed -> {
                    checkRoomFail = true
                }

                is Rlt.Success -> {
                    checkRoomFail = false
                    myRoomInfo = rlt.data
                    createRoomPermissionLD.postValue(myRoomInfo?.canCreateRoom() ?: false)
                }
            }
        }
    }

    private suspend fun getMyRoomInfoInner(): Rlt<MyRoomInfo> {
        when (val rlt = roomHttpService.checkCreateRoomPermission()) {
            is Rlt.Failed -> {
                return rlt
            }

            is Rlt.Success -> {
                val res = rlt.data.data ?: return Rlt.Failed(CommonDataNullError())
                val myRoomInfo = MyRoomInfo(
                    res.isOwner,
                    res.roomTitle,
                    res.roomCover,
                    res.forbidAudio,
                    res.roomTypes
                )
                return Rlt.Success(myRoomInfo)
            }
        }
    }

    override suspend fun createRoom(req: CreateRoomReq): Rlt<String> {
        when (val rlt = roomHttpService.createRoom(req)) {
            is Rlt.Failed -> {
                return rlt
            }

            is Rlt.Success -> {
                val roomId = rlt.data.data?.roomId
                if (roomId.isNullOrEmpty()) {
                    return Rlt.Failed(CommonDataNullError())
                }
                myRoomInfo?.roomCover = req.roomCover
                myRoomInfo?.roomTitle = req.roomTitle
                myRoomInfo?.forbidAudio = req.forbidAudio
                return Rlt.Success(roomId)
            }
        }
    }

    override fun clear() {
        super.clear()
        myRoomInfo = null
        lastCheckTs = 0L
        checkRoomFail = false
    }


}