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 PersonInfoPopup extends BasePopupWindow {

    private Button mJoinBtn;
    private EditText mNameEdit;
    private EditText mPhoneEdit;

    public PersonInfoPopup(Activity context, View.OnClickListener listener) {
        super(context);
        mJoinBtn = (Button) mPopupView.findViewById(R.id.btn_info_confirm);
        mNameEdit = (EditText) mPopupView.findViewById(R.id.et_user_name);
        mPhoneEdit = (EditText)mPopupView.findViewById(R.id.et_user_phone);

        mPhoneEdit.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) {
                mJoinBtn.setEnabled(s.toString().length()==11);
            }
        });
        setAutoShowInputMethod(true);
        mJoinBtn.setOnClickListener(listener);
    }

    public String getInputName(){
        if(mNameEdit.getText()!=null){
            return mNameEdit.getText().toString();
        }
        return null;
    }

    public String getInputPhone(){
        if(mPhoneEdit.getText()!=null){
            return mPhoneEdit.getText().toString();
        }
        return null;
    }

    @Override
    protected Animation getShowAnimation() {
         return getDefaultScaleAnimation();
    }

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

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

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

    @Override
    public View getPopupView() {
        return LayoutInflater.from(mContext).inflate(R.layout.popup_person_info,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;
    }
}