package com.yyrh.ui.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
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 com.yyrh.constant.SDKSettings;
import com.yyrh.networking.NetRequestApi;
import com.yyrh.sdk.SDKEntry;
import com.yyrh.sdk.YyrhSdkManager;
import com.yyrh.ui.dialog.WvPageListDialog;
import com.yyrh.utils.ResourceUtil;
import com.yyrh.utils.SignUtils;
import com.yyrh.utils.TosUtil;
import com.yyrh.utils.Utils;
import com.yyrh.utils.YYLog;

import static com.yyrh.constant.Constants.CLICK_INTERVAL;
import static com.yyrh.constant.URLConstants.RSAKEY;


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"),SDKSettings.initConfig.agreement_user).show();

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

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

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

            if (System.currentTimeMillis() - clickLoginTime < CLICK_INTERVAL) {
                YYLog.i("调用登录接口太频繁，拦截此次操作");
                return;
            }
            clickLoginTime = System.currentTimeMillis();

            registerWithPhone(getActivity());

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

            Fragment accountLoginFragment = FragmentUtils.getSdkFragment(getActivity(),FragmentUtils.SDK_PHONE_REGISTER_TAG);
            replaceFragmentToActivity(getFragmentManager(),accountLoginFragment,ResourceUtil.getId(getActivity(),"hnyy_content"));


        }



    }



    private void registerWithPhone(final Activity activity){

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

        String userInfo ="";

        if(account.length() < 10 || Utils.judgeStrNull(account))
        {
            new TosUtil(getActivity(),TosUtil.TOAST_ERROR,getString(ResourceUtil.getStringId(getActivity(), "qj_pls_input_right_phone"))).show();


            return;
        }
        if(pwd.length() < 6 || Utils.judgeStrNull(pwd))
        {

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



            return;
        }
        if(code.length() < 4 || Utils.judgeStrNull(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;

        }

        try {
            userInfo =  SignUtils.encryptRsaByPublicKey(RSAKEY,account+"|"+ pwd);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        NetRequestApi.registerWithPhone(activity,code,userInfo, new PhoneRegisterCallback() {
            @Override
            public void onRegisterSuccess(String msg) {



                YyrhSdkManager.defaultManager().saveUserLoginInfo(SDKEntry.getSdkInstance().gameAct,account,pwd,SDKSettings.loginData.login_token,msg);

                SDKEntry.getSdkInstance().adSdkCallback.onRegisterSuccess(account);

                getActivity().finish();

            }

            @Override
            public void onRegisterFailed(String msg) {
                new TosUtil(getActivity(),TosUtil.TOAST_ERROR, msg).show();
            }
        });
    }






    public interface PhoneRegisterCallback {

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


    }



}
