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.formatTime
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.module.profile.data.UserInterest
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.ProfileViewModelFactory
import com.adealink.weparty.util.TIME_FORMAT_DMY
import com.adealink.weparty.util.get18YearsAgoTs
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 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() {
        editViewModel.editUserInfoLD.observe(viewLifecycleOwner) { userInfo ->
            binging.tvNickname.text = userInfo?.nickname
            binging.tvGender.text = userInfo?.gender?.getDesc()
            binging.tvBirthday.text = userInfo?.birthday?.let { ts ->
                formatTime(ts, TIME_FORMAT_DMY)
            }
            binging.tvIntro.text = userInfo?.intro
            binging.tvInterest.text =
                userInfo?.interest?.joinToString(separator = ",", transform = { it.name })
        }
    }

    private fun editNickName() {
        EditNickNameDialog().apply {
            setNickName(editViewModel.editUserInfoLD.value?.nickname)
            setCallback(object : EditNickNameDialog.IEditNameCallback {
                override fun onEditName(name: String) {
                    editViewModel.setNickName(name)
                }
            })
        }.show(fragmentManager, "editNickName")
    }

    private fun editGender() {
        EditGenderDialog().apply {
            setGender(editViewModel.editUserInfoLD.value?.gender)
            setCallback(object : EditGenderDialog.IEditGenderCallback {
                override fun onEditGender(gender: Gender) {
                    editViewModel.setGender(gender)
                }
            })
        }.show(fragmentManager, "editGender")
    }

    private fun editBirthday() {
        WheelDatePickerDialog.newInstance(
            initTimestamp = editViewModel.editUserInfoLD.value?.birthday,
            currentTimestamp = get18YearsAgoTs(),
            confirmButtonText = getCompatString(APP_R.string.common_save)
        ).apply {
            selectBirthdayCallback = object : WheelDatePickerDialog.ISelectDateCallback {
                override fun onSelectComplete(timestamp: Long, timeStr: String) {
                    editViewModel.setBirthday(timestamp)
                }
            }
        }.show(
            fragmentManager, "editBirthday"
        )
    }

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

    private fun editInterest() {
        EditInterestDialog().apply {
            setInterests(editViewModel.editUserInfoLD.value?.interest)
            setCallback(object : EditInterestDialog.IEditInterestCallback {
                override fun onEditInterest(interests: List<UserInterest>?) {
                    editViewModel.setInterest(interests)
                }
            })
        }.show(fragmentManager, "EditInterest")
    }

}