package com.sheep.gamegroup.module.login.fragments;


import android.content.Context;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.LoginEntity;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.module.login.controller.ChangePasswordController;
import com.sheep.gamegroup.module.login.controller.LoginController;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.PreferenceUtils;
import com.sheep.gamegroup.util.UMConfigUtils;
import com.sheep.gamegroup.view.customview.CountDownButton;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.base.BaseFragment;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.SpUtils;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
public class Captcha4ForgetFgt extends BaseFragment {

    private ChangePasswordController mController;

    @BindView(R.id.tip_view)
    TextView tipView;
    @BindView(R.id.title_view)
    TextView titleView;
    @BindView(R.id.captcha_box)
    EditText captchaBox;
    @BindView(R.id.resend_captcha_btn)
    CountDownButton countDownButton;

    private String phoneNum;

    public Captcha4ForgetFgt() {
        // Required empty public constructor
    }

    public static Captcha4ForgetFgt newInstance(ChangePasswordController controller) {
        Captcha4ForgetFgt fragment = new Captcha4ForgetFgt();
        fragment.mController = controller;
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_valid_captcha;
    }

    @Override
    public void onViewCreated() {
//        titleView.setVisibility(View.INVISIBLE);
        phoneNum = DataUtil.getInstance().getUserEntity().getMobile();
        tipView.setText("我们已向 " + phoneNum + " 发送验证码短信请查看短信并输入验证码");
        captchaBox.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s != null && s.length() == 6) {
                    submitCaptcha();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        countDownButton.click();
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(captchaBox, InputMethodManager.SHOW_FORCED);
    }

    private void submitCaptcha() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", captchaBox.getText().toString());
        SheepApp.getInstance().getNetComponent().getApiService().resetPass4Valid(jsonObject)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        hideProgress();
                        G.shortToast(baseMessage);
                    }

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        hideProgress();
                        mController.whenCaptcha(captchaBox.getText().toString());
                    }
                });
    }

    @OnClick(R.id.resend_captcha_btn)
    public void sendCaptcha(View v) {
        SheepApp.getInstance().getNetComponent().getApiService().resetPass4Captcha(new JSONObject())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        Snackbar.make(v, "验证码已发送", Snackbar.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        Snackbar.make(v, "发生错误", Snackbar.LENGTH_SHORT).show();
                    }
                });
    }

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

}
