package com.cooleshow.usercenter.ui.activity

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.text.TextPaint
import android.text.TextUtils
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.cooleshow.base.common.BaseApplication.Companion.isTeacherClient
import com.cooleshow.base.common.WebConstants
import com.cooleshow.base.ext.onClick
import com.cooleshow.base.router.RouterPath
import com.cooleshow.base.ui.activity.BaseMVPActivity
import com.cooleshow.base.utils.PhoneUtils
import com.cooleshow.base.utils.SpannableStringUtils
import com.cooleshow.base.utils.ToastUtil
import com.cooleshow.base.utils.helper.QMUIStatusBarHelper
import com.cooleshow.usercenter.R
import com.cooleshow.usercenter.bean.UserLoginInfo
import com.cooleshow.usercenter.constants.UserConstants
import com.cooleshow.usercenter.databinding.ActivityLoginBinding
import com.cooleshow.usercenter.helper.UserHelper
import com.cooleshow.usercenter.presenter.LoginPresenter
import com.cooleshow.usercenter.presenter.contract.LoginContract
import kotlinx.android.synthetic.main.activity_login.*

@Route(path = RouterPath.UserCenter.PATH_LOGIN)
class LoginActivity : BaseMVPActivity<ActivityLoginBinding, LoginPresenter>(),
    LoginContract.LoginView {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        QMUIStatusBarHelper.setStatusBarLightMode(this);
    }

    override fun getLayoutView(): ActivityLoginBinding {
        return ActivityLoginBinding.inflate(layoutInflater)
    }

    override fun createPresenter(): LoginPresenter {
        return LoginPresenter()
    }

    override fun initView() {
        tv_login_by_code.onClick {
            finish()
        }

        var phone: String? = intent.getStringExtra(UserConstants.PHONE_NUM_KEY);
        if (!TextUtils.isEmpty(phone)) {
            viewBinding?.etPhoneNum?.setText(phone);
        }
        if (isTeacherClient) {
            viewBinding.tvHeaderTitle.text = "您好， \n欢迎使用酷乐秀学院"
        } else {
            viewBinding.tvHeaderTitle.text = "您好， \n欢迎使用酷乐秀"
        }
//        viewBinding.cbPrivacy.isChecked = UserHelper.getAppPrivacyMode() == 1

        setPrivacyText()
        mLoginBtn.setOnClickListener {
            if (TextUtils.isEmpty(et_phone_num?.text)) {
                ToastUtil.getInstance().showShort("手机号不可为空")
                return@setOnClickListener;
            }

            if (et_phone_num?.text?.length != 11 || !PhoneUtils.isMobile(et_phone_num?.text.toString())) {
                ToastUtil.getInstance().showShort("请输入正确手机号")
                return@setOnClickListener;
            }
            if (TextUtils.isEmpty(et_password?.text)) {
                ToastUtil.getInstance().showShort("密码不可为空")
                return@setOnClickListener;
            }
            if (!viewBinding.cbPrivacy.isChecked) {
                ToastUtil.getInstance().showShort("请勾选用户协议")
                return@setOnClickListener
            }
            presenter?.onLoginByPwd(et_phone_num?.text.toString(), et_password?.text.toString());
        }
    }

    /**
     * 登录成功
     */
    override fun onLoginResult(result: UserLoginInfo) {
        if (isFinishing || isDestroyed) {
            return;
        }
        UserHelper.handleLoginSuccessAction(result)
        setLoginResult()
    }

    private fun setLoginResult() {
        val intent = Intent()
        intent.putExtra(UserConstants.LOGIN_STATUS, UserConstants.LOGIN_STATUS_SUCCESS)
        setResult(RESULT_OK, intent)
        finish()
    }

    private fun setPrivacyText() {
        viewBinding?.tvPrivacy?.movementMethod = LinkMovementMethod.getInstance()
        val builder = SpannableStringUtils.Builder()
        viewBinding?.tvPrivacy?.highlightColor = Color.TRANSPARENT
        builder.append(getString(R.string.login_privacy_tip))
            .setClickSpan(object : ClickableSpan() {
                override fun onClick(view: View) {
                    startWebActivity(WebConstants.REGISTRATION_AGREEMENT);
                }

                override fun updateDrawState(ds: TextPaint) {
                    super.updateDrawState(ds)
                    ds.color = resources.getColor(com.cooleshow.base.R.color.color_2dc7aa)
                    ds.isUnderlineText = false
                }
            }).append("和")
            .setForegroundColor(resources.getColor(com.cooleshow.base.R.color.color_999999))
            .append(getString(R.string.login_privacy_tip2))
            .setClickSpan(object : ClickableSpan() {
                override fun onClick(view: View) {
                    startWebActivity(WebConstants.PRIVACY_AGREEMENT);
                }

                override fun updateDrawState(ds: TextPaint) {
                    super.updateDrawState(ds)
                    ds.color = resources.getColor(com.cooleshow.base.R.color.color_2dc7aa)
                    ds.isUnderlineText = false
                }
            })
        viewBinding?.tvPrivacy?.text = builder.create()
    }

    private fun startWebActivity(agreement: String?) {
        //跳转隐私协议
        ARouter.getInstance()
            .build(RouterPath.WebCenter.ACTIVITY_HTML)
            .withString(WebConstants.WEB_URL, agreement)
            .navigation()
    }
}