package com.zhaohaoting.framework.view;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.zhaohaoting.framework.R;


/**
 * author：Created by ZHT on 2018/1/8 17:36
 * email：526309416@qq.com
 * desc：文本框
 */
public class VerCodeTextView extends android.support.v7.widget.AppCompatTextView implements Runnable {

    private Context context;
    public int recLen = 60;
    private Handler handler;

    public VerCodeTextView(Context context) {
        this(context, null);
    }

    public VerCodeTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VerCodeTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }


    public String getText() {
        return super.getText().toString().trim();
    }

    public void setText(String text) {
        super.setText(text);
    }

    public void setHint(String hint) {
        super.setHint(hint);
    }


    private InputEditListener inputEditListener;

    public void setInputEditListener(InputEditListener inputEditListener) {
        this.inputEditListener = inputEditListener;
    }

    private interface InputEditListener {

        void verCodeClick();
    }


    @Override
    public void run() {
        if (recLen > 1) {
            recLen--;
            setText(recLen + "S");
            handler.postDelayed(this, 1000);
        } else {
            setBtnVerCodeFos();
        }
    }

    public void setBtnVerCodeFos() {
        if (handler != null) {
            handler.removeCallbacks(this);
        }
        setText(context.getString(R.string.re_get_ver_code));
        setEnabled(true);
        recLen = 60;
    }

    public void btnVerCodeNoFos() {
        if (handler == null) {
            handler = new Handler();
        }
        setEnabled(false);
        handler.postDelayed(VerCodeTextView.this, 0);
    }

    public void destroy() {
        if (handler != null) {
            handler.removeCallbacks(this);
            handler = null;
        }
    }
}
