package com.yyrh.ui.fragment;


import android.app.Fragment;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
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 com.yyrh.constant.SDKSettings;
import com.yyrh.constant.URLConstants;
import com.yyrh.networking.NetRequestApi;
import com.yyrh.sdk.SDKEntry;
import com.yyrh.sdk.YyrhSdkManager;
import com.yyrh.sdk.callback.RegisterCallback;

import com.yyrh.ui.dialog.WvPageListDialog;

import com.yyrh.utils.ResourceUtil;
import com.yyrh.utils.ScreenShotUtil;
import com.yyrh.utils.TosUtil;
import com.yyrh.utils.Utils;
import com.yyrh.utils.YYLog;

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


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_account_login,btn_guest_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("yyn" + Utils.getRandom(7));
        reg_pwd_edit.setText(Utils.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_account_login = view.findViewById(ResourceUtil.getId(getActivity(),"btn_account_login"));
        btn_account_login.setOnClickListener(this);

        btn_guest_login = view.findViewById(ResourceUtil.getId(getActivity(),"btn_guest_login"));
        btn_guest_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) {
                YYLog.i("调用登录接口太频繁，拦截此次操作");
                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"),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(), "btn_account_login")){

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

        }else if(id == ResourceUtil.getId(getActivity(), "btn_guest_login")){
            Fragment accountLoginFragment = FragmentUtils.getSdkFragment(getActivity(),FragmentUtils.SDK_LOGIN_TAG);
            replaceFragmentToActivity(getFragmentManager(),accountLoginFragment,ResourceUtil.getId(getActivity(),"hnyy_content"));
        }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 startRegister(final String account, final String pwd) {
        if (!cb_register.isChecked()){

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

            return;
        }
        if (Utils.judgeStrNull(account)){
            new TosUtil(getActivity(),TosUtil.TOAST_ERROR,  getActivity().getString(ResourceUtil.getStringId(getActivity(), "hnyy_band_need_account"))).show();



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

            return;
        }
        if (Utils.judgeStrNull(pwd)){
            new TosUtil(getActivity(),TosUtil.TOAST_ERROR,  getActivity().getString(ResourceUtil.getStringId(getActivity(), "hnyy_band_need_pwd"))).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;
        }



        NetRequestApi.registerWithYy( getActivity(), account, pwd, new RegisterCallback() {
            @Override
            public void onRegisterSuccess(String token) {


                if (ScreenShotUtil.shotByView(getActivity(),getView())){

                    new TosUtil(getActivity(),TosUtil.TOAST_GREEN,  getString(ResourceUtil.getStringId(getActivity(), "hnyy_register_screen_tip"))).show();

                }

                SDKSettings.isVefityRealName = false;
                YyrhSdkManager.defaultManager().saveUserLoginInfo(SDKEntry.getSdkInstance().gameAct,account,pwd,SDKSettings.loginData.login_token,token);
                SDKEntry.getSdkInstance().adSdkCallback.onRegisterSuccess(account);

                getActivity().finish();
            }

            @Override
            public void onRegisterFailed(String msg) {

                new TosUtil(getActivity(),TosUtil.TOAST_ERROR,  msg).show();


            }

        });

    }

}
