package ai.pai.client.views;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.EditText;

import com.android.views.popup.BasePopupWindow;

import ai.pai.client.R;

public class InputPopup extends BasePopupWindow {

    private Button mSendBtn;
    private EditText mInputEdit;

    public InputPopup(Activity context,View.OnClickListener listener) {
        super(context);
        mSendBtn = (Button) mPopupView.findViewById(R.id.btn_comment_send);
        mInputEdit = (EditText) mPopupView.findViewById(R.id.et_comment_content);
        mInputEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mSendBtn.setEnabled(s.toString().length()>0);
            }
        });
        setAutoShowInputMethod(true);
        mSendBtn.setOnClickListener(listener);
    }

    public String getInputContent(){
        if(mInputEdit.getText()!=null){
            return mInputEdit.getText().toString();
        }
        return null;
    }
    @Override
    protected Animation getShowAnimation() {
         return getDefaultScaleAnimation();
    }

    @Override
    public Animator getShowAnimator() {
        return getDefaultSlideFromBottomAnimationSet();
    }

    @Override
    public View getInputView() {
        return mInputEdit;
    }

    @Override
    protected View getClickToDismissView() {
        return mPopupView;
    }

    @Override
    public View getPopupView() {
        return LayoutInflater.from(mContext).inflate(R.layout.popup_comment_input,null);
    }

    @Override
    public View getAnimaView() {
        return mPopupView.findViewById(R.id.popup_anima);
    }

    @Override
    public Animator getExitAnimator() {
        AnimatorSet set = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            set = new AnimatorSet();
            if (getAnimaView() != null) {
                set.playTogether(
                        ObjectAnimator.ofFloat(getAnimaView(), "translationY", 0, 250).setDuration(400),
                        ObjectAnimator.ofFloat(getAnimaView(), "alpha", 1, 0.4f).setDuration(250 * 3 / 2));
            }
        }
        return set;
    }
}