package com.danding.yang800.Adapter;

import java.util.Collections;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.danding.yang800.Config.ConfigUtil;
import com.danding.yang800.Entity.GoodsInfo;
import com.danding.yang800.General.Yang800App;
import com.danding.yang800.R;
import com.zhy.autolayout.utils.AutoUtils;

public class GoodsSortAdapter extends MyBaseAdapter<GoodsInfo> implements
        DragGridBaseAdapter {
    private int mHidePosition = -1;

    public GoodsSortAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Info info = null;
        // if (convertView == null) {
        convertView = inflater.inflate(R.layout.grid_item_layout, null);
        info = new Info();
        info.item_image = (ImageView) convertView
                .findViewById(R.id.grid_item_Iv);
        //info.item_text = (TextView) convertView.findViewById(R.id.item_text);
        // convertView.setTag(info);
        // if(position == mHidePosition){
        // convertView.setVisibility(View.INVISIBLE);
        // }
        // } else {
        // info = (Info) convertView.getTag();
        // }
//		if (getItem(position).getUrl().contains("http")) {
//			info.item_image.setDefaultImageResId(R.drawable.ic_launcher);
//			info.item_image.setErrorImageResId(R.drawable.ic_launcher);
//			info.item_image.setImageUrl(getItem(position).getUrl(),
//					mImageLoader);
//		} else {
//			info.item_image.setDefaultImageResId(R.drawable.ic_launcher);
//			info.item_image.setErrorImageResId(R.drawable.ic_launcher);
//			info.item_image.setImageUrl("", mImageLoader);
//		}
        AutoUtils.autoSize(convertView);
        Glide.with(context).load(ConfigUtil.getImageUrl(getItem(position).getImgUrl(), 210, 210)).error(R.drawable.huisebeijing).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(info.item_image);
        //info.item_text.setText(getItem(position).getName());
        return convertView;
    }

    class Info {
        ImageView item_image;
        TextView item_text;
    }

    @Override
    public void reorderItems(int oldPosition, int newPosition) {
        GoodsInfo imageInfo = lists.get(oldPosition);
        // ImageInfo imageInfo2 = lists.get(oldPosition-1);
        if (oldPosition < newPosition) {
            for (int i = oldPosition; i < newPosition; i++) {
                Collections.swap(lists, i, i + 1);
            }
        } else if (oldPosition > newPosition) {
            for (int i = oldPosition; i > newPosition; i--) {
                Collections.swap(lists, i, i - 1);
            }
        }
        lists.set(newPosition, imageInfo);
        // lists.set(oldPosition, imageInfo2);
    }

    @Override
    public void setHideItem(int hidePosition) {
        this.mHidePosition = hidePosition;
        notifyDataSetChanged();
    }

}
