package cn.yyxx.commsdk.impl.merge.platform.xinrui.fragment;

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.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

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.impl.merge.platform.xinrui.XinRuiLoginFragmentTag;
import cn.yyxx.commsdk.impl.merge.platform.xinrui.dialog.WvPageListDialog;
import cn.yyxx.commsdk.impl.merge.platform.xinrui.fragment.widget.TosUtil;
import cn.yyxx.commsdk.impl.merge.platform.xinrui.impl.SdkPlatformImpl;

public class UserRetrievePwdFragment extends BaseFragment {

    private EditText et_user_phone,et_user_code,et_user_pwd;
    private Button btn_get_code,reg_compete_btn;
    private ImageView close_btn;



    private TimeCount timer;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(ResourceUtil.getLayoutId(getActivity(),"xinrui_retrieve_pwd"), 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"));
        et_user_pwd  = view.findViewById(ResourceUtil.getId(getActivity(), "et_user_pwd"));
        btn_get_code  = view.findViewById(ResourceUtil.getId(getActivity(), "btn_get_code"));
        reg_compete_btn  = view.findViewById(ResourceUtil.getId(getActivity(), "reg_compete_btn"));

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

        btn_get_code.setOnClickListener(this);
        close_btn.setOnClickListener(this);
        reg_compete_btn.setOnClickListener(this);

        timer = new TimeCount(60000, 1000);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

        int id = v.getId();
        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(), "xinrui_pls_input_right_phone"))).show();
                return;
            }
            getPhoneCaptcha(getActivity(),phone,"modify_pwd");
        }else if (id == ResourceUtil.getId(getActivity(), "reg_compete_btn")) {
            String phone =  et_user_phone.getText().toString();
            String code = et_user_code.getText().toString();
            String pwd = et_user_pwd.getText().toString();
            if (phone.length() < 10) {

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

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


                return;
            }
            if(pwd.length() < 6 || TextUtils.isEmpty(pwd))
            {
                new TosUtil(getActivity(),TosUtil.TOAST_ERROR, getString(ResourceUtil.getStringId(getActivity(), "pwd_limit_6"))).show();

                return;
            }
            resetPwdWithPhone(getActivity(),phone,code,pwd);

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

            loginImpl.switchFragment(XinRuiLoginFragmentTag.PAGE_ACCOUNT_LOGIN,null);

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

            new WvPageListDialog(getActivity(),ResourceUtil.getStyleId(getActivity(),"half_black_dialog"), SdkPlatformImpl.initConfig.service_center).show();

        }

    }


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


        timer.start();
    }

    class TimeCount extends CountDownTimer {

        public TimeCount(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(), "get_phone_code"));
        }

    }
    public interface ResetPwdCallback {

        void onResetSuccess(String msg);
        void onResetFailed(String msg);


    }

    private void resetPwdWithPhone(final Activity activity, final String phoneNum, String code, final String pwd) {
        SdkPlatformImpl.loginViewModel.resetPassword(activity,phoneNum,pwd,code, new ICallback() {
            @Override
            public void onResult(int code, String result) {
                if (code == ResultInfo.CODE_SUCCESS){

                    new TosUtil(getActivity(),TosUtil.TOAST_GREEN,  result).show();

                    loginImpl.switchFragment(XinRuiLoginFragmentTag.PAGE_ACCOUNT_LOGIN,null);
                }else{
                    new TosUtil(getActivity(),TosUtil.TOAST_ERROR,  result).show();
                }
            }
        });
    }


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

    }
}
