package com.yyrh.core.impl.login.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.yyrh.core.entity.ClickType;
import com.yyrh.core.entity.ResultInfo;
import com.yyrh.core.entity.SmsAction;
import com.yyrh.core.impl.login.LoginActivity;
import com.yyrh.core.internal.IRequestCallback;
import com.yyrh.core.network.SdkRequest;
import com.yyrh.factory.SdkManager;
import com.yyrh.ui.dialog.TipsToast;
import com.yyrh.ui.widget.EventEditText;
import com.yyrh.ui.widget.VerifyEditText;
import com.yyrh.utils.TimeDownUtils;

import cn.yyxx.support.ResUtils;

/**
 * @author #Suyghur.
 * Created on 2021/10/19
 */
public class PhoneRegisterFragment extends Fragment implements View.OnClickListener {

    private View view;
    private LoginActivity loginImpl;
    private EventEditText eetPhone, eetPwd;
    private VerifyEditText vetSms;
    private Button btnRegister;
    private ImageView ivCheck;
    private TextView tvAgreement, tvPrivacy;
    private int imgCheck, imgUncheck, imgShow, imgHide;
    private boolean check = false;
    private boolean showText = false;

    private final Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 101) {
                //倒计时
                changeTimeView((Integer) msg.obj);
            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        if (getActivity() instanceof LoginActivity) {
            loginImpl = (LoginActivity) getActivity();
        }

        if (view == null) {
            view = inflater.inflate(ResUtils.getResId(getActivity(), "hnyy_login_phone_register", "layout"), container, false);
            initView(view);
        }
        return view;
    }

    private void initView(View view) {

        imgCheck = ResUtils.getResId(getActivity(), "hnyy_check_img", "drawable");
        imgUncheck = ResUtils.getResId(getActivity(), "hnyy_uncheck_img", "drawable");
        imgShow = ResUtils.getResId(getActivity(), "hnyy_show_img", "drawable");
        imgHide = ResUtils.getResId(getActivity(), "hnyy_hide_img", "drawable");

        eetPhone = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_eet_phone", "id"));
        eetPhone.getLeftImageView().setBackgroundResource(ResUtils.getResId(getActivity(), "hnyy_phone_img", "drawable"));
        eetPhone.getLeftImageView().setVisibility(View.VISIBLE);
        eetPhone.getEditText().setHint("请输入手机号码");

        vetSms = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_vet_sms", "id"));
        vetSms.getLeftImageView().setBackgroundResource(ResUtils.getResId(getActivity(), "hnyy_sms_img", "drawable"));
        vetSms.getLeftImageView().setVisibility(View.VISIBLE);
        vetSms.getEditText().setHint("请输入验证码");
        vetSms.getRightTextView().setText("获取验证码");
        vetSms.getRightTextView().setVisibility(View.VISIBLE);
        vetSms.getRightTextView().setOnClickListener(this);
        vetSms.getRightTextView().setTag(ClickType.ACTION_GET_CAPTCHA);


        eetPwd = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_eet_pwd", "id"));
        eetPwd.getLeftImageView().setBackgroundResource(ResUtils.getResId(getActivity(), "hnyy_pwd_img", "drawable"));
        eetPwd.getLeftImageView().setVisibility(View.VISIBLE);
        eetPwd.getEditText().setHint("请输入密码");
        eetPwd.getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
        eetPwd.getRightImageView().setBackgroundResource(imgShow);
        eetPwd.getRightImageView().setVisibility(View.VISIBLE);
        eetPwd.getRightImageView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!showText) {
                    showText = true;
                    eetPwd.getRightImageView().setBackgroundResource(imgHide);
                    eetPwd.getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    showText = false;
                    eetPwd.getRightImageView().setBackgroundResource(imgShow);
                    eetPwd.getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
                }
            }
        });

        btnRegister = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_btn_register", "id"));
        btnRegister.setOnClickListener(this);
        btnRegister.setTag(ClickType.ACTION_REGISTER);

        ivCheck = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_iv_check", "id"));
        ivCheck.setBackgroundResource(imgUncheck);
        ivCheck.setOnClickListener(this);
        ivCheck.setTag(ClickType.ACTION_CHECK);


        tvAgreement = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_tv_agreement", "id"));
        tvAgreement.setOnClickListener(this);
        tvAgreement.setTag(ClickType.ACTION_AGREEMENT);

        tvPrivacy = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_tv_privacy", "id"));
        tvPrivacy.setOnClickListener(this);
        tvPrivacy.setTag(ClickType.ACTION_PRIVACY);
    }

    private void changeCheck() {
        if (check) {
            check = false;
            ivCheck.setBackgroundResource(imgUncheck);
        } else {
            check = true;
            ivCheck.setBackgroundResource(imgCheck);
        }
    }

    private void getCaptcha() {

        String phone = eetPhone.getEditText().getText().toString();
        if (TextUtils.isEmpty(phone)) {
            TipsToast.showErrorMsg(getActivity(), "手机号不能为空");
            return;
        }
        if (phone.length() != 11) {
            TipsToast.showErrorMsg(getActivity(), "请填写正确的手机号");
            return;
        }

        SdkRequest.getCaptcha(getActivity(), SmsAction.REGISTER, phone, new IRequestCallback() {
            @Override
            public void onResponse(ResultInfo resultInfo) {
                if (resultInfo.code == 1) {
                    changeTimeNum();
                    TipsToast.showSuccessMsg(getActivity(), resultInfo.msg);
                } else {
                    String msg = resultInfo.msg;
                    if (TextUtils.isEmpty(msg)) {
                        msg = "验证码发送异常，请重试";
                    }
                    TipsToast.showErrorMsg(getActivity(), msg);
                }
            }
        });
    }

    private void phoneRegister() {
        String phone = eetPhone.getEditText().getText().toString();
        String captcha = vetSms.getEditText().getText().toString();
        final String pwd = eetPwd.getEditText().getText().toString();
        if (TextUtils.isEmpty(phone)) {
            TipsToast.showErrorMsg(getActivity(), "手机号不能为空");
            return;
        }
        if (phone.length() != 11) {
            TipsToast.showErrorMsg(getActivity(), "请填写正确的手机号");
            return;
        }
        if (TextUtils.isEmpty(captcha)) {
            TipsToast.showErrorMsg(getActivity(), "请填写有效的验证码");
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            TipsToast.showErrorMsg(getActivity(), "密码不能为空");
            return;
        }
        if (!check) {
            TipsToast.showErrorMsg(getActivity(), "请您先勾选同意下方协议");
            return;
        }
        loginImpl.viewModel.phoneRegister(getActivity(), phone, pwd, captcha);
    }

    private void changeTimeNum() {
        if (!TimeDownUtils.isRunning()) {
            TimeDownUtils.start(59, new TimeDownUtils.TimeCallback() {
                @Override
                public void onTime(int time) {
                    Message msg = new Message();
                    msg.what = 101;
                    msg.obj = time;
                    handler.sendMessage(msg);
                }
            });
        } else {
            TimeDownUtils.resetCallback(new TimeDownUtils.TimeCallback() {
                @Override
                public void onTime(int time) {
                    Message msg = new Message();
                    msg.what = 101;
                    msg.obj = time;
                    handler.sendMessage(msg);
                }
            });
        }
    }

    private void changeTimeView(int time) {
        if (vetSms.getRightTextView() == null) {
            return;
        }
        if (time <= 0) {
            vetSms.getRightTextView().setText("获取验证码");
            vetSms.getRightTextView().setEnabled(true);

        } else {
            vetSms.getRightTextView().setText(time + "s");
            vetSms.getRightTextView().setEnabled(false);
        }
    }

    public void onBack() {
        check = false;
        ivCheck.setBackgroundResource(imgUncheck);

        showText = false;
        eetPwd.getRightImageView().setBackgroundResource(imgShow);

        vetSms.getEditText().setText("");
        eetPhone.getEditText().setText("");
        eetPwd.getEditText().setText("");
    }

    @Override
    public void onClick(View v) {
        if (v == null) {
            return;
        }
        switch ((Integer) v.getTag()) {
            case ClickType.ACTION_CHECK:
                changeCheck();
                break;
            case ClickType.ACTION_GET_CAPTCHA:
                getCaptcha();
                break;
            case ClickType.ACTION_AGREEMENT:
                loginImpl.showAgreementDialog(SdkManager.defaultManager().initBean.agreementPrivacy);
                break;
            case ClickType.ACTION_PRIVACY:
                loginImpl.showAgreementDialog(SdkManager.defaultManager().initBean.agreementUser);
                break;
            case ClickType.ACTION_REGISTER:
                phoneRegister();
                break;
        }
    }
}
