package com.renrenliao.im.ui.activity

import com.gyf.immersionbar.ktx.immersionBar
import com.gyf.immersionbar.ktx.statusBarHeight
import com.renrenliao.im.R
import com.renrenliao.im.base.BaseVMActivity
import com.renrenliao.im.config.Const
import com.renrenliao.im.ext.click
import com.renrenliao.im.ext.gone
import com.renrenliao.im.utils.DataCacheUtils
import com.renrenliao.im.utils.MapUtils
import com.renrenliao.im.utils.RouteUtils
import com.renrenliao.im.viewmodel.LoginViewModel
import com.renrenliao.im.widget.dialog.LinesDialog
import kotlinx.android.synthetic.main.activity_login.*
import org.koin.androidx.viewmodel.ext.android.getViewModel


class LoginActivity : BaseVMActivity<LoginViewModel>() {

    override fun getLayoutResId(): Int = R.layout.activity_login

    override fun initVM(): LoginViewModel = getViewModel()

    override fun initView() {
        root_login.setPadding(0, statusBarHeight, 0, 0)
        immersionBar {
            statusBarDarkFont(true)
            navigationBarColor(R.color.white)
        }
        tv_appname.text = getString(R.string.app_name)
        if (Const.useMobileRegister==0){
            change_login_way.gone()
            forget_password_btn.gone()
            tv_prefix.gone()
            mLinCode.gone()
            mTvAccountPhone.text = "账号"
            phone_numer_edit.hint = "请输入账号"
        }else{
            mTvAccountPhone.text = "手机"
            phone_numer_edit.hint = "请输入电话号码"
        }

    }

    override fun initData() {

        login_btn.click {
            login()
        }
        mTvRouter.click {
            mViewModel.getRouter()
        }
    }




    private fun  login(){
        val userName = phone_numer_edit.text.toString().trim()
        val password = password_edit.text.toString().trim()

        if (userName.isBlank()){
            phone_numer_edit.error = "用户名不能为空!"
            return
        }
        if (password.isBlank()){
            password_edit.error = "密码必须填写完整"
            return
        }

        var map = HashMap<String,String>()
        map["userName"] = userName
        map["password"] = password
        map["loginType"] = "1"
        map["umDeviceToken"] =""//DataCacheUtils.getToken()

        map = MapUtils.getComParam(map)

        mViewModel.login(map)

    }

    override fun startObserve() {

        mViewModel.run {
            loginLiveData.observe(this@LoginActivity) {
                if (it) {
                    RouteUtils.gotoMain(this@LoginActivity)
                    finish()
                }
            }
            lineLiveData.observe(this@LoginActivity){
                val lineDialog = LinesDialog(this@LoginActivity,it)
                lineDialog.show()
            }
        }
    }
}