package com.sambath.klaklouk_main.screen.login

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import com.sambath.klaklouk_main.R
import com.sambath.klaklouk_main.app.App
import com.sambath.klaklouk_main.app.ShareActivity
import com.sambath.klaklouk_main.config.Config
import com.sambath.klaklouk_main.extension.btnClick
import com.sambath.klaklouk_main.remote.User
import com.sambath.klaklouk_main.util.Const
import com.sambath.klaklouk_main.util.ModelPreferencesManager
import com.sambath.klaklouk_main.util.PrefHelper
import kotlinx.android.synthetic.main.activity_login.*

class LoginActivity : AppCompatActivity() {
    private lateinit var loginViewModel: LoginViewModel
    private var user: User? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        loginViewModel = LoginViewModel(App.injectApiService(), App.injectPrefHelper())
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        val intent: Intent = getIntent()
        if (intent.hasExtra("token")) {
            val token = intent.getStringExtra("token")
            if (token != null) {
                val prefHelper: PrefHelper = App.injectPrefHelper()
                prefHelper.setUserToken(token)
                loginViewModel.getUser(token)
            }
            Toast.makeText(this, R.string.login_success, Toast.LENGTH_LONG).show()
            progressbar_login.visibility = View.VISIBLE
        } else {
            Toast.makeText(this, R.string.login_fail, Toast.LENGTH_LONG).show()
        }

        back_btn.setOnClickListener {
            it.btnClick().subscribe() {
                val launchIntent = packageManager.getLaunchIntentForPackage(Config.MAIN_APP)
                if (launchIntent != null) {
                    startActivity(launchIntent)
                    finish()
                }
            }
        }

        loginViewModel.userState.observe(this, Observer(this::renderUser))

        /////////////////////////////////////////////////////////////////
        user = ModelPreferencesManager.getUser(Const.USER_KEY)
        if (user != null) et_username.setText(user?.userName)
        loginViewModel.state.observe(this, Observer(this::render))
        btn_login.setOnClickListener {
            til_username.isErrorEnabled = false
            til_username.error = null
            til_password.isErrorEnabled = false
            til_password.error = null
            val username = et_username.text
            val password = et_password.text
            if (username.isNullOrEmpty()) {
                til_username.error = getString(R.string.err_not_empty)
                til_username.requestFocus()
                return@setOnClickListener
            }
            if (password.isNullOrEmpty()) {
                til_password.error = getString(R.string.err_not_empty)
                til_password.requestFocus()
                return@setOnClickListener
            }
            loginViewModel.login(username.toString(), password.toString())
        }
    }

    private fun renderUser(state: UserViewState) {
        if (state.isLoginSuccess) {
            val intent = Intent(this, ShareActivity::class.java)
            startActivity(intent)
            finishAffinity()
        }
    }

    private fun render(state: LoginViewState) {
        if (state.isProgress) {
            tv_error.text = ""
        }
        if (state.isLoginSuccess) {
            val intent = Intent(this, ShareActivity::class.java)
            startActivity(intent)
            finishAffinity()
        }
        if (state.error != null) {
            tv_error.text = state.error
        }
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}