package com.zxhl.main.page.fragment

import android.util.Log
import android.view.View
import com.tencent.liteav.debug.GenerateTestUserSig
import com.tencent.liteav.trtcvoiceroom.model.TRTCVoiceRoom
import com.tencent.liteav.trtcvoiceroom.model.TRTCVoiceRoomCallback
import com.tencent.liteav.trtcvoiceroom.model.TRTCVoiceRoomDef
import com.zxhl.cms.common.FragmentAdapter
import com.zxhl.cms.common.base.BaseFragment
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.net.model.chat.ChatRoomEntity
import com.zxhl.cms.net.model.chat.RoomTypeEntity
import com.zxhl.cms.utils.JumpUtils
import com.zxhl.cms.widget.CenterDialog
import com.zxhl.cms.widget.ChatRoomDialogUtils
import com.zxhl.main.R
import com.zxhl.main.page.contract.VoiceRoomContract
import com.zxhl.main.page.presenter.VoiceRoomPrsenter
import kotlinx.android.synthetic.main.fragment_layout_voice_room.*
import kotlin.collections.ArrayList
import kotlin.collections.List

class VoiceRoomFragment : BaseFragment(),VoiceRoomContract.View {
    private var mTRTCVoiceRoom: TRTCVoiceRoom? = null
    private var roomIdList: List<Int>? = ArrayList()
    var userId: String? = ""
    var userName: String? = ""
    var userHeadImg: String? = ""
    private var mPresenter:VoiceRoomPrsenter?=null
    override fun layoutID(): Int {
        return R.layout.fragment_layout_voice_room
    }

    override fun init(view: View?) {
        mPresenter= VoiceRoomPrsenter(this)
        mPresenter?.getTab()
        id_ll_creat_room?.setOnClickListener {
            JumpUtils.CreateRoomJump()
        }
        val user = SettingPreference.getUserInfoData()
        userId = user.uid.toString()
        userName = user.nickname
        userHeadImg = user.headImage
        mTRTCVoiceRoom = TRTCVoiceRoom.sharedInstance(mActivity)
        mTRTCVoiceRoom?.login(
            GenerateTestUserSig.SDKAPPID,
            userId,
            GenerateTestUserSig.genTestUserSig(userId),
            object : TRTCVoiceRoomCallback.ActionCallback {
                override fun onCallback(code: Int, msg: String) {
                    Log.e("MXL", "code" + msg)
                    if (code == 0) {
                        mTRTCVoiceRoom?.setSelfProfile(
                            userName,
                            userHeadImg,
                            object :
                                TRTCVoiceRoomCallback.ActionCallback {
                                override fun onCallback(code: Int, msg: String) {
                                    if (code == 0) {
                                        Log.d("MXL", "setSelfProfile success")

                                    }
                                }
                            })
                    }
                }
            })


    }

    fun getRooList() {
        mTRTCVoiceRoom?.getRoomInfoList(roomIdList,object :TRTCVoiceRoomCallback.RoomInfoCallback{
            override fun onCallback(
                code: Int,
                msg: String?,
                list: MutableList<TRTCVoiceRoomDef.RoomInfo>?
            ) {

            }

        })
    }
    override fun lazyLoad() {
        super.lazyLoad()
        if (isInitial && isVisible) {
            mPresenter?.hasWinterBox()
        }

    }

    override fun before() {
        super.before()
    }

    override fun showWinterBox(has: Boolean?) {
        if(has==true){
            CenterDialog.showWinterBox(mActivity,View.OnClickListener {
                    JumpUtils.WinterBoxJump()
            })
        }

    }

    override fun setTab(result: List<RoomTypeEntity>?) {
        if (result != null && result.size > 0) {
            initVeiwPager(result)
        }
    }
    fun initVeiwPager(result: List<RoomTypeEntity>) {
        val adapter = FragmentAdapter(childFragmentManager)
        viewPager?.adapter = adapter

        for (i in 0 until (result.size )) {
            val title = result[i].name ?: ""
            adapter?.addFrag(OneFragment.newInstance(title), title)
        }
        adapter?.notifyDataSetChanged()
        id_fragment_home_tab?.setViewPager(viewPager)
        id_fragment_home_tab?.setOverScrollMode(View.OVER_SCROLL_ALWAYS)
    }
}