package com.hanyee.androidlib.base;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.android.volley.toolbox.NetworkImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.ImageViewTarget;
import com.hanyee.androidlib.R;
import com.hanyee.androidlib.cache.image.ImageUtils;
import com.hanyee.androidlib.cache.image.glide.CircleTransform;
import com.hanyee.androidlib.cache.image.volley.VolleyImageLoader;
import com.hanyee.androidlib.utils.DisplayUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by Hanyee on 16/11/21.
 */

public abstract class BaseAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = "BaseAdapter";

    private static final int DEFAULT_VIEW_TYPE_COUNT = 1;

    protected List<T> mData = new ArrayList<>();

    private Object mContext;

    private boolean mIsScrollStateFling;

    public BaseAdapter(Object context) {
        mContext = context;
    }

    public void setData(List<T> data) {
        mData.clear();
        mData.addAll(data);
        notifyDataSetChanged();
    }

    public void addData(List<T> data) {
        mData.addAll(data);
        notifyDataSetChanged();
    }

    public void clear() {
        mData.clear();
        notifyDataSetChanged();
    }

    public void setScrollStateFling(boolean scrollState) {
        mIsScrollStateFling = scrollState;
    }

    protected View getCreatedView(ViewGroup parent, int layoutRes) {
        return LayoutInflater.from(parent.getContext()).inflate(layoutRes, parent, false);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        BaseViewHolder baseViewHolder = (BaseViewHolder) holder;
        baseViewHolder.setContext(mContext);
        baseViewHolder.setScrollState(mIsScrollStateFling);
        if (getViewTypeCount() == 1) {
            baseViewHolder.onBindData2View(mData.get(position));
        } else {
            int type = getItemViewType(position);
            onBindMultipliedTypeView(baseViewHolder, type, position);
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    protected int getViewTypeCount() {
        return DEFAULT_VIEW_TYPE_COUNT;
    }

    protected void onBindMultipliedTypeView(BaseViewHolder holder, int type, int position) {
        holder.onBindData2View(mData.get(position));
    }

    public abstract static class BaseViewHolder<T> extends RecyclerView.ViewHolder {

        private View mView;
        private int mImageViewWidth;
        private Object mContext;
        private boolean mScrollStateFling;
        private float mRatio;

        public BaseViewHolder(View view) {
            super(view);
            mView = view;
            ButterKnife.bind(this, view);
        }

        protected Context getApplicationContext() {
            return mView.getContext().getApplicationContext();
        }

        private int getImageViewWidth(View view) {
            if (mImageViewWidth == 0) {
                int screenWidth = DisplayUtils.getScreenWidth(getApplicationContext());
                int padding = 0;
                int margin = 0;
                ViewGroup parent = (ViewGroup) view.getParent();
                while (parent != null && parent != mView) {
                    Log.i(TAG, "getImageViewWidth: parent=" + parent);
                    padding += parent.getPaddingLeft() + parent.getPaddingRight();
                    ViewGroup.LayoutParams vlp = parent.getLayoutParams();
                    if (vlp instanceof FrameLayout.LayoutParams) {
                        FrameLayout.LayoutParams flp = (FrameLayout.LayoutParams) vlp;
                        margin += flp.rightMargin + flp.leftMargin;
                    }
                    parent = (ViewGroup) parent.getParent();
                }
                mImageViewWidth = screenWidth / 2 - padding - margin;
            }
            return mImageViewWidth;
        }

        private RequestManager getRequestManager() {
            RequestManager requestManager = null;
            if (mContext instanceof Activity) {
                requestManager = Glide.with((Activity) mContext);
            } else if (mContext instanceof Fragment) {
                requestManager = Glide.with((Fragment) mContext);
            } else {
                requestManager = Glide.with((Context) mContext);
            }
            return requestManager;
        }

        protected void loadAvatar(String url, ImageView view) {
            getRequestManager()
                    .load(url)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .error(R.drawable.cheese_1)
                    .fitCenter()
                    .crossFade()
                    .into(view);
        }

        protected void loadCircleAvatar(String url, ImageView view) {
            getRequestManager()
                    .load(url)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .transform(new CircleTransform(getApplicationContext()))
                    .error(R.drawable.cheese_1)
                    .crossFade()
                    .into(view);
        }

        protected void loadCustomSizeImage(String url, final NetworkImageView view) {
//            view.setDefaultImageResId(R.mipmap.ic_default);
//            view.setErrorImageResId(R.mipmap.ic_error);
            mScrollStateFling = false;
            view.setImageUrl(mScrollStateFling ? null : url,
                    VolleyImageLoader.getInstance(getApplicationContext()).getImageLoader());
        }

        protected void loadAndCompressImage(String url, ImageView view) {
            getRequestManager()
                    .load(url)
                    .downloadOnly(new ImageViewTarget<File>(view) {
                        @Override
                        protected void setResource(File resource) {
                            Bitmap bitmap = ImageUtils.resizeBitmap(resource, view.getWidth(), view.getHeight());
                            view.setImageBitmap(bitmap);
                        }
                    });
        }

        private void setContext(Object context) {
            mContext = context;
        }

        public void setScrollState(boolean scrollState) {
            mScrollStateFling = scrollState;
        }

        public abstract void onBindData2View(T data);
    }
}

@@ -214,33 +214,8 @@
 ew;%0A
-import android.util.Log;%0A
 impo
@@ -242,24 +242,24 @@
 utInflater;%0A
+
 import andro
@@ -307,43 +307,8 @@
 up;%0A
-import android.widget.FrameLayout;%0A
 impo
@@ -798,57 +798,8 @@
 der;
-%0Aimport com.hanyee.androidlib.utils.DisplayUtils;
 %0A%0Aim
@@ -1043,62 +1043,8 @@
  %7B%0A%0A
-    private static final String TAG = %22BaseAdapter%22;%0A%0A
     
@@ -2859,45 +2859,8 @@
 ew;%0A
-        private int mImageViewWidth;%0A
     
@@ -2934,38 +2934,8 @@
 ing;
-%0A        private float mRatio;
 %0A%0A  
@@ -3209,1060 +3209,8 @@
  %7D%0A%0A
-        private int getImageViewWidth(View view) %7B%0A            if (mImageViewWidth == 0) %7B%0A                int screenWidth = DisplayUtils.getScreenWidth(getApplicationContext());%0A                int padding = 0;%0A                int margin = 0;%0A                ViewGroup parent = (ViewGroup) view.getParent();%0A                while (parent != null && parent != mView) %7B%0A                    Log.i(TAG, %22getImageViewWidth: parent=%22 + parent);%0A                    padding += parent.getPaddingLeft() + parent.getPaddingRight();%0A                    ViewGroup.LayoutParams vlp = parent.getLayoutParams();%0A                    if (vlp instanceof FrameLayout.LayoutParams) %7B%0A                        FrameLayout.LayoutParams flp = (FrameLayout.LayoutParams) vlp;%0A                        margin += flp.rightMargin + flp.leftMargin;%0A                    %7D%0A                    parent = (ViewGroup) parent.getParent();%0A                %7D%0A                mImageViewWidth = screenWidth / 2 - padding - margin;%0A            %7D%0A            return mImageViewWidth;%0A        %7D%0A%0A
     
@@ -4425,32 +4425,32 @@
 ew);%0A        %7D%0A%0A
+
         protecte
@@ -4523,128 +4523,8 @@
 ) %7B%0A
-//            view.setDefaultImageResId(R.mipmap.ic_default);%0A//            view.setErrorImageResId(R.mipmap.ic_error);%0A
     

Fix the OOM issue for loading image waterfall