package com.adealink.weparty.account.register.dialog

import androidx.fragment.app.activityViewModels
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.onClick
import com.adealink.weparty.account.R
import com.adealink.weparty.account.databinding.DialogRegisterModifyAvatarBinding
import com.adealink.weparty.account.register.viewmodel.RegisterProfileViewModel
import com.adealink.weparty.account.viewModel.AccountViewModelFactory
import com.adealink.weparty.commonui.mediapicker.imageselect.clip.avatarClipParam
import com.adealink.weparty.commonui.mediapicker.imageselect.comp.TakeFromAlbumComp
import com.adealink.weparty.commonui.mediapicker.imageselect.comp.TakePhotoComp
import com.adealink.weparty.commonui.widget.BottomDialogFragment

class ModifyAvatarDialog : BottomDialogFragment(R.layout.dialog_register_modify_avatar) {
    private val binding by viewBinding(DialogRegisterModifyAvatarBinding::bind)

    private val viewModel by activityViewModels<RegisterProfileViewModel> { AccountViewModelFactory() }
    private lateinit var takePhotoComp: TakePhotoComp
    private lateinit var takeFromAlbumComp: TakeFromAlbumComp
    override fun initViews() {
        super.initViews()
        binding.tvTakePhoto.onClick {
            takePhoto()
        }
        binding.tvFromAlbum.onClick {
            takeFromAlbum()
        }
        binding.tvUseDefaultAvatar.onClick {
            useDefaultAvatar()
        }
    }

    override fun initComponents() {
        super.initComponents()
        TakePhotoComp(
            this,
            clipParamData = avatarClipParam,
            onPhotoSelect = { path, uri ->
                viewModel.selectAvatarPath(path, uri)
                dismiss()
            },
            source = "ModifyAvatarDialog"
        ).also {
            takePhotoComp = it
        }.attach()
        TakeFromAlbumComp(
            this,
            clipParamData = avatarClipParam,
            onSelect = { path, uri ->
                viewModel.selectAvatarPath(path, uri)
                dismiss()
            },
            source = "ModifyAvatarDialog"
        ).also {
            takeFromAlbumComp = it
        }.attach()
    }

    private fun takePhoto() {
        takePhotoComp.takePhoto()
    }

    private fun takeFromAlbum() {
        takeFromAlbumComp.takeFromAlbum()
    }

    private fun useDefaultAvatar() {
        viewModel.setRandomAvatarUrl(viewModel.getNextRandomAvatar())
        dismiss()
    }


}