package kr.co.vividnext.sodalive.user.login

import android.content.Intent
import android.os.Bundle
import android.text.InputType
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.Constants
import kr.co.vividnext.sodalive.common.LoadingDialog
import kr.co.vividnext.sodalive.databinding.ActivityLoginBinding
import kr.co.vividnext.sodalive.main.MainActivity
import kr.co.vividnext.sodalive.user.find_password.FindPasswordActivity
import kr.co.vividnext.sodalive.user.signup.SignUpActivity
import org.koin.android.ext.android.inject

class LoginActivity : BaseActivity<ActivityLoginBinding>(ActivityLoginBinding::inflate) {

    private val viewModel: LoginViewModel by inject()

    private lateinit var loadingDialog: LoadingDialog

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

        bindData()
    }

    override fun setupView() {
        binding.tvToolbar.text = "로그인"
        loadingDialog = LoadingDialog(this, layoutInflater)

        binding.tvLogin.setOnClickListener {
            viewModel.login {
                it?.let { Toast.makeText(applicationContext, it, Toast.LENGTH_LONG).show() }
                finishAffinity()
                val nextIntent = Intent(applicationContext, MainActivity::class.java)
                val extras = intent.getBundleExtra(Constants.EXTRA_DATA)
                    ?: if (intent.extras != null) {
                        intent.extras
                    } else {
                        null
                    }
                if (extras != null) {
                    nextIntent.putExtra(Constants.EXTRA_DATA, extras)
                }
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
                startActivity(nextIntent)
            }
        }

        binding.tvSignUp.setOnClickListener {
            val nextIntent = Intent(applicationContext, SignUpActivity::class.java)
            val extras = intent.getBundleExtra(Constants.EXTRA_DATA)
                ?: if (intent.extras != null) {
                    intent.extras
                } else {
                    null
                }
            if (extras != null) {
                nextIntent.putExtra(Constants.EXTRA_DATA, extras)
            }
            startActivity(nextIntent)
        }

        binding.tvForgotPassword.setOnClickListener {
            startActivity(
                Intent(
                    applicationContext,
                    FindPasswordActivity::class.java
                )
            )
        }

        binding.tvVisiblePassword.setOnClickListener { viewModel.onClickVisiblePassword() }
    }

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

        compositeDisposable.add(
            binding.etPassword.textChanges().skip(1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    viewModel.password = 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()
            }
        }

        viewModel.visiblePasswordLiveData.observe(this) {
            binding.tvVisiblePassword.isSelected = it
            if (it) {
                binding.etPassword.inputType =
                    InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
            } else {
                binding.etPassword.inputType =
                    InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD
            }
        }
    }
}
