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


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.sheep.gamegroup.module.login.ChangePasswordAct;
import com.sheep.gamegroup.module.login.controller.ChangePasswordController;
import com.sheep.gamegroup.util.StringUtils;
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 butterknife.BindView;
import butterknife.OnClick;

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

    private ChangePasswordController mController;

    @BindView(R.id.old_pwd_box)
    EditText oldpwdBox;
    @BindView(R.id.show_hide_pwd_btn)
    ImageView showHidePwdBtn;

    public static OldPasswordFgt newInstance(ChangePasswordController controller) {
        OldPasswordFgt fgt = new OldPasswordFgt();
        fgt.mController = controller;
        return fgt;
    }

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

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

    @Override
    public void onViewCreated() {

    }

    @OnClick(R.id.forget_pwd_btn)
    public void doForgetPwd(View v) {
        mController.whenForget();
    }

    @OnClick(R.id.show_hide_pwd_btn)
    public void doShowHidePwd(View v) {
        showHidePwdBtn.setSelected(!showHidePwdBtn.isSelected());
        showHidePwdBtn.setImageResource(showHidePwdBtn.isSelected() ? R.mipmap.pwd_show : R.mipmap.pwd_hide);
        if (showHidePwdBtn.isSelected()) {
            //如果选中，显示密码
            oldpwdBox.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            //否则隐藏密码
            oldpwdBox.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
        oldpwdBox.setSelection(oldpwdBox.getText().toString().length());
    }

    @OnClick(R.id.next_btn)
    public void doNext(View v) {
        String password = oldpwdBox.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            G.shortToast("旧密码不能为空");
            return;
        }
        if (!StringUtils.isPassword(password)) {
            G.shortToast("密码只能包含英文、数字、_.-@$!*%符号，长度6-16");
            return;
        }
        mController.whenNext(password);
    }

}
