package com.itant.joycity.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.Layout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.itant.joycity.R;
import com.itant.joycity.bean.GridImageTextBean;
import com.itant.joycity.tool.UITool;

import java.util.List;

/**
 * Created by Jason on 2016/10/6.
 * 图片列表适配器
 */
public class GridImageTextAdapter extends BaseAdapter {

    private List<GridImageTextBean> gridImageTextBeens;
    private Context context;

    private RelativeLayout.LayoutParams goneParams;
    private RelativeLayout.LayoutParams visibleParams;

    public void setGridTextBeen(List<GridImageTextBean> gridImageTextBeen) {
        this.gridImageTextBeens = gridImageTextBeen;
    }

    public GridImageTextAdapter() {

    }

    public GridImageTextAdapter(Context context, List<GridImageTextBean> gridImageTextBeens) {
        this.gridImageTextBeens = gridImageTextBeens;
        this.context = context;

        goneParams = new RelativeLayout.LayoutParams(1, 1);
        visibleParams = new RelativeLayout.LayoutParams(UITool.dpToPx(context,22), UITool.dpToPx(context,20));
    }

    @Override
    public int getCount() {
        return gridImageTextBeens == null ? 0 : gridImageTextBeens.size();
    }

    @Override
    public Object getItem(int position) {
        return gridImageTextBeens == null ? null : gridImageTextBeens.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GridImageTextBean gridImageTextBean = gridImageTextBeens.get(position);
        // 图标
        String imageUrl = gridImageTextBean.getPicUrl().trim();

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_image_text_grid, null);
            viewHolder = new ViewHolder();
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.iv_url_icon = (ImageView) convertView.findViewById(R.id.iv_url_icon);
            viewHolder.rl_left = (RelativeLayout) convertView.findViewById(R.id.rl_left);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (!TextUtils.isEmpty(imageUrl)) {
            if (imageUrl.endsWith(".png")
                    || imageUrl.endsWith(".PNG")
                    || imageUrl.endsWith(".jpg")
                    || imageUrl.endsWith(".JPG")
                    || imageUrl.endsWith(".jpeg")
                    || imageUrl.endsWith(".JPEG")
                    || imageUrl.endsWith(".ico")
                    || imageUrl.endsWith(".ICO")
                    || imageUrl.endsWith(".gif")
                    || imageUrl.endsWith(".GIF")
                    || imageUrl.endsWith(".bmp")
                    || imageUrl.endsWith(".BMP")
                    ) {
                viewHolder.rl_left.setVisibility(View.VISIBLE);
                // 显示图片
                Glide.with(context).load(imageUrl).into(viewHolder.iv_url_icon);
            } else {
                viewHolder.rl_left.setVisibility(View.GONE);
            }
        } else {
            viewHolder.rl_left.setVisibility(View.GONE);
        }

        // 文字
        viewHolder.tv_name.setText(gridImageTextBean.getTitle());
        //viewHolder.tv_name.setText("中国银行很有钱");

        return convertView;
    }


    private ViewHolder viewHolder;
    private static class ViewHolder {
        TextView tv_name;
        ImageView iv_url_icon;
        RelativeLayout rl_left;
    }
}
