
package com.ewei.demo.widget.tag;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ewei.demo.R;
import com.ewei.sdk.entity.Tag;

import java.util.ArrayList;
import java.util.List;


/**
 * @author kince
 * @category 模仿最美应用底部tagview
 */
public class TagListView extends FlowLayout {

    private boolean mIsDeleteMode;
    private OnTagClickListener mOnTagClickListener;
    private OnTagCloseListener mOnTagCloseListener;
    private final List<Tag> mTags = new ArrayList<Tag>();

    /**
     * @param context
     */
    public TagListView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        init();
    }

    /**
     * @param context
     * @param attributeSet
     */
    public TagListView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        // TODO Auto-generated constructor stub
        init();
    }

    /**
     * @param context
     * @param attributeSet
     * @param defStyle
     */
    public TagListView(Context context, AttributeSet attributeSet, int defStyle) {
        super(context, attributeSet, defStyle);
        // TODO Auto-generated constructor stub
        init();
    }


    private void init() {

    }


    private void inflateTagView(final Tag t) {

        View localTagView = LayoutInflater.from(getContext()).inflate(R.layout.ewei_open_tag, null);
        ImageView mEweiTagClose = (ImageView) localTagView.findViewById(R.id.ewei_tag_close);
        TextView mEweiTagText = (TextView) localTagView.findViewById(R.id.ewei_tag_text);
        mEweiTagText.setText(t.name);
        if (!mIsDeleteMode) {
            mEweiTagClose.setVisibility(View.GONE);
            localTagView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnTagClickListener != null) {
                        mOnTagClickListener.onTagClick(v, t);
                    }
                }
            });

        } else {
            mEweiTagClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnTagCloseListener != null) {
                        mOnTagCloseListener.onTagClickClose(v, t);
                    }
                }
            });
        }
        localTagView.setTag(t);
        addView(localTagView);
    }


    public void addTag(Tag tag) {

        boolean isHas = false;
        for (Tag itemTage : mTags) {
            if (itemTage.id != null && itemTage.id.equals(tag.id)) {
                isHas = true;
                break;
            }
        }

        if (!isHas) {
            mTags.add(tag);
            inflateTagView(tag);
        }

    }


    public void addTags(List<Tag> lists) {
        mTags.clear();
        removeAllViews();
        for (int i = 0; i < lists.size(); i++) {
            addTag((Tag) lists.get(i));
        }
    }

    public List<Tag> getTags() {
        return mTags;
    }

    public View getViewByTag(Tag tag) {
        return findViewWithTag(tag);
    }

    public void removeTag(Tag tag) {
        mTags.remove(tag);
        removeView(getViewByTag(tag));
    }


    public void clearTag() {
        mTags.clear();
        removeAllViews();
    }


    public void setOnTagClickListener(OnTagClickListener onTagClickListener) {
        mOnTagClickListener = onTagClickListener;
    }

    public void setmOnTagCloseListener(OnTagCloseListener mOnTagCloseListener) {
        this.mOnTagCloseListener = mOnTagCloseListener;
    }


    public void setTags(List<? extends Tag> lists) {
        removeAllViews();
        mTags.clear();
        for (int i = 0; i < lists.size(); i++) {
            addTag(lists.get(i));
        }
    }

    public void setDeleteMode(boolean b) {
        mIsDeleteMode = b;
    }

    public interface OnTagClickListener {
        void onTagClick(View tagView, Tag tag);
    }

    public interface OnTagCloseListener {
        void onTagClickClose(View tagView, Tag tag);
    }

}
