package com.fxy.view;

import android.R.color;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.fxy.R;


/**
 * qibin0506
 * 2016-01-18
 */

public class DropTextView extends FrameLayout implements View.OnClickListener, OnItemClickListener {
    private TextView dropview_tv;  // 输入框
    private ImageView mDropImage; // 右边的图片按钮
    private PopupWindow mPopup; // 点击图片弹出popupwindow
    private WrapListView mPopView; // popupwindow的布局
    private LinearLayout dropview_ll;

    private int mDrawableLeft;
    private int mDropMode; // flow_parent or wrap_content
    private String mHit;

    public DropTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DropTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        LayoutInflater.from(context).inflate(R.layout.edit_layout, this);
        mPopView = (WrapListView) LayoutInflater.from(context).inflate(R.layout.pop_view, null);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DropTextView, defStyle, 0);
        mDrawableLeft = ta.getResourceId(R.styleable.DropTextView_drawableRight, R.drawable.ic_launcher);
        mDropMode = ta.getInt(R.styleable.DropTextView_dropMode, 0);
        mHit = ta.getString(R.styleable.DropTextView_hint);
        ta.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        dropview_tv = (TextView) findViewById(R.id.dropview_tv);
        mDropImage = (ImageView) findViewById(R.id.dropview_image);
        dropview_ll = (LinearLayout) findViewById(R.id.dropview_ll);

        dropview_tv.setSelectAllOnFocus(true);
        mDropImage.setImageResource(mDrawableLeft);

        if (!TextUtils.isEmpty(mHit)) {
            dropview_tv.setHint(mHit);
        }

        mDropImage.setOnClickListener(this);
        mPopView.setOnItemClickListener(this);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        // 如果布局发生改
        // 并且dropMode是flower_parent
        // 则设置ListView的宽度
        if (changed && 0 == mDropMode) {
            mPopView.setListWidth(getMeasuredWidth());
        }
    }

    /**
     * 设置Adapter
     *
     * @param adapter ListView的Adapter
     */
    public void setAdapter(BaseAdapter adapter) {
        mPopView.setAdapter(adapter);

        mPopup = new PopupWindow(mPopView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        mPopup.setBackgroundDrawable(new ColorDrawable(getResources().getColor(color.transparent)));
        mPopup.setFocusable(true); // 让popwin获取焦点
    }

    /**
     * 获取输入框内的内容
     *
     * @return String content
     */
    public String getText() {
        return dropview_tv.getText().toString();
    }

    /**
     * 设置输入框的内容
     */
    public void setText(String content) {
        dropview_tv.setText(content);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.dropview_image) {//dropview_image
            if (null != mPopup && mPopup.isShowing()) {
                mPopup.dismiss();
                return;
            }

            if (null != mPopup)
                mPopup.showAsDropDown(this, 0, 5);
        } else if (v.getId() == R.id.dropview_tv) {//mEditText
            if (null != mPopup)
                mPopup.showAsDropDown(this, 0, 5);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        dropview_tv.setText(mPopView.getAdapter().getItem(position).toString());
        mPopup.dismiss();
    }
}
