package kr.co.vividnext.sodalive.mypage.profile.password

import android.os.Bundle
import android.widget.Toast
import com.jakewharton.rxbinding4.widget.textChanges
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers
import kr.co.vividnext.sodalive.base.BaseActivity
import kr.co.vividnext.sodalive.databinding.ActivityModifyPasswordBinding
import kr.co.vividnext.sodalive.mypage.profile.ProfileUpdateViewModel
import org.koin.android.ext.android.inject
import java.util.concurrent.TimeUnit

class ModifyPasswordActivity : BaseActivity<ActivityModifyPasswordBinding>(
    ActivityModifyPasswordBinding::inflate
) {

    private val viewModel: ProfileUpdateViewModel by inject()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        bindData()
    }

    private fun bindData() {
        compositeDisposable.add(
            binding.etCurrentPassword.textChanges().skip(1)
                .debounce(500, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe {
                    viewModel.currentPassword = it.toString()
                }
        )

        compositeDisposable.add(
            binding.etNewPassword.textChanges().skip(1)
                .debounce(500, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe {
                    viewModel.newPassword = it.toString()
                }
        )

        compositeDisposable.add(
            binding.etNewPasswordConfirm.textChanges().skip(1)
                .debounce(500, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe {
                    viewModel.newPasswordConfirm = it.toString()
                }
        )

        viewModel.toastLiveData.observe(this) {
            it?.let {
                Toast.makeText(applicationContext, it, Toast.LENGTH_LONG).show()
            }
        }
    }

    override fun setupView() {
        binding.toolbar.tvBack.text = "비밀번호 변경"
        binding.toolbar.tvBack.setOnClickListener { finish() }

        binding.tvModifyPassword.setOnClickListener {
            viewModel.updatePassword { finish() }
        }
    }
}
