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


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

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
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.TextView;

import cn.yyxx.commsdk.base.entity.ResultInfo;
import cn.yyxx.commsdk.base.internal.ICallback;
import cn.yyxx.commsdk.base.utils.Logger;
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 UserPhoneLoginSetpwdFragment extends BaseFragment {


    private EditText reg_pwd_edit;
    private CheckBox cb_register;
    private TextView tv_tip,tv_private_policy;
    private Button reg_compete_btn;
    private ImageButton close_btn;
    private long clickLoginTime = 0L;

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

        Bundle bundle = getArguments();
        account = bundle.getString("account");
        code = bundle.getString("code");
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        reg_pwd_edit = view.findViewById(ResourceUtil.getId(getActivity(),"reg_pwd_edit"));

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

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


        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(), "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(), "reg_compete_btn")){

            if (System.currentTimeMillis() - clickLoginTime < CLICK_INTERVAL) {

                Logger.i("调用登录接口太频繁，拦截此次操作");

                return;
            }
            clickLoginTime = System.currentTimeMillis();

            registerWithPhone(getActivity());

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

            loginImpl.switchFragment(HNYYLoginFragmentTag.PAGE_PHONE_LOGIN,null);

        }



    }



    private void registerWithPhone(final Activity activity){

        final String pwd = reg_pwd_edit.getText().toString();


        if(account.length() < 10 || TextUtils.isEmpty(account))
        {

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

            return;
        }
        if(pwd.length() < 6 || TextUtils.isEmpty(pwd))
        {

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

            return;
        }
        if(code.length() < 4 || TextUtils.isEmpty(code))
        {
            new TosUtil(getActivity(),TosUtil.TOAST_ERROR, getString(ResourceUtil.getStringId(getActivity(), "qj_pls_input_right_code"))).show();

            return;
        }

        if (!cb_register.isChecked()){

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


            return;

        }

        loginImpl.loginViewModel.setPassword(activity, account, pwd, code, 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();

                }

            }
        });

    }






    public interface PhoneRegisterCallback {

        void onRegisterSuccess(String msg);
        void onRegisterFailed(String msg);


    }



}
