package com.adealink.weparty.playmate.detail.dialog

import androidx.fragment.app.activityViewModels
import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.view.viewBinding
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.module.playmate.data.PlaymateDetailData
import com.adealink.weparty.playmate.R
import com.adealink.weparty.playmate.data.SkillSwitchData
import com.adealink.weparty.playmate.databinding.DialogPlaymateDetailSettingBinding
import com.adealink.weparty.playmate.detail.viewmodel.PlaymateDetailViewModel
import com.adealink.weparty.playmate.viewmodel.PlaymateViewModel
import com.adealink.weparty.playmate.viewmodel.PlaymateViewModelFactory

class PlaymateDetailSettingDialog : BottomDialogFragment(R.layout.dialog_playmate_detail_setting) {

    private val binding by viewBinding(DialogPlaymateDetailSettingBinding::bind)

    private val detailViewModel by activityViewModels<PlaymateDetailViewModel> { PlaymateViewModelFactory() }
    private val viewModel by activityViewModels<PlaymateViewModel> { PlaymateViewModelFactory() }

    private var data: PlaymateDetailData? = null

    fun setPlaymateData(data: PlaymateDetailData?) {
        this.data = data
    }

    override fun initViews() {
        super.initViews()
        binding.vAcceptSwitch.isEnabled = false
        binding.vAcceptSwitch.checkChangedListener = object : OnCheckChangedListener {
            override fun onChecked(isChecked: Boolean, fromUser: Boolean) {
                if (fromUser) {
                    changeAcceptSwitch(isChecked)
                }
            }
        }
        binding.vMainSwitch.isEnabled = false
        binding.vMainSwitch.checkChangedListener = object : OnCheckChangedListener {
            override fun onChecked(isChecked: Boolean, fromUser: Boolean) {
                if (fromUser) {
                    changeMainSwitch(isChecked)
                }
            }
        }
    }

    override fun observeViewModel() {
        super.observeViewModel()
        detailViewModel.detailLD.observe(viewLifecycleOwner) {
            this.data = it
        }
        viewModel.skillSwitchLD.observe(viewLifecycleOwner) {
            updateUI(it)
        }
    }

    override fun loadData() {
        super.loadData()
        val data = data ?: return
        viewModel.getBusinessOpenData(data.playmateId).observe(viewLifecycleOwner) { rlt ->
            when (rlt) {
                is Rlt.Failed -> {
                    showFailedToast(rlt)
                }

                is Rlt.Success -> {
                    updateUI(rlt.data)
                }
            }
        }
    }

    private fun updateUI(switchData: SkillSwitchData?) {
        binding.vAcceptSwitch.isEnabled = true
        binding.vMainSwitch.isEnabled = true
        binding.vAcceptSwitch.setChecked(switchData?.open ?: false)
        binding.vMainSwitch.setChecked(switchData?.mainSkill ?: false)
    }

    private fun changeAcceptSwitch(open: Boolean) {
        val data = data ?: return
        showLoading()
        viewModel.openBusiness(data.playmateId, open, binding.vMainSwitch.isChecked())
            .observe(viewLifecycleOwner) { rlt ->
                dismissLoading()
                showToast(rlt)
                when (rlt) {
                    is Rlt.Failed -> {
                        binding.vAcceptSwitch.setChecked(!open)
                    }

                    is Rlt.Success -> {
                        //Ntd.
                    }
                }
            }
    }

    private fun changeMainSwitch(open: Boolean) {
        val data = data ?: return
        showLoading()
        viewModel.openBusiness(data.playmateId, binding.vAcceptSwitch.isChecked(), open)
            .observe(viewLifecycleOwner) { rlt ->
                dismissLoading()
                showToast(rlt)
                when (rlt) {
                    is Rlt.Failed -> {
                        binding.vMainSwitch.setChecked(!open)
                    }

                    is Rlt.Success -> {
                        //Ntd.
                    }
                }
            }
    }


}