package com.adealink.weparty.profile.edit

import androidx.activity.viewModels
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.router.annotation.BindExtra
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.frame.util.naviBarHeight
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.commonui.toast.util.showToast
import com.adealink.weparty.commonui.widget.CommonDialog
import com.adealink.weparty.module.profile.Profile
import com.adealink.weparty.profile.R
import com.adealink.weparty.profile.databinding.ActivityEditProfileBinding
import com.adealink.weparty.profile.edit.comp.EditAvatarComp
import com.adealink.weparty.profile.edit.comp.EditPersonComp
import com.adealink.weparty.profile.edit.comp.EditPhotoWallComp
import com.adealink.weparty.profile.edit.comp.EditTalentComp
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.applyImmersive
import com.adealink.weparty.R as APP_R

@RouterUri(
    path = [Profile.EditProfile.PATH],
    desc = "编辑个人页"
)
class EditProfileActivity : BaseActivity() {

    @BindExtra(name = Profile.Common.EXTRA_UID)
    var userUid: String = ""

    private val binding by viewBinding(ActivityEditProfileBinding::inflate)
    private val profileViewModel by viewModels<ProfileViewModel> { ProfileViewModelFactory() }
    private val editViewModel by viewModels<EditProfileViewModel> { ProfileViewModelFactory() }

    override fun onBeforeCreate() {
        super.onBeforeCreate()
        Router.bind(this)
    }

    override fun initViews() {
        super.initViews()
        setContentView(binding.root)
        applyImmersive { top ->
            binding.topBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
                topMargin = top
            }
        }
        binding.clContent.setPadding(
            16.dp(), 0, 16.dp(), naviBarHeight() + 24.dp()
        )
        binding.ivClose.onClick {
            clickClose()
        }
        binding.btnSave.onClick {
            saveEdit()
        }
    }

    override fun initComponents() {
        super.initComponents()
        EditAvatarComp(this, binding.vAvatar).attach()
        EditPhotoWallComp(this, binding.vPhotoWall).attach()
        EditPersonComp(this, binding.vPersonInfo).attach()
        EditTalentComp(this, binding.vTalent).attach()
    }

    override fun observeViewModel() {
        super.observeViewModel()
        profileViewModel.myUserInfoLD.observe(this) {
            editViewModel.setUserInfo(it?.userInfo)
        }
        editViewModel.editUserInfoLD.observe(this) { edit ->
            binding.btnSave.isEnabled = edit.isEdited
        }
    }

    override fun loadData() {
        super.loadData()
        profileViewModel.pullMyUserInfo()
    }

    private fun clickClose() {
        val editUserInfo = editViewModel.getEditUserInfo()
        if (editUserInfo.isEdited) {
            CommonDialog.Builder()
                .message(getCompatString(R.string.profile_edit_not_save_tips))
                .positiveText(getCompatString(APP_R.string.commonui_confirm))
                .setShowDefaultCancel(true)
                .onPositive {
                    finish()
                }
                .show(supportFragmentManager, "CloseConfirm")
            return
        }
        finish()
    }

    private fun saveEdit() {
        val editUserInfo = editViewModel.getEditUserInfo()
        if (!editUserInfo.isEdited) {
            finish()
            return
        }
        showLoading()
        profileViewModel.updateProfile(editUserInfo).observe(this) { rlt ->
            dismissLoading()
            when (rlt) {
                is Rlt.Failed -> {
                    showFailedToast(rlt)
                }

                is Rlt.Success<*> -> {
                    showToast(getCompatString(APP_R.string.common_success))
                    this@EditProfileActivity.finish()
                }
            }
        }
    }

}