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

import android.os.Bundle;
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.impl.login.LoginActivity;
import com.yyrh.core.impl.login.LoginFragmentTag;
import com.yyrh.factory.SdkManager;
import com.yyrh.ui.dialog.TipsToast;
import com.yyrh.ui.widget.EventEditText;

import cn.yyxx.support.ResUtils;

/**
 * @author #Suyghur.
 * Created on 2021/10/19
 */
public class QuickRegisterFragment extends Fragment implements View.OnClickListener {
    private View view;
    private LoginActivity loginImpl;
    private EventEditText eetAccount, eetPwd;
    private Button btnRegister, btnPhoneRegister, btnAccountLauncher;

    private ImageView ivCheck;
    private TextView tvAgreement, tvPrivacy;
    private int imgCheck, imgUncheck, imgShow, imgHide;
    private boolean check = false;
    private boolean showText = false;

    @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_quick_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");

        eetAccount = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_eet_account", "id"));
        eetAccount.getLeftImageView().setBackgroundResource(ResUtils.getResId(getActivity(), "hnyy_account_img", "drawable"));
        eetAccount.getLeftImageView().setVisibility(View.VISIBLE);
        eetAccount.getEditText().setHint("请输入账号");

        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);

        btnPhoneRegister = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_btn_phone_register", "id"));
        btnPhoneRegister.setOnClickListener(this);
        btnPhoneRegister.setTag(ClickType.ACTION_PHONE_REGISTER);

        btnAccountLauncher = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_btn_has_account", "id"));
        btnAccountLauncher.setOnClickListener(this);
        btnAccountLauncher.setTag(ClickType.ACTION_RETURN);


        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);
        }
    }

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

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

        eetAccount.getEditText().setText("");
        eetPwd.getEditText().setText("");
    }

    private void accountRegister() {
        final String userName = eetAccount.getEditText().getText().toString();
        final String pwd = eetPwd.getEditText().getText().toString();
        if (TextUtils.isEmpty(userName)) {
            TipsToast.showErrorMsg(getActivity(), "用户名不能为空");
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            TipsToast.showErrorMsg(getActivity(), "密码不能为空");
            return;
        }
        if (!check) {
            TipsToast.showErrorMsg(getActivity(), "请您先勾选同意下方协议");
            return;
        }
        loginImpl.viewModel.accountRegister(getActivity(), userName, pwd);
    }

    @Override
    public void onClick(View v) {
        if (v == null) {
            return;
        }
        switch ((Integer) v.getTag()) {
            case ClickType.ACTION_RETURN:
                loginImpl.onBackPressed();
                break;
            case ClickType.ACTION_PHONE_REGISTER:
                loginImpl.switchFragment(LoginFragmentTag.PHONE_REGISTER);
                break;
            case ClickType.ACTION_CHECK:
                changeCheck();
                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:
                accountRegister();
                break;
        }
    }
}
