package cn.yyxx.commsdk.platform.hnyy.fragment;



import static cn.yyxx.commsdk.base.constants.Constants.CLICK_INTERVAL;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Random;

import cn.yyxx.commsdk.base.entity.ResultInfo;
import cn.yyxx.commsdk.base.internal.ICallback;
import cn.yyxx.commsdk.core.utils.ResourceUtil;
import cn.yyxx.commsdk.platform.hnyy.HNYYLoginFragmentTag;
import cn.yyxx.commsdk.platform.hnyy.dialog.WvPageListDialog;
import cn.yyxx.commsdk.platform.hnyy.fragment.widget.TosUtil;
import cn.yyxx.commsdk.platform.hnyy.impl.SdkPlatformImpl;


public class UserRegisterFragment extends BaseFragment {
    private EditText reg_account_edit,reg_pwd_edit;
    private ImageView iv_clear_text,iv_show_pwd;
    private TextView tv_tip,tv_private_policy;
    private Button reg_compete_btn,btn_phone_register,btn_account_login;
    private ImageButton close_btn;
    private CheckBox cb_register;
    private boolean isShowPwd = true;
    private long clickLoginTime = 0L;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(ResourceUtil.getLayoutId(getActivity(),"hnyy_register_dialog"), container, false);
        view.setClickable(true);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        reg_account_edit = view.findViewById(ResourceUtil.getId(getActivity(),"reg_account_edit"));


        reg_pwd_edit= view.findViewById(ResourceUtil.getId(getActivity(),"reg_pwd_edit"));


        reg_account_edit.setText("yyz" + getRandom(7));

        reg_pwd_edit.setText(getRandom(7));


        cb_register= view.findViewById(ResourceUtil.getId(getActivity(),"cb_register"));

        iv_clear_text = view.findViewById(ResourceUtil.getId(getActivity(),"iv_clear_text"));
        iv_clear_text.setOnClickListener(this);

        iv_show_pwd = view.findViewById(ResourceUtil.getId(getActivity(),"iv_show_pwd"));
        iv_show_pwd.setOnClickListener(this);

        tv_tip = view.findViewById(ResourceUtil.getId(getActivity(),"tv_tip"));
        tv_tip.setOnClickListener(this);

        tv_private_policy = view.findViewById(ResourceUtil.getId(getActivity(),"tv_private_policy"));
        tv_private_policy.setOnClickListener(this);

        reg_compete_btn = view.findViewById(ResourceUtil.getId(getActivity(),"reg_compete_btn"));
        reg_compete_btn.setOnClickListener(this);

        btn_phone_register = view.findViewById(ResourceUtil.getId(getActivity(),"btn_phone_register"));
        btn_phone_register.setOnClickListener(this);

        btn_account_login = view.findViewById(ResourceUtil.getId(getActivity(),"btn_account_login"));
        btn_account_login.setOnClickListener(this);


        close_btn = view.findViewById(ResourceUtil.getId(getActivity(),"close_btn"));
        close_btn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        int id = v.getId();
        if (id == ResourceUtil.getId(getActivity(), "reg_compete_btn")){
            if (System.currentTimeMillis() - clickLoginTime < CLICK_INTERVAL) {
                return;
            }
            clickLoginTime = System.currentTimeMillis();
            startRegister(reg_account_edit.getText().toString(), reg_pwd_edit.getText().toString());
        }else if(id == ResourceUtil.getId(getActivity(), "iv_clear_text")){
            reg_account_edit.setText("");
        }else if(id == ResourceUtil.getId(getActivity(), "iv_show_pwd")){

            if (isShowPwd) {

                isShowPwd = false;
                reg_pwd_edit.setTransformationMethod(PasswordTransformationMethod.getInstance());
                iv_show_pwd.setImageResource(ResourceUtil.getMipmapId(getActivity(), "hnyy_close_eye"));


            } else {
                isShowPwd = true;
                reg_pwd_edit.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                iv_show_pwd.setImageResource(ResourceUtil.getMipmapId(getActivity(), "hnyy_open_eye"));
            }

        }else if(id == ResourceUtil.getId(getActivity(), "tv_tip")){


            new WvPageListDialog(getActivity(),ResourceUtil.getStyleId(getActivity(),"hnyy_base_dialog"), SdkPlatformImpl.initConfig.agreement_user).show();


        }else if(id == ResourceUtil.getId(getActivity(), "tv_private_policy")){

            new WvPageListDialog(getActivity(),ResourceUtil.getStyleId(getActivity(),"hnyy_base_dialog"), SdkPlatformImpl.initConfig.agreement_privacy).show();


        }else if(id == ResourceUtil.getId(getActivity(), "btn_phone_register")){

            loginImpl.switchFragment(HNYYLoginFragmentTag.PAGE_PHONE_LOGIN,null);

        }else if(id == ResourceUtil.getId(getActivity(), "btn_account_login")){

            loginImpl.switchFragment(HNYYLoginFragmentTag.PAGE_ACCOUNT_LOGIN,null);

        }else if(id == ResourceUtil.getId(getActivity(), "close_btn")){

                loginImpl.switchFragment(HNYYLoginFragmentTag.PAGE_PHONE_LOGIN,null);

        }
    }





    private void startRegister( String account,  String pwd) {
        if (!cb_register.isChecked()){


            return;
        }
        if (TextUtils.isEmpty(account)){

            new TosUtil(getActivity(),TosUtil.TOAST_ERROR,  getActivity().getString(ResourceUtil.getStringId(getActivity(), "hnyy_band_need_account"))).show();


            return;
        }

        if (TextUtils.isEmpty(pwd)){
            new TosUtil(getActivity(),TosUtil.TOAST_ERROR,  getActivity().getString(ResourceUtil.getStringId(getActivity(), "hnyy_band_need_pwd"))).show();
            return;
        }

        if (isNumeric(account)){
            new TosUtil(getActivity(),TosUtil.TOAST_ERROR,  getActivity().getString(ResourceUtil.getStringId(getActivity(), "hnyy_acount_not_only_number"))).show();
            return;

        }

        if (account.length() < 6 || pwd.length() > 20) {
            new TosUtil(getActivity(),TosUtil.TOAST_ERROR,  getActivity().getString(ResourceUtil.getStringId(getActivity(), "hnyy_regist_account_error"))).show();


            return;
        }

        if (pwd.length() < 6 || pwd.length() > 20) {

            new TosUtil(getActivity(), TosUtil.TOAST_ERROR, getActivity().getString(ResourceUtil.getStringId(getActivity(), "hnyy_regist_pwd_error"))).show();
            return;
        }

        loginImpl.loginViewModel.accountRegister(getActivity(), account, pwd, new ICallback() {
            @Override
            public void onResult(int code, String result) {
                if (code == ResultInfo.CODE_SUCCESS){

                    if (!getActivity().isFinishing()) {
                        getActivity().finish();
                    }

                }else{
                    new TosUtil(getActivity(), TosUtil.TOAST_ERROR, result).show();
                }
            }
        });


    }

      String getRandom(int length) {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            val += String.valueOf(random.nextInt(10));
        }
        return val;
    }

     boolean isNumeric(String str){
        for (int i = str.length();--i>=0;){
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }

}
