package com.navinfo.outdoor.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.model.HttpParams;
import com.navinfo.outdoor.R;
import com.navinfo.outdoor.activity.FragmentManagement;
import com.navinfo.outdoor.activity.LoginActivity;
import com.navinfo.outdoor.api.Constant;
import com.navinfo.outdoor.base.BaseFragment;
import com.navinfo.outdoor.bean.MessageCodeBean;
import com.navinfo.outdoor.bean.UserBean;
import com.navinfo.outdoor.http.Callback;
import com.navinfo.outdoor.http.HttpInterface;
import com.navinfo.outdoor.http.OkGoBuilder;
import com.navinfo.outdoor.util.Base64;
import com.navinfo.outdoor.util.RegexUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * 忘记密码页面
 */
public class ForgetPawFragment extends BaseFragment implements View.OnClickListener {
    private ImageView ivFinish;
    private TextView forgetPawGetNote;
    private EditText etForgetPawPhone, etForgetPawNote, etForgetPawPaw, etForgetPawConfirmPaw;
    private Button btnForgetPaw;
    private String sessionId;
    private CountDownTimer timer;

    public static ForgetPawFragment newInstance(Bundle bundle) {
        ForgetPawFragment fragment = new ForgetPawFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_forget_paw;
    }

    @Override
    protected void initView() {
        super.initView();
        ivFinish = findViewById(R.id.iv_forgePaw_finish);
        ivFinish.setOnClickListener(this);
        etForgetPawPhone = findViewById(R.id.et_forgetPaw_phone);
        etForgetPawNote = findViewById(R.id.et_forgetPaw_note);
        etForgetPawPaw = findViewById(R.id.et_forgetPaw_paw);
        etForgetPawConfirmPaw = findViewById(R.id.et_forgetPaw_confirm_paw);
        forgetPawGetNote = findViewById(R.id.forgetPaw_get_note);
        forgetPawGetNote.setOnClickListener(this);
        btnForgetPaw = findViewById(R.id.btn_forgetPaw);
        btnForgetPaw.setOnClickListener(this::onClick);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_forgePaw_finish:
                getActivity().finish();
                break;
            case R.id.forgetPaw_get_note:
                if (!RegexUtil.isPhone(etForgetPawPhone.getText().toString())){
                    Toast.makeText(getActivity(), "手机号错误，请重新输入", Toast.LENGTH_SHORT).show();
                }else {
                    registerNote();
                    initGetNote();
                }

                break;
            case R.id.btn_forgetPaw:
                initForgetPaw();
                break;
        }
    }

    private void initForgetPaw() {
        HttpParams httpParams = new HttpParams();
        String mobile = etForgetPawPhone.getText().toString().trim();
        if (!RegexUtil.isPhone(mobile)) {
            Toast.makeText(getActivity(), "手机号输入错误", Toast.LENGTH_SHORT).show();
            return;
        } else {
            httpParams.put("mobile", mobile);
        }
        String code = etForgetPawNote.getText().toString().trim();
        if (code == null || code.equals("")) {
            Toast.makeText(getActivity(), "请获取短信验证码", Toast.LENGTH_SHORT).show();
            return;
        } else {
            httpParams.put("code", code);
        }
        String password = etForgetPawPaw.getText().toString().trim();
        if (password == null || password.equals("")) {
            Toast.makeText(getActivity(), "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        } else {
            try {
                httpParams.put("password",  Base64.desEncrypt(password));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        String conFirmPaw = etForgetPawConfirmPaw.getText().toString().trim();
        if (conFirmPaw == null || conFirmPaw.equals("")) {
            Toast.makeText(getActivity(), "请输入确认密码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!conFirmPaw.equals(password)) {
            Toast.makeText(getActivity(), "密码和确认密码请保持一致", Toast.LENGTH_SHORT).show();
        }
        if (sessionId == null || sessionId.equals("")) {
            Toast.makeText(getActivity(), "请短信获取验证码", Toast.LENGTH_SHORT).show();
            return;
        } else {
            httpParams.put("sessionId", sessionId);
        }
        long time=System.currentTimeMillis();
        httpParams.put("datetime",time);
        showLoadingDialog();
        OkGoBuilder.getInstance()
                .Builder(getActivity())
                .url(HttpInterface.USER_FORGET_PASSWORD)
                .token(null)
                .params(httpParams)
                .cls(UserBean.class)
                .postRequest(new Callback<UserBean>() {
                    @Override
                    public void onSuccess(UserBean response, int id) {
                        dismissLoadingDialog();
                        if (response.getCode()==200){
                            Constant.USER_NAME = mobile;
                            Constant.PASS_WORD = password;
                            Message obtain = Message.obtain();
                            obtain.what=Constant.REGISTER_WORD;
                            obtain.obj = true;
                            EventBus.getDefault().post(obtain);
                            getActivity().finish();
                        }else {
                            Toast.makeText(getActivity(), response.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                       // Toast.makeText(getActivity(), response.getMessage() + "", Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onError(Throwable e, int id) {
                        dismissLoadingDialog();
                        Toast.makeText(getActivity(), e.getMessage() + "", Toast.LENGTH_SHORT).show();
                    }
                });
    }
    private void registerNote() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        forgetPawGetNote.setEnabled(false);
        timer = new CountDownTimer(60 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                forgetPawGetNote.setText(millisUntilFinished / 1000 + "秒后重新获取验证码");
            }

            @Override
            public void onFinish() {
                forgetPawGetNote.setEnabled(true);
                forgetPawGetNote.setText("重新发送");
            }
        };
        timer.start();
    }
    private void initGetNote() {
        HttpParams httpParams = new HttpParams();
        String mobile = etForgetPawPhone.getText().toString().trim();
        if (mobile == null || mobile.equals("")) {
            Toast.makeText(getActivity(), "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        } else {
            httpParams.put("mobile", mobile);
        }
        httpParams.put("type", 2);
        showLoadingDialog();
        OkGoBuilder.getInstance()
                .Builder(getActivity())
                .url(HttpInterface.USER_MESSAGE)
                .cls(MessageCodeBean.class)
                .token(Constant.ACCESS_TOKEN)
                .params(httpParams)
                .getRequest(new Callback<MessageCodeBean>() {
                    @Override
                    public void onSuccess(MessageCodeBean response, int id) {
                        dismissLoadingDialog();
                        if (response.getCode() == 200) {
                            sessionId = response.getBody();
                        }else {
                            Toast.makeText(getActivity(), response.getMessage() + "", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e, int id) {
                        dismissLoadingDialog();
                        Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
    }
}