package com.rdiot.yx485.ui.family

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import com.drake.net.utils.TipUtils
import com.drake.net.utils.scopeDialog
import com.rdiot.yx485.R
import com.rdiot.yx485.base.BaseFragment
import com.rdiot.yx485.databinding.FraAddFamilyBinding
import com.rdiot.yx485.net.addFamily
import com.rdiot.yx485.util.setClickAlpha
import com.rdiot.yx485.util.setClickLimitListener
import kotlinx.coroutines.delay

/**
 * 新建家庭
 * @author mR2hao
 * @date 2023/1/13
 */
class AddFamilyFragment : BaseFragment<FraAddFamilyBinding>() {
    override fun getLayoutId() = R.layout.fra_add_family

    override fun initView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ) {
        binding.nb.setLeftClickListener { findNavController().popBackStack() }
        binding.llSetDefault.setClickAlpha {
            binding.isSetDefault = !binding.isSetDefault
        }
        binding.btnConfirm.setClickLimitListener {
            if (binding.etName.text.isNullOrBlank()) {
                TipUtils.toast(getString(R.string.new_build_family_name_hint))
            } else {
                doAddFamily()
            }
        }
    }

    private fun doAddFamily() {
        scopeDialog {
            addFamily(binding.etName.text.toString(), binding.isSetDefault).await()
            delay(500)
            TipUtils.toast(getString(R.string.new_build_family_success))
            findNavController().popBackStack()

        }
    }
}