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


import android.os.Bundle;
import android.support.v4.app.Fragment;
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.controller.ChangePasswordController;
import com.sheep.gamegroup.util.StringUtils;
import com.sheep.jiuyan.samllsheep.R;
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 NewPasswordFgt extends BaseFragment {

    private ChangePasswordController mController;

    @BindView(R.id.new_pwd_box)
    EditText newpwdBox;
    @BindView(R.id.show_hide_pwd_btn)
    ImageView showHidePwdBtn;

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

    public NewPasswordFgt() {
    }


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

    @Override
    public void onViewCreated() {

    }

    @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()) {
            //如果选中，显示密码
            newpwdBox.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            //否则隐藏密码
            newpwdBox.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
        newpwdBox.setSelection(newpwdBox.getText().toString().length());
    }

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