package com.app.baselibrary.widget.viewpager;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.app.baselibrary.R;
import com.app.baselibrary.commonUtil.LogUtil;
import com.app.baselibrary.image.ImageUtils;


public class AutoScrollPoster extends AutoScrollableView<String> {

    private static final String TAG = "AutoScrollPoster";

    private ScaleType mScaleType = null;
    private boolean mNeedLoadAnimation = true;
    private Drawable mLoadIndeterminateDrawable = null;

    private LayoutInflater mLayoutInflater;

    public AutoScrollPoster(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AutoScrollPoster(Context context) {
        super(context);
        init();
    }

    private void init() {
        mLayoutInflater = LayoutInflater.from(getContext());

        this.setOffscreenPageLimit(1);

    }
    /**
     * Options for scaling the bounds of an image to the bounds of this view.
     *
     * @param scaleType
     */
    public void setScaleType(ScaleType scaleType) {
        this.mScaleType = scaleType;
    }

    /**
     * Needs a load animation.
     *
     * @param needAnimation
     */
    public void needLoadAnimation(boolean needAnimation) {
        this.mNeedLoadAnimation = needAnimation;
    }

    /**
     * {@link android.widget.ProgressBar#setIndeterminateDrawable(Drawable)}
     */
    public void setLoadIndeterminateDrawable(Drawable drawable) {
        this.mLoadIndeterminateDrawable = drawable;
    }

    @Override
    public Object instantiateItem(ViewGroup view, int position) {
        final View imageLayout = mLayoutInflater.inflate(R.layout.poster_list_item, view, false);
        ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
        if (mScaleType != null) {
            imageView.setScaleType(mScaleType);
        }

        final String imageUri = getItem(position);

        LogUtil.e("imageUri:"+imageUri);
        imageView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mOnItemViewClickListener != null) {
                    mOnItemViewClickListener.onItemViewClick(v, imageUri);
                }
            }
        });
        ImageUtils.loadImageView(imageUri, imageView);

        view.addView(imageLayout, 0);
        return imageLayout;
    }

    /**
     * item view click listener.
     */
    private OnItemViewClickListener mOnItemViewClickListener;

    public void setOnItemViewClickListener(OnItemViewClickListener listener) {
        this.mOnItemViewClickListener = listener;
    }

    /**
     * ItemView click
     *
     * @author 笨鸟不乖
     * @version 1.0.0
     * @email benniaobuguai@gmail.com
     * @Modify 2015-4-23
     * @since 2015-4-24
     */
    public static interface OnItemViewClickListener {

        void onItemViewClick(View view, Object object);

    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {


        ImageView imageView = (ImageView) ((View) object).findViewById(R.id.image);

        if (imageView != null) {
            imageView.setImageDrawable(null);
            Drawable drawable = imageView.getDrawable();
            if (drawable != null && drawable instanceof BitmapDrawable) {
                BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;

                if (bitmapDrawable.getBitmap() != null && !bitmapDrawable.getBitmap().isRecycled())
                    bitmapDrawable.getBitmap().recycle();
            }
        }
        System.gc();

        super.destroyItem(container, position, object);
    }
}
