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.AppManager;
import com.cooleshow.base.common.BaseApplication;
import com.cooleshow.base.common.WebConstants;
import com.cooleshow.base.constanst.LoginStatusConstants;
import com.cooleshow.base.data.net.CommonParamsHelper;
import com.cooleshow.base.event.LoginStatusEvent;
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.ToastUtils;
import com.cooleshow.base.utils.helper.QMUIStatusBarHelper;
import com.cooleshow.usercenter.R;
import com.cooleshow.usercenter.constants.UserConstants;
import com.cooleshow.usercenter.databinding.ActivityVerifyCodeLoginBinding;
import com.cooleshow.usercenter.helper.PhoneCheckHelper;
import com.cooleshow.usercenter.helper.UserHelper;
import com.cooleshow.usercenter.presenter.VerifyLoginPresenter;

import org.greenrobot.eventbus.EventBus;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Author by pq, Date on 2022/4/19.
 */
@Route(path = RouterPath.UserCenter.PATH_VERIFY_LOGIN)
public class VerifyCodeLoginActivity extends BaseMVPActivity<ActivityVerifyCodeLoginBinding, VerifyLoginPresenter> implements View.OnClickListener {
    public static final int LOGIN_STATUS_REQUEST_CODE = 10000002;
    public static final int TEACHER_ITEM = 1;
    public static final int STUDENT_ITEM = 2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        QMUIStatusBarHelper.setStatusBarLightMode(this);
    }

    @NonNull
    @Override
    protected ActivityVerifyCodeLoginBinding getLayoutView() {
        return ActivityVerifyCodeLoginBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        if (BaseApplication.Companion.isTeacherClient()) {
            viewBinding.tvHeaderTitle.setText("您好， \n欢迎使用酷乐秀学院");
        } else {
            viewBinding.tvHeaderTitle.setText("您好， \n欢迎使用酷乐秀");
        }
        viewBinding.tvSendVerifyCode.setOnClickListener(this);
        viewBinding.tvLoginByCode.setOnClickListener(this);
        String phoneNum = getIntent().getStringExtra(UserConstants.PHONE_NUM_KEY);
        if (!TextUtils.isEmpty(phoneNum)) {
            viewBinding.etPhoneNum.setText(phoneNum);
        }
//        if (UserHelper.getAppPrivacyMode() == 1) {
//            //已同意隐私协议及用户协议
//            viewBinding.cbPrivacy.setChecked(true);
//        } else {
//            viewBinding.cbPrivacy.setChecked(false);
//        }
        setPrivacyText();
    }

    @Override
    public void initData() {
        super.initData();
        UserHelper.clearUserInfo();
        CommonParamsHelper.getInstance().clear();
        //登出
        EventBus.getDefault().post(new LoginStatusEvent(LoginStatusConstants.LOGIN_OUT));
    }

    @Override
    protected VerifyLoginPresenter createPresenter() {
        return new VerifyLoginPresenter();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_login_by_code) {
            //密码登录
            String phone = viewBinding.etPhoneNum.getText().toString();
            ARouter.getInstance().build(RouterPath.UserCenter.PATH_LOGIN)
                    .withString(UserConstants.PHONE_NUM_KEY, phone).navigation(this, LOGIN_STATUS_REQUEST_CODE);
        }

        if (v.getId() == R.id.tv_send_verify_code) {
            if (!viewBinding.cbPrivacy.isChecked()) {
                ToastUtil.getInstance().showShort("请勾选用户协议");
                return;
            }
            //获取验证码
            String phone = viewBinding.etPhoneNum.getText().toString();
            if (phone.length() != 11 || !PhoneUtils.isMobile(phone)) {
                ToastUtil.getInstance().showShort("请输入正确手机号");
                return;
            }
            if (PhoneCheckHelper.checkPhoneValidity(phone)) {
                ARouter.getInstance().build(RouterPath.UserCenter.PATH_VERIFY_INPUT)
                        .withString(VerifyCodeInputActivity.TARGET_PHONE, phone)
                        .navigation(this, LOGIN_STATUS_REQUEST_CODE);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (data != null && requestCode == LOGIN_STATUS_REQUEST_CODE) {
                String status = data.getStringExtra(UserConstants.LOGIN_STATUS);
                if (TextUtils.equals(status, UserConstants.LOGIN_STATUS_SUCCESS)) {
                    //密码登录或者验证码登录(设置密码)等两种登录行为,最终通过这里跳转首页
                    ARouter.getInstance().build(RouterPath.APPCenter.PATH_HOME).navigation();
                    finish();
                }
            }
        }
    }

    private void setPrivacyText() {
        viewBinding.tvPrivacy.setMovementMethod(LinkMovementMethod.getInstance());
        SpannableStringUtils.Builder builder = new SpannableStringUtils.Builder();
        viewBinding.tvPrivacy.setHighlightColor(Color.TRANSPARENT);
        builder.append(getString(R.string.login_privacy_tip))
                .setClickSpan(new ClickableSpan() {
                    @Override
                    public void onClick(View view) {
                        startWebActivity(WebConstants.REGISTRATION_AGREEMENT);
                    }

                    public void updateDrawState(@NonNull TextPaint ds) {
                        super.updateDrawState(ds);
                        ds.setColor(getResources().getColor(com.cooleshow.base.R.color.color_2dc7aa));
                        ds.setUnderlineText(false);
                    }
                }).append("和").setForegroundColor(getResources().getColor(com.cooleshow.base.R.color.color_999999))
                .append(getString(R.string.login_privacy_tip2))
                .setClickSpan(new ClickableSpan() {
                    @Override
                    public void onClick(View view) {
                        startWebActivity(WebConstants.PRIVACY_AGREEMENT);
                    }

                    public void updateDrawState(@NonNull TextPaint ds) {
                        super.updateDrawState(ds);
                        ds.setColor(getResources().getColor(com.cooleshow.base.R.color.color_2dc7aa));
                        ds.setUnderlineText(false);
                    }
                });
        viewBinding.tvPrivacy.setText(builder.create());
    }

    @Override
    public void onBackPressed() {
        AppManager.Companion.getInstance().finishAllActivity();
    }

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