package com.bc_chat.account

import android.app.Activity
import android.text.TextUtils
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bc_chat.account.contract.ModifyPwdContract
import com.bc_chat.account.databinding.ActivityModifyPasswordBinding
import com.bc_chat.account.presenter.ModifyPresenter
import com.bc_chat.bc_base.ImService.ImRongService
import com.bc_chat.bc_base.config.RouteConfig
import com.zhaohaoting.framework.abs.BaseActivity
import com.zhaohaoting.framework.ui.dialog.CustomDialog
import com.zhaohaoting.framework.utils.ToastUtils
import java.lang.Exception

/**
 * author：Created by zhaohaoting on 2019/2/22
 * email：526309416@qq.com
 * desc：修改密码
 */
@Route(path = RouteConfig.MODIFY_PASSWORD_ACTIVITY)
class ModifyPasswordActivity : BaseActivity<ModifyPresenter>(), View.OnClickListener, ModifyPwdContract.View {

    private val binding by lazy {
        rootDataBinding as ActivityModifyPasswordBinding
    }

    override fun initialize() {
        binding.onClickEvent = this
    }

    override fun showLoading(vararg task: Any?) {
        super.showLoading(task)
        binding.btnConfirm.isEnabled = false
    }
    override fun modifyPadSuccess() {
        binding.btnConfirm.isEnabled = true
        CustomDialog.Builder(this)
                .setTitle("密码修改成功")
                .setMessage("请重新登录")
                .setNegativeButton("以后再说") { dialog, _ ->
                    setResult(Activity.RESULT_OK)
                    finish()
                }
                .setPositiveButton("确定") { dialog, _ ->
                    ARouter.getInstance().navigation(ImRongService::class.java)?.loginOut(this)
                }
                .create().show()
    }

    override fun modifyPadFailure(exception: Exception?) {
        binding.btnConfirm.isEnabled = true
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btn_confirm -> {
                val etOriginalPassword = binding.etOriginalPassword.text.toString().trim()
                val etPassword = binding.etPassword.text.toString().trim()
                val etConfirmPassword = binding.etConfirmPassword.text.toString().trim()

                if (TextUtils.isEmpty(etOriginalPassword)|| etOriginalPassword.length < 6) {
                    ToastUtils.show("原密码不能少于六位")
                    return
                }
                if (TextUtils.isEmpty(etPassword)|| etPassword.length < 6) {
                    ToastUtils.show("新密码不能少于六位")
                    return
                }
                if (TextUtils.isEmpty(etConfirmPassword)|| etConfirmPassword.length < 6) {
                    ToastUtils.show("确认密码不能少于六位")
                    return
                }
                if (!etConfirmPassword.equals(etPassword)) {
                    ToastUtils.show("两次输入的密码不相同")
                    return
                }
                if (etPassword.equals(etOriginalPassword)) {
                    ToastUtils.show("新密码不能和旧密码相同")
                    return
                }
                presenter.modifyPwd(etOriginalPassword, etPassword)
            }
        }
    }

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

    override fun initPresenter(): ModifyPresenter? {
        return ModifyPresenter(this)
    }
}