package com.rdiot.yx485.ui.ctrl

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.widget.AppCompatEditText
import androidx.core.widget.doAfterTextChanged
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.drake.net.utils.scopeDialog
import com.lindroy.iosdialog.IAlertDialog
import com.rdiot.yx485.R
import com.rdiot.yx485.base.BaseFragment
import com.rdiot.yx485.base.RoomCtrlViewModelFactory
import com.rdiot.yx485.bean.RoomData
import com.rdiot.yx485.databinding.FraRoomSettingBinding
import com.rdiot.yx485.net.updateRoomInfo
import com.rdiot.yx485.ui.ctrl.model.RoomCtrlViewModel
import com.rdiot.yx485.util.setClickLimitListener
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 房间更多设置
 * @author mR2hao
 * @date 2023/2/2
 */
class RoomSettingFragment : BaseFragment<FraRoomSettingBinding>() {
    override fun getLayoutId() = R.layout.fra_room_setting
    private val viewModel: RoomCtrlViewModel by activityViewModels { RoomCtrlViewModelFactory() }

    override fun initView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ) {
        binding.nb.setLeftClickListener {
            findNavController().popBackStack()
        }

        binding.llChangeName.setClickLimitListener { showEditNameDialog()}

        viewModel.roomData.observe(this) {
            binding.roomData = it
        }
    }


    private fun showEditNameDialog() {
        val data = viewModel.roomData.value ?: return
        val text = data.name
        var inputText = ""
        IAlertDialog
            .build(this)
            .setTitle(R.string.set_room_name)
            .setContentView(R.layout.dialog_simple_edit)
            .setCancelOutside(false)
            .setNegButtonText(R.string.cancel)
            .setNegButtonTextColor(resources.getColor(R.color.main_text_color, null))
            .setNegClickListener { dialogInterface ->
                dialogInterface.dismiss()
            }
            .setPosClickListener { dialogInterface ->
                dialogInterface.dismiss()
                if (inputText.isNotBlank()) {
                    data.name = inputText
                    doUpdateRoomInfo(data)
                }
            }
            .setDismissible(false)
            .setAlpha(1.0F)
            .setPosButtonTextColor(resources.getColor(R.color.btn_blue, null))
            .setOnViewHandler { holder, _ ->
                lifecycleScope.launch {
                    (holder.getView(R.id.et) as AppCompatEditText).apply {
                        setText(text)
                        hint = getString(R.string.room_name)
                        //延时弹出键盘
                        delay(300)
                        setSelection(text.length)
                        requestFocus()
                        val inputManager = act.getSystemService(
                            Context.INPUT_METHOD_SERVICE
                        ) as InputMethodManager
                        inputManager.showSoftInput(this, 0)

                        doAfterTextChanged {
                            inputText = it.toString()
                        }
                    }

                }
            }.show()
    }


    private fun doUpdateRoomInfo(roomData: RoomData) {
        scopeDialog {
            updateRoomInfo(roomData).await()
            viewModel.roomData.postValue(roomData)
        }
    }

}