package ai.pai.client.views;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import ai.pai.client.R;

public class CommentDialog extends Dialog {

    private View rootView;
    private Button mSendBtn;
    private EditText mInputEdit;

    private View.OnClickListener listener;

    public CommentDialog(Activity context, View.OnClickListener listener) {
        super(context, R.style.inputDialog);
        this.listener = listener;
        initViews(context);
    }

    private void initViews(Context context) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rootView = inflater.inflate(R.layout.popup_comment_input, null);
        mSendBtn = (Button) rootView.findViewById(R.id.btn_comment_send);
        mInputEdit = (EditText) rootView.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);
            }
        });
        this.setContentView(rootView);
        mSendBtn.setOnClickListener(listener);
        rootView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                int height = rootView.findViewById(R.id.popup_anima).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
    }

    @Override
    public void show() {
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);  //此处可以设置dialog显示的位置
        window.setWindowAnimations(R.style.AnimBottom);  //添加动画

        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(layoutParams);
        super.show();
    }

    public String getInputContent() {
        if (mInputEdit.getText() != null) {
            return mInputEdit.getText().toString();
        }
        return null;
    }

    public View getInputView() {
        return mInputEdit;
    }

}
