package cn.yyxx.commsdk.impl.channel.hnyy.fragment;


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

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
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.TextView;

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


public class UserPhoneLoginFragment extends BaseFragment {


    private EditText et_user_phone,et_user_code;
    private CheckBox cb_register;
    private TextView tv_tip,tv_private_policy;
    private Button btn_get_code,btn_account_login,btn_guest_register,btn_reg_compete;
    private CodeVerifyTimeCount codeVerifyTimeCount;
    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_phone_login"), container, false);
        view.setClickable(true);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        et_user_phone = view.findViewById(ResourceUtil.getId(getActivity(),"et_user_phone"));
        et_user_code = view.findViewById(ResourceUtil.getId(getActivity(),"et_user_code"));
        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);
        btn_get_code = view.findViewById(ResourceUtil.getId(getActivity(),"btn_get_code"));
        btn_get_code.setOnClickListener(this);
        btn_account_login = view.findViewById(ResourceUtil.getId(getActivity(),"btn_account_login"));
        btn_account_login.setOnClickListener(this);
        btn_guest_register = view.findViewById(ResourceUtil.getId(getActivity(),"btn_guest_register"));
        btn_guest_register.setOnClickListener(this);
        btn_reg_compete = view.findViewById(ResourceUtil.getId(getActivity(),"btn_reg_compete"));
        btn_reg_compete.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(), "btn_reg_compete")){

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

                return;
            }
            clickLoginTime = System.currentTimeMillis();

            phoneLoginWithSms(getActivity());

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

            String phone =  et_user_phone.getText().toString();

            if (phone.length() < 10) {

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

                return;

            }
            getPhoneCaptcha(getActivity(),phone,"register");

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

            loginImpl.switchFragment(HNYYLoginFragmentTag.PAGE_ACCOUNT_LOGIN,null);


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

            loginImpl.switchFragment(HNYYLoginFragmentTag.PAGE_ACCOUNT_REGISTER,null);


        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (codeVerifyTimeCount != null){
            codeVerifyTimeCount.cancel();
        }

    }

    private void phoneLoginWithSms(final Activity activity){
        final String code = et_user_code.getText().toString();
        final String account = et_user_phone.getText().toString();


        if(account.length() < 10 || TextUtils.isEmpty(account))
        {
            new TosUtil(getActivity(),TosUtil.TOAST_ERROR,getString(ResourceUtil.getStringId(getActivity(), "hnyy_pls_input_right_phone"))).show();

            return;
        }

        if(code.length() < 4 || TextUtils.isEmpty(code))
        {

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

            return;
        }

        if (!cb_register.isChecked()){

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

            return;

        }

        SdkPlatformImpl.loginViewModel.phoneLoginWithSms(getActivity(), account, code, new PhoneLoginCallBack() {

            @Override
            public void onPhoneLoginSuccess(String msg) {


                    if (!getActivity().isFinishing()) {
                        getActivity().finish();
                        SdkPlatformImpl.getInstance(activity).loginSuccess();
                    }

            }

            @Override
            public void onPhoneLoginFailed(String msg) {

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

            }

            @Override
            public void onPhoneNewLogin() {
                Bundle bundle = new Bundle();
                bundle.putString("account",account);
                bundle.putString("code",code);
                loginImpl.switchFragment(HNYYLoginFragmentTag.PAGE_POHONE_SET_PASSWORD,bundle);
            }
        });

    }


    private void  getPhoneCaptcha(Activity activity, String phoneNum, String action){
        SdkPlatformImpl.loginViewModel.getPhoneCaptcha(activity, phoneNum, action, new ICallback() {
            @Override
            public void onResult(int code, String result) {
                if (code == ResultInfo.CODE_SUCCESS){
                    getCodeSuccess();
                }else{
                    new TosUtil(getActivity(), TosUtil.TOAST_ERROR, result).show();
                }
            }
        });
    }
    private void getCodeSuccess() {
        btn_get_code.setEnabled(false);
        btn_get_code.setText("60s");

        codeVerifyTimeCount = new CodeVerifyTimeCount(60000, 1000);
        codeVerifyTimeCount.start();
    }

    class CodeVerifyTimeCount extends CountDownTimer {

        public CodeVerifyTimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub
            btn_get_code.setText(millisUntilFinished / 1000 + "s");

        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            btn_get_code.setEnabled(true);

            btn_get_code.setText(ResourceUtil.getStringId(getActivity(), "hnyy_get_phone_code"));
        }

    }







}
