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;
    @BindView(R.id.confirm_pwd_box)
    EditText confirmpwdBox;
    @BindView(R.id.show_hide_confirm_btn)
    ImageView showHideConfirmBtn;

    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, R.id.show_hide_confirm_btn})
    public void doShowHidePwd(View v) {
        ImageView btn = v.getId() == R.id.show_hide_confirm_btn ? showHideConfirmBtn : showHidePwdBtn;
        EditText box = v.getId() == R.id.show_hide_confirm_btn ? confirmpwdBox : newpwdBox;
        btn.setSelected(!btn.isSelected());
        btn.setImageResource(btn.isSelected() ? R.mipmap.pwd_show : R.mipmap.pwd_hide);
        if (btn.isSelected()) {
            //如果选中，显示密码
            box.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            //否则隐藏密码
            box.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
        box.setSelection(box.getText().toString().length());
    }

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