package com.zxhl.main.page.activity

import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.tencent.liteav.trtcvoiceroom.model.TRTCVoiceRoom
import com.tencent.liteav.trtcvoiceroom.ui.room.VoiceRoomAnchorActivity
import com.tencent.trtc.TRTCCloudDef
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseActivity
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.net.model.chat.ToCreateRoomEntity
import com.zxhl.cms.utils.NoDoubleClickListener
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.widget.CenterDialog
import com.zxhl.main.R
import com.zxhl.main.page.adapter.RoomTypeAdapter
import com.zxhl.main.page.contract.CreateRoomContract
import com.zxhl.main.page.presenter.CreateRoomPresenter
import kotlinx.android.synthetic.main.activity_layout_create_room.*
import java.util.*

class CreatRoomActivity : BaseActivity(), CreateRoomContract.View,
    OnRecycleItemClickListener<RoomTypeEntity> {

    private var mTRTCVoiceRoom: TRTCVoiceRoom? = null
    private var mPresenter: CreateRoomPresenter? = null
    private var mAdapter: RoomTypeAdapter? = null
    var userId: String? = ""
    var userName: String? = ""
    var userHeadImg: String? = ""
    private var topicList: List<String>? = null
    private var topicStr: String? = ""
    private var remainingTimes: Int = 0

    private var entity: ChatRoomEntity? = null
    private val ROOM_COVER_ARRAY = arrayOf(
        R.drawable.bj_1,
        R.drawable.bj_2,
        R.drawable.bj_3,
        R.drawable.bj_4,
        R.drawable.bj_5,
        R.drawable.bj_6,
        R.drawable.bj_7,
        R.drawable.bj_8
    )

    //    )
    private val ROOM_COVER_ARRAY2 = arrayOf(
        "https://zx-cdn.zhangxinhulian.com/consumption/images/2022/2/afd627cbc916a7636865e65a0aa04a19ab9f99cd.jpg",
        "https://zx-cdn.zhangxinhulian.com/consumption/images/2022/2/cd8e5ac2d54541b0360f8f51062d839a10f47c65.jpg",
        "https://zx-cdn.zhangxinhulian.com/consumption/images/2022/2/43ca10da2260b015c65fdb323d0423c6842d9f3d.jpg",
        "https://zx-cdn.zhangxinhulian.com/consumption/images/2022/2/ad3d953cfefa33ef78503b63f863fed0f708d210.jpg",
        "https://zx-cdn.zhangxinhulian.com/consumption/images/2022/2/e16f30f53d5b8cc859b2c1cac8b05d5ada068a41.jpg",
        "https://zx-cdn.zhangxinhulian.com/consumption/images/2022/2/11156c560299043dab3a63c66311439140d14e40.jpg",
        "https://zx-cdn.zhangxinhulian.com/consumption/images/2022/2/b46b1718f3010f2b14bd6bb545b87c552e92d251.jpg",
        "https://zx-cdn.zhangxinhulian.com/consumption/images/2022/2/bf723e999fede9f539d9a1dafe089cde07c4162a.jpg"
    )

    override fun onClick(v: View?) {
    }

    override fun before() {
        super.before()
        setStatusBarBackground(AppContext.get().resources.getColor(R.color.transparent))
    }

    override fun layoutID(): Int {
        return R.layout.activity_layout_create_room
    }

    override fun init() {
        showFirstDialog()
        mPresenter = CreateRoomPresenter(this)
        // mPresenter?.getTab()
        mPresenter?.toCreateRoom()
        mAdapter = RoomTypeAdapter(this, this)
        val gridLayoutManager: GridLayoutManager = object : GridLayoutManager(mActivity, 3) {
            override fun canScrollVertically(): Boolean {
                return true
            }
        }
        id_rl_room_type.layoutManager = gridLayoutManager
        id_rl_room_type?.adapter = mAdapter
        val user = SettingPreference.getUserInfoData()
        userId = user.uid.toString()
        userName = user.nickname
        userHeadImg = user.headImage
        mTRTCVoiceRoom = TRTCVoiceRoom.sharedInstance(this)
        id_img_back?.setOnClickListener {
            finish()
        }
        id_ll_creat_room?.setOnClickListener(object : NoDoubleClickListener() {
            override fun onNoDoubleClick(v: View?) {
                createRoom()
            }
        })
        id_tv_random?.setOnClickListener(object : NoDoubleClickListener() {
            override fun onNoDoubleClick(v: View?) {
                setTopic()
            }
        })
        id_edit_roo_topic?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                checkIfWrite()
            }

            override fun afterTextChanged(p0: Editable?) {

            }

        })
    }

    private fun setTopic() {
        if (!topicList.isNullOrEmpty()) {
            val index = Random().nextInt(topicList?.size!!)
            topicStr = topicList!!.get(index)
            id_edit_roo_topic?.setText(topicStr)
            checkIfWrite()
        }

    }

    fun checkIfWrite(): Boolean {
        if (!TextUtils.isEmpty(id_edit_roo_topic?.text) && !TextUtils.isEmpty(typeEntity?.name)) {
            id_ll_creat_room?.setBackgroundResource(R.drawable.shape_7a9cf_ae71dd_r25)
            id_tv_text1?.setTextColor(resources.getColor(R.color.white))
            id_tv_nums?.setTextColor(resources.getColor(R.color.white))
            id_tv_text2?.setTextColor(resources.getColor(R.color.white))
            return true
        } else {
            id_ll_creat_room?.setBackgroundResource(R.drawable.shape_efefef_r25)
            id_tv_text1?.setTextColor(resources.getColor(R.color.color_999999))
            id_tv_nums?.setTextColor(resources.getColor(R.color.color_333333))
            id_tv_text2?.setTextColor(resources.getColor(R.color.color_999999))
            return false
        }
    }

    private fun getRoomId(mSelfUserId: String?): Int {
        // 这里我们用简单的 userId hashcode，然后取余
        // 您的room id应该是您后台生成的唯一值
        return (mSelfUserId + "xglt_room").hashCode() and 0x7FFFFFFF
    }

    private fun createRoom() {
        if (remainingTimes > 0 && checkIfWrite()) {
            entity = ChatRoomEntity()
            entity?.ownerId = userId
            // entity?.audioId=Constant.SDKAPPID.toString()
            entity?.category = "1"
            entity?.notice = ""
            entity?.title = id_edit_roo_topic?.text.toString()
            //  mPresenter?.createRoom(entity)
            val index = Random().nextInt(ROOM_COVER_ARRAY.size)
            val coverUrl: String = ROOM_COVER_ARRAY2.get(0)
            val bg: Int = ROOM_COVER_ARRAY.get(index)

            VoiceRoomAnchorActivity.createRoom(
                context, 0,
                getRoomId(userId),
                userName + "的房间",
                userId,
                userName,
                id_edit_roo_topic?.text.toString(),
                coverUrl,
                TRTCCloudDef.TRTC_AUDIO_QUALITY_DEFAULT,
                true
            )
            finish()
        }
    }


    override fun setData(result: ToCreateRoomEntity?) {
        if (result != null) {
            mAdapter?.clear()
            mAdapter?.appendToList(result.category)
            mAdapter?.notifyDataSetChanged()
            topicList = result.title
            remainingTimes = result.remainingTimes
            id_tv_nums?.text = result.remainingTimes.toString()
        }

    }

    override fun createSuc() {

    }


    private var typeEntity: RoomTypeEntity? = null
    override fun onItemClick(view: View?, position: Int, data: RoomTypeEntity?) {
        this.typeEntity = data
        checkIfWrite()
    }

    fun showFirstDialog() {
        CenterDialog.showOwnerNoticeactivity(this)
    }
}