package kr.co.vividnext.sodalive.settings.signout

import android.content.Intent
import android.os.Bundle
import android.widget.TextView
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.ActivitySignOutBinding
import kr.co.vividnext.sodalive.splash.SplashActivity
import org.koin.android.ext.android.inject
import java.util.concurrent.TimeUnit

class SignOutActivity : BaseActivity<ActivitySignOutBinding>(ActivitySignOutBinding::inflate) {
    private val viewModel: SignOutViewModel by inject()

    private val reasonButtons = mutableListOf<TextView>()
    private lateinit var loadingDialog: LoadingDialog

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

        bindData()
    }

    private fun bindData() {
        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()
            }
        }

        compositeDisposable.add(
            binding.etPassword.textChanges().skip(1)
                .debounce(500, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    viewModel.password = it.toString()
                }
        )
        compositeDisposable.add(
            binding.etReasonEtc.textChanges().skip(1)
                .debounce(500, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    if (binding.tvReason10.isSelected) {
                        viewModel.reason = it.toString()
                    }
                }
        )
    }

    override fun setupView() {
        reasonButtons.add(binding.tvReason1)
        reasonButtons.add(binding.tvReason2)
        reasonButtons.add(binding.tvReason3)
        reasonButtons.add(binding.tvReason4)
        reasonButtons.add(binding.tvReason5)
        reasonButtons.add(binding.tvReason6)
        reasonButtons.add(binding.tvReason7)
        reasonButtons.add(binding.tvReason8)
        reasonButtons.add(binding.tvReason9)
        reasonButtons.add(binding.tvReason10)

        loadingDialog = LoadingDialog(this, layoutInflater)
        binding.toolbar.tvBack.text = "회원탈퇴"
        binding.toolbar.tvBack.setOnClickListener { finish() }

        for (reasonButton in reasonButtons) {
            val reason = reasonButton.text.toString()
            reasonButton.setOnClickListener {
                checkboxSelectedFalse()
                reasonButton.isSelected = true
                viewModel.reason = if (reason == "기타") {
                    binding.etReasonEtc.text.toString()
                } else {
                    reasonButton.text.toString()
                }
            }
        }

        binding.tvSignOut.setOnClickListener {
            viewModel.signOut {
                finishAffinity()
                startActivity(Intent(applicationContext, SplashActivity::class.java))
            }
        }
    }

    private fun checkboxSelectedFalse() {
        for (reasonButton in reasonButtons) {
            reasonButton.isSelected = false
        }
    }
}
