package com.sunzee.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sunzee.R;
import com.sunzee.base.MvpFragment;
import com.sunzee.mvp.passwordreset.PasswordResetPresenter;
import com.sunzee.mvp.passwordreset.PasswordResetView;
import com.sunzee.utils.ToastUtil;
import com.sunzee.utils.UiUtil;

/**
 * 重置密码界面 fragment
 */
public class PasswordResetFragment extends MvpFragment<PasswordResetPresenter> implements PasswordResetView, View.OnClickListener {

    private EditText mUsername;
    private EditText mUserPassword;
    private EditText mAgineUserPassword;
    private TextView mTvLogin;
    private LinearLayout mLoginGroup;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_change_password, container, false);
        initView(inflate);
        initEvent();
        return inflate;
    }

    private void initEvent() {
        mTvLogin.setOnClickListener(this);
        mLoginGroup.setOnClickListener(this);

    }

    private void initView(View inflate) {
        mUsername = inflate.findViewById(R.id.et_username_changepass);
        mUserPassword = inflate.findViewById(R.id.et_userpassword_changepass);
        mAgineUserPassword = inflate.findViewById(R.id.et_agine_userpassword_changepass);
        mTvLogin = inflate.findViewById(R.id.tv_login_chagepass);
        mLoginGroup = inflate.findViewById(R.id.ll_logingroup);

    }

    @Override
    protected PasswordResetPresenter createPresenter() {
        return new PasswordResetPresenter(this);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showResult(boolean success) {
        if (success) {
            ToastUtil.showToast(UiUtil.getStringRes(R.string.modified_success));
        } else {
            ToastUtil.showToast(UiUtil.getStringRes(R.string.modified_failed));
        }
    }

    private static final String TAG = "PasswordResetFragment";

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_login_chagepass:
                mvpPresenter.reset(mUsername.getText().toString().trim(),
                        mUserPassword.getText().toString().trim(),
                        mAgineUserPassword.getText().toString().trim());
                break;
            case R.id.ll_logingroup:
                Log.d(TAG, "onClick: ");
                //隐藏键盘
                @SuppressLint("WrongConstant") InputMethodManager imm = (InputMethodManager)
                        mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                break;
        }
    }
}
