package kr.co.vividnext.sodalive.user.find_password

import android.content.Intent
import android.net.Uri
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.common.LoadingDialog
import kr.co.vividnext.sodalive.databinding.ActivityFindPasswordBinding
import org.koin.android.ext.android.inject

class FindPasswordActivity : BaseActivity<ActivityFindPasswordBinding>(
    ActivityFindPasswordBinding::inflate
) {

    private val viewModel: FindPasswordViewModel by inject()

    private lateinit var loadingDialog: LoadingDialog

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

    override fun setupView() {
        loadingDialog = LoadingDialog(this, layoutInflater)
        binding.toolbar.tvBack.text = "비밀번호 재설정"
        binding.toolbar.tvBack.setOnClickListener { finish() }

        binding.tvServiceCenter.setOnClickListener {
            startActivity(
                Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse("http://pf.kakao.com/_lkxgxhG/chat")
                )
            )
        }

        binding.tvFindPassword.setOnClickListener { viewModel.findPassword { finish() } }
    }

    private fun bindData() {
        compositeDisposable.add(
            binding.etEmail.textChanges().skip(1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    viewModel.email = it.toString()
                }
        )

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

        viewModel.isLoading.observe(this) {
            if (it) {
                loadingDialog.show(screenWidth, "")
            } else {
                loadingDialog.dismiss()
            }
        }
    }
}
