package com.adealink.weparty.room.create

import android.annotation.SuppressLint
import androidx.fragment.app.viewModels
import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.mediapicker.imageselect.clip.ClipParamData
import com.adealink.weparty.commonui.mediapicker.imageselect.comp.TakeFromAlbumComp
import com.adealink.weparty.commonui.mediapicker.imageselect.model.MediaType
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.commonui.toast.util.showToast
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.commonui.widget.switchbutton.SwitchButton.OnCheckChangedListener
import com.adealink.weparty.constant.ROOM_NAME_MAX_LENGTH
import com.adealink.weparty.room.R
import com.adealink.weparty.room.create.viewmodel.EditRoomViewModel
import com.adealink.weparty.room.databinding.DialogEditRoomBinding
import com.adealink.weparty.room.sdk.service.roomService

class EditRoomDialog : BottomDialogFragment(R.layout.dialog_edit_room) {
    private val binding by viewBinding(DialogEditRoomBinding::bind)

    private val viewModel by viewModels<EditRoomViewModel>()

    private lateinit var takePhotoAlbumComp: TakeFromAlbumComp

    override fun initViews() {
        super.initViews()
        binding.clRoomCover.onClick {
            takePhotoAlbumComp.takeFromAlbum()
        }
        binding.tvRoomName.onClick {
            showNameEditDialog()
        }
        binding.btnSave.onClick {
            saveRoom()
        }
        binding.vMicMuteSwitch.checkChangedListener = object : OnCheckChangedListener {
            override fun onChecked(isChecked: Boolean, fromUser: Boolean) {
                if (fromUser) {
                    viewModel.autoMuteMic(isChecked)
                }
            }
        }
    }

    override fun loadData() {
        super.loadData()
        viewModel.setRoomInfo(roomService.joinController.joinedRoomInfo)
    }

    override fun initComponents() {
        super.initComponents()
        TakeFromAlbumComp(
            this,
            clipParamData = ClipParamData(0, 1.0, true),
            onSelect = { path, uri ->
                viewModel.changeCover(path, uri)
            },
            mediaType = MediaType.IMAGE,
            source = "SelectRoomCover"
        ).also {
            takePhotoAlbumComp = it
        }.attach()
    }

    @SuppressLint("SetTextI18n")
    override fun observeViewModel() {
        super.observeViewModel()
        viewModel.roomCoverLD.observe(viewLifecycleOwner) { cover ->
            if (cover == null || cover.isNull()) {
                return@observe
            }
            if (cover.isLocal()) {
                binding.ivRoomCover.setImageURI(cover.uri)
            } else {
                binding.ivRoomCover.setImageUrl(cover.url)
            }
        }
        viewModel.roomNameLD.observe(viewLifecycleOwner) { name ->
            binding.tvRoomName.text = name
            binding.tvRoomNameCount.text = "${name?.length ?: 0}/${ROOM_NAME_MAX_LENGTH}"
        }
        viewModel.autoMuteMicLD.observe(viewLifecycleOwner) { autoMuteMic ->
            binding.vMicMuteSwitch.setChecked(autoMuteMic)
        }
    }

    private fun showNameEditDialog() {
        EditRoomNameDialog().apply {
            setRoomName(binding.tvRoomName.text?.toString() ?: "")
            setCallback(object : EditRoomNameDialog.IEditNameCallback {
                override fun onEditName(name: String) {
                    viewModel.updateRoomName(name)
                }
            })
        }.show(childFragmentManager, "showNameEditDialog")
    }

    private fun saveRoom() {
        val cover = viewModel.roomCoverLD.value
        if (cover == null || cover.isNull()) {
            showToast(R.string.room_cover_emtpy)
            return
        }
        val roomName = viewModel.roomNameLD.value
        if (roomName.isNullOrEmpty()) {
            showToast(R.string.room_edit_room_name_empty)
            return
        }
        showLoading()
        viewModel.updateRoom().observe(viewLifecycleOwner) { rlt ->
            dismissLoading()
            when (rlt) {
                is Rlt.Failed -> {
                    showFailedToast(rlt)
                }

                is Rlt.Success -> {
                    dismiss()
                }
            }
        }
    }
}