package com.adealink.weparty.profile.edit.comp

import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.frame.mvvm.viewmodel.activityViewModels
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.widget.wheel.WheelDatePickerDialog
import com.adealink.weparty.module.profile.data.Gender
import com.adealink.weparty.profile.databinding.LayoutEditProfilePersonInfoBinding
import com.adealink.weparty.profile.edit.dialog.EditGenderDialog
import com.adealink.weparty.profile.edit.dialog.EditInterestDialog
import com.adealink.weparty.profile.edit.dialog.EditIntroDialog
import com.adealink.weparty.profile.edit.dialog.EditNickNameDialog
import com.adealink.weparty.profile.edit.viewmodel.EditProfileViewModel
import com.adealink.weparty.profile.viewmodel.ProfileViewModel
import com.adealink.weparty.profile.viewmodel.ProfileViewModelFactory
import com.adealink.weparty.util.formatTimeStr
import com.adealink.weparty.R as APP_R

class EditPersonComp(
    lifecycleOwner: LifecycleOwner,
    private val binging: LayoutEditProfilePersonInfoBinding
) : ViewComponent(lifecycleOwner) {

    companion object {
        private const val SOURCE_TAG = "EditPersonComp"
    }


    private val profileViewModel by activityViewModels<ProfileViewModel> { ProfileViewModelFactory() }
    private val editViewModel by activityViewModels<EditProfileViewModel> { ProfileViewModelFactory() }

    override fun onCreate() {
        super.onCreate()
        initView()
        observeViewModel()
    }

    private fun initView() {
        binging.clNickname.onClick {
            editNickName()
        }
        binging.clGender.onClick {
            editGender()
        }
        binging.clBirthday.onClick {
            editBirthday()
        }
        binging.clIntro.onClick {
            editIntro()
        }
        binging.clInterest.onClick {
            editInterest()
        }
    }

    private fun observeViewModel() {
        profileViewModel.userInfoLD.observe(viewLifecycleOwner) {
            binging.tvNickname.text = it?.nickName
            binging.tvGender.text = Gender.map(it?.gender).getDesc()
            binging.tvBirthday.text = it?.birthday?.formatTimeStr()
            binging.tvIntro.text = it?.intro
            binging.tvInterest.text = it?.interest?.joinToString(separator = ",")
        }
    }

    private fun editNickName() {
        EditNickNameDialog().apply {
            setCallback(object : EditNickNameDialog.IEditNameCallback {
                override fun onEditName(name: String) {

                }
            })
        }.show(fragmentManager, "editNickName")
    }

    private fun editGender() {
        EditGenderDialog().apply {
            setCallback(object : EditGenderDialog.IEditGenderCallback {
                override fun onEditGender(gender: Gender) {

                }
            })
        }.show(fragmentManager, "editGender")
    }

    private fun editBirthday() {
        WheelDatePickerDialog.newInstance(
            confirmButtonText = getCompatString(APP_R.string.common_save)
        ).apply {
            selectBirthdayCallback = object : WheelDatePickerDialog.ISelectDateCallback {
                override fun onSelectComplete(timestamp: Long, timeStr: String) {
                    //viewModel.selectBirthday(timestamp)
                }
            }
        }.show(
            fragmentManager, "editBirthday"
        )
    }

    private fun editIntro() {
        EditIntroDialog().apply {
            setCallback(object : EditIntroDialog.IEditIntroCallback {
                override fun onEditIntro(intro: String?) {
                }
            })
        }.show(fragmentManager, "EditIntro")
    }

    private fun editInterest() {
        EditInterestDialog().apply {

        }.show(fragmentManager, "EditInterest")
    }

}